Skip to content

Commit b216a86

Browse files
add restart counter and fix waiter (#39)
* add restart counter and fix waiter
1 parent 35b8e42 commit b216a86

File tree

5 files changed

+32
-7
lines changed

5 files changed

+32
-7
lines changed

lib/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,24 @@ logger.validate();
2727
logger.start();
2828

2929
process.on('beforeExit', (code) => {
30+
console.log(`beforeExit: ${code}`);
3031
logger.state.beforeExitCode = code;
3132
logger._writeNewState();
3233
});
3334
process.on('exit', (code) => {
35+
console.log(`exit: ${code}`);
3436
logger.state.exitCode = code;
3537
logger._writeNewState();
3638
});
3739

3840
process.on('uncaughtException', (error) => {
41+
console.log(`uncaughtException: ${error}`);
3942
logger.state.uncaughtException = error;
4043
logger._writeNewState();
4144
});
4245

4346
process.on('unhandledRejection', (reason) => {
47+
console.log(`unhandledRejection: ${reason}`);
4448
logger.state.unhandledRejection = reason;
4549
logger._writeNewState();
4650
});

lib/logger.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const ContainerHandlingStatus = require('./enums').ContainerHandlingStatus;
1212
const ContainerLogger = require('./ContainerLogger');
1313
const { TaskLogger } = require('@codefresh-io/task-logger');
1414

15+
16+
const initialState = { status: 'init', lastLogsDate: Date.now() , failedHealthChecks: [] , restartCounter: 0, containers: {} };
1517
class 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
/**

lib/waitUntilFinish.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ class Waiter {
1212
this.finished = false;
1313
}
1414

15-
_stateListener(currentFileState) {
16-
this.lastLogsDate = currentFileState.mtimeMs;
15+
_stateListener() {
16+
const state = JSON.parse(fs.readFileSync(this.filepath, 'utf8'));
17+
this.lastLogsDate = state.lastLogsDate;
1718
}
1819

1920

service.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version: 1.2.1
1+
version: 1.2.2

test/logger.unit.spec.js

+6
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,9 @@ describe('Logger tests', () => {
389389
const Logger = proxyquire('../lib/logger', {
390390
'fs': {
391391
writeFile: writeFileSpy,
392+
readFileSync: sinon.spy(() => {
393+
return '{}';
394+
}),
392395
existsSync: () => { return true; }
393396
}
394397
});
@@ -406,6 +409,9 @@ describe('Logger tests', () => {
406409
const Logger = proxyquire('../lib/logger', {
407410
'fs': {
408411
writeFile: writeFileSpy,
412+
readFileSync: sinon.spy(() => {
413+
return '{}';
414+
}),
409415
existsSync: () => { return true; },
410416
}
411417
});

0 commit comments

Comments
 (0)