feat(@embark/webserver): Add support for service webserver on/off

Add support for `service webserver on/off` commands.

Deprecate commands `webserver start/stop` in favor of `service webserver on/off`.

Handles passing through of arguments to the function executed after process launch.

`service webserver on` - Enables the webserver serving the DApp. Shows an error if the webserver is already starting or started.

`service webserver off` - Disables the webserver serving the DApp. Shows an error if the webserver is already stopping or stopped.

`webserver start` - This command has been deprecated in favor of `service webserver on` and will be removed in future versions.

`webserver stop` - This command has been deprecated in favor of `service webserver off` and will be removed in future versions.
This commit is contained in:
emizzle 2019-04-29 16:05:35 +10:00 committed by Iuri Matias
parent 7495095d49
commit 0c394fea29
2 changed files with 48 additions and 21 deletions

View File

@ -24,6 +24,7 @@ class WebServer {
this.port = parseInt(this.webServerConfig.port, 10);
this.enableCatchAll = this.webServerConfig.enableCatchAll === true;
this.enableCatchAll = false; // FIXME when true, some Requests end up failing (eg: process-logs)
this.isServiceRegistered = false;
this.events.request('processes:register', 'webserver', {
launchFn: (cb) => { this.server.start(cb); },
@ -46,7 +47,10 @@ class WebServer {
this.listenToCommands();
this.registerConsoleCommands();
this.init();
}
init() {
this.events.on('webserver:config:change', () => {
this.embark.config.webServerConfig = null;
this.embark.config.loadWebServerConfigFile();
@ -78,48 +82,70 @@ class WebServer {
this.setServiceCheck();
});
});
}
setServiceCheck() {
const self = this;
this.events.request("services:register", 'Webserver', function (cb) {
let url = self.protocol + '://' + canonicalHost(self.host) + ':' + self.port;
checkIsAvailable(url, function (available) {
let devServer = __('Webserver') + ' (' + url + ')';
let serverStatus = (available ? 'on' : 'off');
return cb({name: devServer, status: serverStatus});
});
});
this.events.on('check:wentOffline:Webserver', () => {
this.logger.info(__("Webserver is offline"));
});
this.events.on('check:backOnline:Webserver', () => {
this.logger.info(__("Webserver is online"));
});
}
setServiceCheck() {
if (!this.isServiceRegistered) {
this.isServiceRegistered = true;
this.events.request("services:register", 'Webserver', (cb) => {
let url = this.protocol + '://' + canonicalHost(this.host) + ':' + this.port;
checkIsAvailable(url, function (available) {
let devServer = __('Webserver') + ' (' + url + ')';
let serverStatus = (available ? 'on' : 'off');
return cb({name: devServer, status: serverStatus});
});
});
}
}
listenToCommands() {
this.events.setCommandHandler('placeholder:build', (cb) => this.buildPlaceholderPage(cb));
this.events.setCommandHandler('browser:open', (cb) => this.openBrowser(cb));
this.events.setCommandHandler('webserver:start', (cb) => this.events.request('processes:launch', 'webserver', cb));
this.events.setCommandHandler('webserver:stop', (cb) => this.events.request('processes:stop', 'webserver', cb));
// TODO: remove this in v5
this.events.setCommandHandler('webserver:start', (cb) => {
this.logger.warn(__("The event 'webserver:start' has been deprecated and will be removed in future versions."));
this.events.request('processes:launch', 'webserver', cb);
});
// TODO: remove this in v5
this.events.setCommandHandler('webserver:stop', (cb) => {
this.logger.warn(__("The event 'webserver:stop' has been deprecated and will be removed in future versions."));
this.events.request('processes:stop', 'webserver', cb);
});
this.events.setCommandHandler('logs:webserver:enable', (cb) => this.server.enableLogging(cb));
this.events.setCommandHandler('logs:webserver:disable', (cb) => this.server.disableLogging(cb));
}
// TODO: remove this in v5
registerConsoleCommands() {
this.embark.registerConsoleCommand({
usage: "webserver start/stop",
description: __("Start or stop the websever"),
matches: ['webserver start'],
process: (cmd, callback) => {
this.events.request('webserver:start', callback);
const message = __("The command 'webserver:start' has been deprecated in favor of 'service webserver on' and will be removed in future versions.");
this.logger.warn(message); // logs to Embark's console
this.events.request('processes:launch', 'webserver', (_err, msg) => {
callback(_err || msg); // logs to Cockpit's console
});
}
});
// TODO: remove this in v5
this.embark.registerConsoleCommand({
matches: ['webserver stop'],
process: (cmd, callback) => {
this.events.request('webserver:stop', callback);
const message = __("The command 'webserver:stop' has been deprecated in favor of 'service webserver off' and will be removed in future versions.");
this.logger.warn(message); // logs to Embark's console
this.events.request('processes:stop', 'webserver', (_err, msg) => {
callback(_err || msg); // logs to Cockpit's console
});
}
});

View File

@ -90,17 +90,18 @@ class ProcessManager {
const enable = cmd.trim().endsWith('on');
this.logger.info(`${enable ? 'Starting' : 'Stopping'} the ${name} process...`);
if(enable) {
return this.events.request("processes:launch", name, (err) => {
if (err) this.logger.info(err); // writes to embark's console
return this.events.request("processes:launch", name, (...args) => {
const err = args[0];
if (err) return this.logger.error(err); // writes to embark's console
const process = self.processes[name];
if(process && process.afterLaunchFn) {
process.afterLaunchFn.call(process.afterLaunchFn, err);
process.afterLaunchFn.apply(process.afterLaunchFn, args);
}
callback(err, `${name} process started.`); // passes a message back to cockpit console
});
}
this.events.request("processes:stop", name, (err) => {
if (err) this.logger.info(err); // writes to embark's console
if (err) return this.logger.error(err); // writes to embark's console
callback(err, `${name} process stopped.`); // passes a message back to cockpit console
});
}