@@ -12,6 +12,8 @@ const ContainerHandlingStatus = require('./enums').ContainerHandlingStatus;
12
12
const ContainerLogger = require ( './ContainerLogger' ) ;
13
13
const { TaskLogger } = require ( '@codefresh-io/task-logger' ) ;
14
14
15
+
16
+ const initialState = { status : 'init' , lastLogsDate : Date . now ( ) , failedHealthChecks : [ ] , restartCounter : 0 , containers : { } } ;
15
17
class Logger {
16
18
17
19
constructor ( {
@@ -20,7 +22,6 @@ class Logger {
20
22
findExistingContainers,
21
23
logSizeLimit
22
24
} ) {
23
- this . state = { status : 'init' , lastLogsDate : Date . now ( ) , failedHealthChecks : [ ] } ;
24
25
this . taskLoggerConfig = taskLoggerConfig ;
25
26
this . loggerId = loggerId ;
26
27
this . findExistingContainers = findExistingContainers === 'true' ;
@@ -41,6 +42,7 @@ class Logger {
41
42
this . docker = new Docker ( {
42
43
socketPath : dockerSockPath ,
43
44
} ) ;
45
+ this . _readState ( ) ;
44
46
}
45
47
46
48
/**
@@ -103,6 +105,18 @@ class Logger {
103
105
} ) ;
104
106
}
105
107
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
+ }
106
120
/**
107
121
* will print the error and exit the process
108
122
* @param err
@@ -205,7 +219,7 @@ class Logger {
205
219
}
206
220
207
221
208
- this . state [ containerId ] = { status : ContainerHandlingStatus . INITIALIZING } ;
222
+ this . state . containers [ containerId ] = { status : ContainerHandlingStatus . INITIALIZING } ;
209
223
logger . info ( `Handling container: ${ containerId } , status: '${ containerStatus } '` ) ;
210
224
const stepLogger = this . taskLogger . create ( stepName , undefined , runCreationLogic ) ;
211
225
logger . info ( `Brought step logger for container: ${ containerId } ` ) ;
@@ -228,7 +242,7 @@ class Logger {
228
242
229
243
containerLogger . start ( )
230
244
. done ( ( ) => {
231
- this . state [ containerId ] = { status : ContainerHandlingStatus . LISTENING } ;
245
+ this . state . containers [ containerId ] = { status : ContainerHandlingStatus . LISTENING } ;
232
246
this . _writeNewState ( ) ;
233
247
} , ( err ) => {
234
248
const error = new CFError ( {
@@ -256,7 +270,7 @@ class Logger {
256
270
* @private
257
271
*/
258
272
_containerHandled ( containerId ) {
259
- return this . state [ containerId ] ;
273
+ return this . state . containers [ containerId ] ;
260
274
}
261
275
262
276
/**
0 commit comments