@@ -12,6 +12,8 @@ const ContainerHandlingStatus = require('./enums').ContainerHandlingStatus;
1212const ContainerLogger = require ( './ContainerLogger' ) ;
1313const { TaskLogger } = require ( '@codefresh-io/task-logger' ) ;
1414
15+
16+ const initialState = { status : 'init' , lastLogsDate : Date . now ( ) , failedHealthChecks : [ ] , restartCounter : 0 , containers : { } } ;
1517class Logger {
1618
1719 constructor ( {
@@ -20,7 +22,6 @@ class Logger {
2022 findExistingContainers,
2123 logSizeLimit
2224 } ) {
23- this . state = { status : 'init' , lastLogsDate : Date . now ( ) , failedHealthChecks : [ ] } ;
2425 this . taskLoggerConfig = taskLoggerConfig ;
2526 this . loggerId = loggerId ;
2627 this . findExistingContainers = findExistingContainers === 'true' ;
@@ -41,6 +42,7 @@ class Logger {
4142 this . docker = new Docker ( {
4243 socketPath : dockerSockPath ,
4344 } ) ;
45+ this . _readState ( ) ;
4446 }
4547
4648 /**
@@ -103,6 +105,18 @@ class Logger {
103105 } ) ;
104106 }
105107
108+ _readState ( ) {
109+ const filePath = `${ __dirname } /state.json` ;
110+ if ( fs . existsSync ( filePath ) ) {
111+ this . state = _ . omit ( JSON . parse ( fs . readFileSync ( filePath , 'utf8' ) , 'containers' ) ) ;
112+ this . state . containers = { } ;
113+ let restartCounter = _ . get ( this . state , 'restartCounter' , 0 ) ;
114+ restartCounter ++ ;
115+ this . state . restartCounter = restartCounter ;
116+ } else {
117+ this . state = initialState ;
118+ }
119+ }
106120 /**
107121 * will print the error and exit the process
108122 * @param err
@@ -205,7 +219,7 @@ class Logger {
205219 }
206220
207221
208- this . state [ containerId ] = { status : ContainerHandlingStatus . INITIALIZING } ;
222+ this . state . containers [ containerId ] = { status : ContainerHandlingStatus . INITIALIZING } ;
209223 logger . info ( `Handling container: ${ containerId } , status: '${ containerStatus } '` ) ;
210224 const stepLogger = this . taskLogger . create ( stepName , undefined , runCreationLogic ) ;
211225 logger . info ( `Brought step logger for container: ${ containerId } ` ) ;
@@ -228,7 +242,7 @@ class Logger {
228242
229243 containerLogger . start ( )
230244 . done ( ( ) => {
231- this . state [ containerId ] = { status : ContainerHandlingStatus . LISTENING } ;
245+ this . state . containers [ containerId ] = { status : ContainerHandlingStatus . LISTENING } ;
232246 this . _writeNewState ( ) ;
233247 } , ( err ) => {
234248 const error = new CFError ( {
@@ -256,7 +270,7 @@ class Logger {
256270 * @private
257271 */
258272 _containerHandled ( containerId ) {
259- return this . state [ containerId ] ;
273+ return this . state . containers [ containerId ] ;
260274 }
261275
262276 /**
0 commit comments