Skip to content

Commit 7ce4741

Browse files
committed
feat: include chrome healthcheck
1 parent ede9d15 commit 7ce4741

File tree

6 files changed

+52
-10
lines changed

6 files changed

+52
-10
lines changed

__tests__/unit/render.spec.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
const Printer = require('../../src');
2-
const amqpConfig = require('../configs/amqp');
1+
const request = require('request-promise').defaults({
2+
baseUrl: 'http://localhost:3000',
3+
});
4+
5+
describe('render action', () => {
6+
const Printer = require('../../src');
7+
const amqpConfig = require('../configs/amqp');
38

4-
describe('render action', async () => {
59
let service;
610
let amqpClient;
711

@@ -85,6 +89,10 @@ describe('render action', async () => {
8589
expect(data).toMatch(/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i);
8690
});
8791

92+
it('healh check should return successful response', async () => {
93+
await request.get('/generic/health');
94+
});
95+
8896
afterAll(async () => {
8997
await service.close();
9098
});

src/configs/amqp.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ exports.amqp = {
2424
},
2525
router: {
2626
enabled: true,
27+
prefix: 'pdf',
2728
},
2829
retry: {
2930
enabled: true,

src/configs/http.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ exports.http = {
99
},
1010
router: {
1111
enabled: true,
12+
prefix: '',
1213
},
1314
};

src/configs/router.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const metrics = routerExtension('audit/metrics');
2525
exports.router = {
2626
routes: {
2727
directory: path.resolve(__dirname, '../actions'),
28-
prefix: 'pdf',
2928
setTransportsAsDefault: false,
3029
transports: [ActionTransport.amqp, ActionTransport.http],
3130
enabledGenericActions: ['health'],

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { Microfleet, ConnectorsTypes } = require('@microfleet/core');
2+
const { PluginHealthCheck } = require('@microfleet/core/lib/utils/pluginHealthStatus');
23
const merge = require('lodash/merge');
34

45
const Mustache = require('./utils/mustache');
@@ -39,5 +40,6 @@ module.exports = class PdfPrinter extends Microfleet {
3940
// add connectors & disconnectors
4041
this.addConnector(ConnectorsTypes.essential, chrome.init.bind(chrome));
4142
this.addDestructor(ConnectorsTypes.essential, chrome.kill.bind(chrome));
43+
this.addHealthCheck(new PluginHealthCheck('chrome', chrome.status.bind(chrome)));
4244
}
4345
};

src/utils/chrome.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ const noop = require('lodash/noop');
33
const ChromeRemote = require('chrome-remote-interface');
44
const { encode } = require('64');
55
const { Launcher } = require('chrome-launcher');
6+
const { HttpStatusError } = require('common-errors');
67

78
const debug = require('debug')('ms-printer:chrome');
89

910
const _SIGINT = 'SIGINT';
11+
const kStatusCheckHTML = '<html><head><title>status</title></head><body></body></html>';
1012

1113
class Chrome {
1214
/**
@@ -30,7 +32,7 @@ class Chrome {
3032
constructor(opts = {}) {
3133
const {
3234
logger,
33-
timeout = 10000,
35+
timeout = 7500,
3436
...restOpts
3537
} = opts;
3638

@@ -40,7 +42,7 @@ class Chrome {
4042
// settings
4143
this.settings = Object.assign({
4244
logLevel: 'info',
43-
port: 9222,
45+
port: 0, // generates random port
4446
chromeFlags: [
4547
'--window-size=1024,768',
4648
'--disable-gpu',
@@ -85,13 +87,42 @@ class Chrome {
8587
* @returns {Promise<null>}
8688
*/
8789
async kill() {
90+
const { launcher } = this;
91+
8892
// kill chrome instance if has been launched
89-
if (this.launcher) {
90-
await this.launcher.kill();
93+
if (launcher) {
94+
this.launcher = null;
95+
await launcher.kill();
9196
}
97+
}
9298

93-
this.launcher = null;
94-
return null;
99+
async status(attempt = 0) {
100+
this.log.debug('evaluating chrome health');
101+
102+
if (this.launcher == null) {
103+
throw new HttpStatusError(502, 'chrome not started');
104+
}
105+
106+
try {
107+
await this.printToPdf(kStatusCheckHTML);
108+
return true;
109+
} catch (e) {
110+
this.log.warn({ err: e }, 'failed chrome status check');
111+
if (attempt > 0) {
112+
throw new HttpStatusError(504, `failed to open tab: ${e.message}`);
113+
}
114+
}
115+
116+
try {
117+
this.log.warn('restarting chrome due to a healthcheck error');
118+
await Promise.all([this.kill(), this.init()]);
119+
} catch (e) {
120+
this.log.fatal({ err: e }, 'failed to restart chrome');
121+
throw new HttpStatusError(500, 'failed to restart chrome');
122+
}
123+
124+
// do a second attempt after restart
125+
return this.status(1);
95126
}
96127

97128
/**

0 commit comments

Comments
 (0)