Process logs API refactor
There are three separate instances of process log APIs: embark logs, blockchain logs (when in standalone mode), and child process logs (storage, communication, blockchain, etc). Each one was repeating the implementation of creating a process log API endpoint. This commit centralises the API declaration by using the class `ProcessLogsApi`.
`ProcessLogsApi` is started for all three components mentioned above: blockchain (in standalone) in the `BlockchainListener` module, embark in the `EmbarkListener` module, and for all child processes in the `ProcessLauncher`.
These listeners have two functions:
1. Create the process logs API endpoints for `get` and `ws`, and
2. Ensure that all logs are logged through the `LogHandler`, which normalises the output of the log and ensures each log has a timestamp and id (used in the cockpit for log ordering).
Also, this commit moved the pipeline in to a module, so that the `embark` object could be passed to the `ProcessLogsApi` (to be used for registering API endpoints).
2018-10-24 05:54:09 +00:00
|
|
|
const LogHandler = require('../../utils/logHandler');
|
|
|
|
|
|
|
|
class ProcessLogsApi {
|
|
|
|
constructor({embark, processName, silent}) {
|
|
|
|
this.embark = embark;
|
|
|
|
this.processName = processName;
|
|
|
|
this.logger = this.embark.logger;
|
|
|
|
this.events = this.embark.events;
|
|
|
|
this.logHandler = new LogHandler({events: this.events, logger: this.logger, processName: this.processName, silent});
|
|
|
|
|
|
|
|
this.registerAPICalls();
|
|
|
|
}
|
|
|
|
|
|
|
|
registerAPICalls() {
|
|
|
|
const apiRoute = '/embark-api/process-logs/' + this.processName;
|
|
|
|
this.embark.registerAPICall(
|
|
|
|
'ws',
|
|
|
|
apiRoute,
|
|
|
|
(ws, _req) => {
|
|
|
|
this.events.on('process-log-' + this.processName, function (log) {
|
|
|
|
ws.send(JSON.stringify(log), () => {});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
this.embark.registerAPICall(
|
|
|
|
'get',
|
|
|
|
'/embark-api/process-logs/' + this.processName,
|
|
|
|
(req, res) => {
|
|
|
|
let limit = parseInt(req.query.limit, 10);
|
|
|
|
if (!Number.isInteger(limit)) limit = 0;
|
2018-10-25 06:01:48 +00:00
|
|
|
const result = this.logHandler.logs.slice(limit * -1);
|
Process logs API refactor
There are three separate instances of process log APIs: embark logs, blockchain logs (when in standalone mode), and child process logs (storage, communication, blockchain, etc). Each one was repeating the implementation of creating a process log API endpoint. This commit centralises the API declaration by using the class `ProcessLogsApi`.
`ProcessLogsApi` is started for all three components mentioned above: blockchain (in standalone) in the `BlockchainListener` module, embark in the `EmbarkListener` module, and for all child processes in the `ProcessLauncher`.
These listeners have two functions:
1. Create the process logs API endpoints for `get` and `ws`, and
2. Ensure that all logs are logged through the `LogHandler`, which normalises the output of the log and ensures each log has a timestamp and id (used in the cockpit for log ordering).
Also, this commit moved the pipeline in to a module, so that the `embark` object could be passed to the `ProcessLogsApi` (to be used for registering API endpoints).
2018-10-24 05:54:09 +00:00
|
|
|
res.send(JSON.stringify(result));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ProcessLogsApi;
|