Skip to content

Commit 35b8e42

Browse files
Saas 4622 (#38)
* add firebase health check
1 parent bcf657f commit 35b8e42

File tree

6 files changed

+128
-9
lines changed

6 files changed

+128
-9
lines changed

Diff for: lib/index.js

+19
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,22 @@ const logger = new Logger({
2525

2626
logger.validate();
2727
logger.start();
28+
29+
process.on('beforeExit', (code) => {
30+
logger.state.beforeExitCode = code;
31+
logger._writeNewState();
32+
});
33+
process.on('exit', (code) => {
34+
logger.state.exitCode = code;
35+
logger._writeNewState();
36+
});
37+
38+
process.on('uncaughtException', (error) => {
39+
logger.state.uncaughtException = error;
40+
logger._writeNewState();
41+
});
42+
43+
process.on('unhandledRejection', (reason) => {
44+
logger.state.unhandledRejection = reason;
45+
logger._writeNewState();
46+
});

Diff for: lib/logger.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Logger {
2020
findExistingContainers,
2121
logSizeLimit
2222
}) {
23-
this.state = { status: 'init', lastLogsDate: Date.now() };
23+
this.state = { status: 'init', lastLogsDate: Date.now() , failedHealthChecks: [] };
2424
this.taskLoggerConfig = taskLoggerConfig;
2525
this.loggerId = loggerId;
2626
this.findExistingContainers = findExistingContainers === 'true';
@@ -71,6 +71,16 @@ class Logger {
7171
taskLogger.on('error', (err) => {
7272
logger.error(err.stack);
7373
});
74+
taskLogger.startHealthCheck();
75+
taskLogger.onHealthCheckReported((status) => {
76+
if (status.status === 'failed') {
77+
this.state.failedHealthChecks.push(status);
78+
this.state.status = 'failed';
79+
}else {
80+
this.state.healthCheckStatus = status;
81+
}
82+
this._writeNewState();
83+
});
7484

7585
this.taskLogger = taskLogger;
7686
logger.info(`taskLogger successfully created`);

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"cf-container-logger"
77
],
88
"dependencies": {
9-
"@codefresh-io/task-logger": "1.5.7",
9+
"@codefresh-io/task-logger": "1.5.9",
1010
"cf-errors": "^0.1.11",
1111
"cf-logs": "^1.1.0",
1212
"docker-events": "0.0.2",

Diff for: service.yaml

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

Diff for: test/logger.unit.spec.js

+92-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ describe('Logger tests', () => {
7474
it('should start and not listen for existing container in case findExistingContainers param is false', async () => { // jshint ignore:line
7575
const taskLogger = { // jshint ignore:line
7676
on: sinon.spy(),
77-
restore: sinon.spy(() => Q.resolve())
77+
restore: sinon.spy(() => Q.resolve()),
78+
startHealthCheck: sinon.spy(),
79+
onHealthCheckReported: sinon.spy(),
7880
};
7981
const TaskLoggerFactory = sinon.spy(() => {
8082
return Q.resolve(taskLogger);
@@ -106,11 +108,99 @@ describe('Logger tests', () => {
106108

107109
});
108110

111+
it('should report health check status if failed', async () => { // jshint ignore:line
112+
let onHealthCheckReportedCb;
113+
const onHealthCheckReportedSpy = sinon.spy((func) => {
114+
onHealthCheckReportedCb = func;
115+
});
116+
117+
const taskLogger = { // jshint ignore:line
118+
on: sinon.spy(),
119+
restore: sinon.spy(() => Q.resolve()),
120+
startHealthCheck: sinon.spy(),
121+
onHealthCheckReported: onHealthCheckReportedSpy,
122+
};
123+
const TaskLoggerFactory = sinon.spy(() => {
124+
return Q.resolve(taskLogger);
125+
});
126+
127+
const Logger = proxyquire('../lib/logger', {
128+
'@codefresh-io/task-logger': { TaskLogger: TaskLoggerFactory }
129+
});
130+
131+
const loggerId = 'loggerId';
132+
const taskLoggerConfig = {task: {}, opts: {}};
133+
const findExistingContainers = false;
134+
135+
const logger = new Logger({
136+
loggerId,
137+
taskLoggerConfig,
138+
findExistingContainers,
139+
});
140+
logger._listenForNewContainers = sinon.spy();
141+
logger._writeNewState = sinon.spy();
142+
logger._listenForExistingContainers = sinon.spy();
143+
logger.start();
144+
145+
146+
await Q.delay(10);
147+
onHealthCheckReportedCb({status: 'failed'});
148+
149+
expect(taskLogger.startHealthCheck).to.be.calledOnce;
150+
expect(logger.state.failedHealthChecks.length).to.be.equals(1);
151+
152+
});
153+
154+
it('should report health check status if succeed', async () => { // jshint ignore:line
155+
let onHealthCheckReportedCb;
156+
const onHealthCheckReportedSpy = sinon.spy((func) => {
157+
onHealthCheckReportedCb = func;
158+
});
159+
160+
const taskLogger = { // jshint ignore:line
161+
on: sinon.spy(),
162+
restore: sinon.spy(() => Q.resolve()),
163+
startHealthCheck: sinon.spy(),
164+
onHealthCheckReported: onHealthCheckReportedSpy,
165+
};
166+
const TaskLoggerFactory = sinon.spy(() => {
167+
return Q.resolve(taskLogger);
168+
});
169+
170+
const Logger = proxyquire('../lib/logger', {
171+
'@codefresh-io/task-logger': { TaskLogger: TaskLoggerFactory }
172+
});
173+
174+
const loggerId = 'loggerId';
175+
const taskLoggerConfig = {task: {}, opts: {}};
176+
const findExistingContainers = false;
177+
178+
const logger = new Logger({
179+
loggerId,
180+
taskLoggerConfig,
181+
findExistingContainers,
182+
});
183+
logger._listenForNewContainers = sinon.spy();
184+
logger._writeNewState = sinon.spy();
185+
logger._listenForExistingContainers = sinon.spy();
186+
logger.start();
187+
188+
189+
await Q.delay(10);
190+
onHealthCheckReportedCb({status: 'succeed'});
191+
192+
expect(taskLogger.startHealthCheck).to.be.calledOnce;
193+
expect(logger.state.healthCheckStatus).to.deep.equal({status: 'succeed'});
194+
195+
});
196+
109197
it('should start and listen for existing container in case findExistingContainers param is "true"', async () => {
110198

111199
const taskLogger = {
112200
on: sinon.spy(),
113-
restore: sinon.spy(() => Q.resolve())
201+
restore: sinon.spy(() => Q.resolve()),
202+
startHealthCheck: sinon.spy(),
203+
onHealthCheckReported: sinon.spy(),
114204
};
115205
const TaskLoggerFactory = sinon.spy(() => {
116206
return Q.resolve(taskLogger);

Diff for: yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
# yarn lockfile v1
33

44

5-
"@codefresh-io/[email protected].7":
6-
version "1.5.7"
7-
resolved "https://registry.yarnpkg.com/@codefresh-io/task-logger/-/task-logger-1.5.7.tgz#0871391247dfffdf47e61daca1e7c0822ae1aa8d"
8-
integrity sha512-F3NWmjsD9eAJm/2uEpDfb9y8uxZktqtD1kTGLO85OeDdmspqT9szI4ZhcKwduz8yuuPidy5Ayz4zlcFRQEBxBQ==
5+
"@codefresh-io/[email protected].9":
6+
version "1.5.9"
7+
resolved "https://registry.yarnpkg.com/@codefresh-io/task-logger/-/task-logger-1.5.9.tgz#de59799634a02fe616be23daf591adca6d0fac5b"
8+
integrity sha512-FqjK5qniqiYSyWI9SMwa35qPumuI7qgDK26UhJfnjeFuOdRu1aDP+/J9wXG1oBNGKlighxGU8oRT3+thnOc3sQ==
99
dependencies:
1010
cf-errors "^0.1.11"
1111
crypto "0.0.3"

0 commit comments

Comments
 (0)