Skip to content

Commit

Permalink
v0.3.8:
Browse files Browse the repository at this point in the history
Added support for manually loading/unloading services and controllers from within the Blackrock module and/or within other services
  • Loading branch information
darrensmith committed Nov 26, 2020
1 parent 75ff2ae commit ec59ac9
Show file tree
Hide file tree
Showing 27 changed files with 1,103 additions and 188 deletions.
6 changes: 3 additions & 3 deletions interfaces/http/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@
op.map(evt => { if(evt) return streamFns.routeRequest(evt); })
);
stream2.subscribe(function HTTPProcessRequestStreamSubscribeHandler(res) {
//console.log(res);
null;
});
}
});
Expand Down Expand Up @@ -282,7 +282,7 @@
op.map(evt => { if(evt) return streamFns.afterResPromise(evt); })
);
stream2.subscribe(function HTTPProcessResponseStreamSubscribeHandler(res) {
//console.log(res);
null;
});
}
});
Expand Down Expand Up @@ -344,7 +344,7 @@
else { form.uploadDir = core.fetchBasePath("root") + "./upload/"; }
if(config.interfaces.http[evt.req.interface].maxUploadFileSizeMb) { form.maxFileSize = config.interfaces.http[evt.req.interface].maxUploadFileSizeMb * 1024 * 1024; }
else { form.maxFileSize = 50 * 1024 * 1024; }
try { form.parse(evt.req, function HTTPParseMultiPartFormParser(err, fields, files) { console.log("err", err); console.log("fields", fields); console.log("files", files); var body = fields; body.files = files; body.error = err; evt.data = body; resolve(evt); }); }
try { form.parse(evt.req, function HTTPParseMultiPartFormParser(err, fields, files) { var body = fields; body.files = files; body.error = err; evt.data = body; resolve(evt); }); }
catch (err) { evt.data = {error: "File Upload Size Was Too Large"}; resolve(evt); }
log("debug", "Blackrock HTTP Interface > [3] Parsed Multi-Part Request Message", {}, "HTTP_REQ_PARSED_MULTI_PART");
});
Expand Down
3 changes: 0 additions & 3 deletions interfaces/zeromq/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
interface.startInterface = startInterface;
core.on("CORE_START_INTERFACES", function(evt) { interface.startInterfaces(); });
/*
console.log('testing zeromq');
const zmq = require("./support/zeromq/v5-compat");
var WORKERS_NUM = 10;
Expand All @@ -30,7 +29,6 @@
var endTime = d.getTime() + 5000;
router.bind('tcp://*:9000', function(err) {
if(err) console.log('error binding', err);
router.on('message', function () {
var identity = Array.prototype.slice.call(arguments)[0];
var d = new Date();
Expand All @@ -52,7 +50,6 @@
worker.on('message', function (msg) {
var message = msg.toString();
if (message === 'Fired!'){
console.log('Completed %d tasks', total);
worker.close();
}
total++;
Expand Down
86 changes: 50 additions & 36 deletions modules/cli/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
* Initialise Core Module Variables: *
* ================================= */

String.prototype.endsWith = function CLIEndsWith(suffix) {return this.indexOf(suffix, this.length - suffix.length) !== -1;};
var core, mod, log, enableConsole;
var core, mod, log, enableConsole, cliCommands = {};







Expand All @@ -28,9 +29,10 @@
* (Constructor) Initialises the module
* @param {object} coreObj - The parent core object
*/
var init = function CLIInit(coreObj) {
var init = function CLIInit(coreObj) {
core = coreObj, mod = new core.Mod("CLI");
setupExternalModuleMethods();
mod.register = register;
log("debug", "Blackrock CLI > Initialising...", {}, "CLI_INIT");
core.on("CORE_FINALISING", function(evt) { start(); });
return mod;
Expand Down Expand Up @@ -76,6 +78,26 @@



/* ==================== *
* Bind Module Methods: *
* ==================== */

/**
* (External) Register CLI Commands
*/
var register = function CLIRegisterCommands(commands){
if(typeof commands === 'object' && commands !== null && !Array.isArray(commands)) {
cliCommands[commands.cmd] = commands;
} else if(Array.isArray(commands)) {
for (var i = 0; i < commands.length; i++) {
cliCommands[commands[i].cmd] = commands[i];
}
}
return true;
}






Expand All @@ -92,35 +114,28 @@
var start = function CLIStart(){
process.nextTick(function CLIStartNextTickCallback(){

process.argv.push("terminator");
const lib = core.lib, rx = lib.rxjs, op = lib.operators, stream = new rx.from(process.argv);
var subRec = false;

var showHelp = function CLIShowHelp() {
var servicesAvailable = true;
core.stopActivation = true;
console.log("\n");
console.log("Usage: " + core.pkg().name + " [options]\n");
console.log("Options: ");
console.log(" config Starts Server Configuration Flow");
if(core.module("daemon") && servicesAvailable){
console.log(" start Starts the daemon server");
}
if(servicesAvailable){
console.log(" start console Starts a non-interactive console server (blocking)");
console.log("start console\t\t\t\tStarts the server in console mode")
for (var cmd in cliCommands) {
console.log(cmd + " " + cliCommands[cmd].params + "\t\t\t" + cliCommands[cmd].info);
}
if(core.module("daemon") && servicesAvailable){
console.log(" stop Stops the daemon server");
console.log(" restart Restarts the daemon server");
console.log(" status Shows the status of the daemon server");
}
console.log("\n\n");
console.log("\n");
process.exit();
return;
}

log("debug", "Blackrock CLI > Server Initialisation Pipeline Created - Executing Now:", {}, "CLI_EXEC_INIT_PIPELINE");

stream.pipe(

op.filter(function CLIStreamFn1Filter(evt) {
String.prototype.endsWith = function CLIEndsWith(suffix) {return this.indexOf(suffix, this.length - suffix.length) !== -1;};
var endsWithAny = function CLIEndsWithAny(suffixes, string) { for (let suffix of suffixes) { if(string.endsWith(suffix)) return true; } return false; };
return !endsWithAny([
"sudo", "node", "nodemon", "forever", "blackrock", "index.js",
Expand All @@ -141,27 +156,26 @@
})

).subscribe(function CLISubscribeCallback(val) {
subRec = true;
var daemonOptions = ["start", "start daemon", "stop", "stop daemon", "restart", "restart daemon", "status", "status daemon"];
if(((val && daemonOptions.includes(val) && core.module("daemon")) || (core.cfg().cli && core.cfg().cli.mode && core.cfg().cli.mode == "daemon")) && !process.send) {
log("debug", "Blackrock CLI > [3] Event sent to 'CORE_START_DAEMON'", {}, "CLI_EVTS_DAEMON_DEP");
core.emit("CORE_START_DAEMON");
} else if (process.send || val == "start console" || core.globals.get("test") || (core.cfg().cli && core.cfg().cli.mode && core.cfg().cli.mode == "console")) {
log("debug", "Blackrock CLI > [3] Start Console Called - Enabling Console and emitting CORE_START_INTERFACES", {}, "CLI_ENABLE_CONSOLE");
setTimeout(function CLIEnableConsoleTimeout(){ enableConsole(); }, 50);
core.emit("CORE_START_INTERFACES");
} else {
showHelp();
log("debug", "Blackrock CLI > [3] No valid commands received - Displaying Command-Line Help", {}, "CLI_NO_ARGS_SHOWING_HELP");
}
val = val.replace("terminator", "").trim();
setTimeout(function(){
var command;
for(var cmd in cliCommands) { if(val.startsWith(cmd)) { command = cmd; } }
if (val == "start console" || core.globals.get("test") || process.send) {
core.stopActivation = false;
log("debug", "Blackrock CLI > [3] Start Console Called - Enabling Console and emitting CORE_START_INTERFACES", {}, "CLI_ENABLE_CONSOLE");
setTimeout(function CLIEnableConsoleTimeout(){ enableConsole(); }, 50);
core.emit("CORE_START_INTERFACES");
} else if(cliCommands[command] && cliCommands[command].fn) {
log("debug", "Blackrock CLI > [3] Registered CLI command being executed", { "cmd": val }, "CLI_EXECUTING_CLI_COMMAND");
cliCommands[command].fn(val.slice(command.length));
} else {
showHelp();
log("debug", "Blackrock CLI > [3] No valid commands received - Displaying Command-Line Help", {}, "CLI_NO_ARGS_SHOWING_HELP");
}
}, 5);

}).unsubscribe();

setTimeout(function CLIHelpTimeoutCallback(){ if(!subRec) {
showHelp();
log("debug", "Blackrock CLI > [4] Timed Out Whilst Processing Command-Line Arguments - Displaying Command-Line Help", {}, "CLI_TIMEOUT_SHOWING_HELP");
} }, 1);

});
}

Expand Down
162 changes: 154 additions & 8 deletions modules/configure/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@


/**
* (Internal > Pipeline [1]) Setup Containers
* (Internal > Pipeline [1]) Setup Configure
*/
pipelines.setupConfigure = function ConfigureSetupPipeline(){
return new core.Base().extend({
Expand All @@ -64,11 +64,21 @@
const stream1 = stream.pipe(

// Fires once on server initialisation:
op.map(evt => { if(evt) return streamFns.setupModule(evt); })
streamFns.registerWithCLI,

// Fires once per CLI command:
streamFns.listenToStart,
streamFns.reloadConfig,
streamFns.listConfig,
streamFns.getConfig,
streamFns.updateConfig

);
stream1.subscribe(function ConfigureSetupPipelineSubscribe(res) {
//console.log(res);
if(!res.complete) {
console.log("\nNot Implemented - Configure\n");
}
process.exit();
});
}
});
Expand All @@ -91,12 +101,148 @@
*/

/**
* (Internal > Stream Methods [1]) Setup Module
* @param {object} evt - The Request Event
* (Internal > Stream Methods [1]) Register With CLI
* @param {observable} source - The Source Observable
*/
streamFns.setupModule = function ConfigureSetup(evt){
log("debug", "Blackrock Configure > [1] Module Not Implemented", {}, "CONFIGURE_NOT_IMPLEMENTED");
return evt;
streamFns.registerWithCLI = function ConfigureRegisterWithCLI(source){
return new Observable(observer => {
const subscription = source.subscribe({
next(evt) {
log("debug", "Blackrock Configure > [1] Configure registering with CLI...", {}, "CONFIGURE_REGISTER_WITH_CLI");
core.isLoaded("cli").then(function(cliMod) {
cliMod.register([
{"cmd": "update", "params": "[param]=[value]", "info": "Updates a config parameter", "fn": function(params) { core.emit("CONFIGURE_INIT_CONFIGURE", { "command": "update", "params": params }); }},
{"cmd": "list", "params": "\t\t", "info": "Shows list of config parameters", "fn": function(params) { core.emit("CONFIGURE_INIT_CONFIGURE", { "command": "list", "params": params }); }},
{"cmd": "get", "params": "[param]\t", "info": "Gets value for a config parameter", "fn": function(params) { core.emit("CONFIGURE_INIT_CONFIGURE", { "command": "get", "params": params }); }},
{"cmd": "reload", "params": "\t\t", "info": "Reloads system config file", "fn": function(params) { core.emit("CONFIGURE_INIT_CONFIGURE", { "command": "reload", "params": params }); }}
]);
}).catch(function(err) {
log("error", "Blackrock Configure > Failed to register with CLI - CLI module not loaded", {}, "CONFIGURE_CLI_MOD_NOT_LOADED");
});
observer.next(evt);
},
error(error) { observer.error(error); }
});
return () => subscription.unsubscribe();
});
}

/**
* (Internal > Stream Methods [2]) Listen to Start Endpoint
* @param {observable} source - The Source Observable
*/
streamFns.listenToStart = function ConfigurePipelineFnsListenToStart(source){
return new Observable(observer => {
const subscription = source.subscribe({
next(evt) {
log("debug", "Blackrock Configure > [2a] Listener created for 'CONFIGURE_INIT_CONFIGURE' event", {}, "CONFIGURE_LISTENER_CREATED");
core.on("CONFIGURE_INIT_CONFIGURE", function ConfigurePipelineFns1ListenToStartStartConfigureCallback(configParams){
core.stopActivation = true;
log("debug", "Blackrock Configure > [2b] 'CONFIGURE_INIT_CONFIGURE' Event Received", {}, "CONFIGURE_LISTENER_EVT_RECEIVED");
evt.command = configParams.command;
evt.params = configParams.params;
evt.complete = false;
observer.next(evt);
});
},
error(error) { observer.error(error); }
});
return () => subscription.unsubscribe();
});
}

/**
* (Internal > Stream Methods [3]) Reloads Config
* @param {observable} source - The Source Observable
*/
streamFns.reloadConfig = function ConfigurePipelineFnsReloadConfig(source){
return new Observable(observer => {
const subscription = source.subscribe({
next(evt) {
if(evt.command == "reload") {
log("debug", "Blackrock Configure > [3] Reloading System Config...", {}, "CONFIGURE_RELOADING_CONFIG");
var configPath = core.fetchBasePath("config");
try {
var config = require(configPath);
} catch(err) {
evt.error = err;
observer.next(evt);
return;
}
var result = core.updateConfig(config);
if(result) { evt.complete = true; }
observer.next(evt);
} else {
observer.next(evt);
}
},
error(error) { observer.error(error); }
});
return () => subscription.unsubscribe();
});
}

/**
* (Internal > Stream Methods [4]) Lists Config
* @param {observable} source - The Source Observable
*/
streamFns.listConfig = function ConfigurePipelineFnsListConfig(source){
return new Observable(observer => {
const subscription = source.subscribe({
next(evt) {
if(evt.command == "list") {
log("debug", "Blackrock Configure > [4] Listing System Config...", {}, "CONFIGURE_LISTING_CONFIG");
observer.next(evt);
} else {
observer.next(evt);
}
},
error(error) { observer.error(error); }
});
return () => subscription.unsubscribe();
});
}

/**
* (Internal > Stream Methods [5]) Gets Config
* @param {observable} source - The Source Observable
*/
streamFns.getConfig = function ConfigurePipelineFnsGetConfig(source){
return new Observable(observer => {
const subscription = source.subscribe({
next(evt) {
if(evt.command == "list") {
log("debug", "Blackrock Configure > [5] Getting System Config...", {}, "CONFIGURE_GETTING_CONFIG");
observer.next(evt);
} else {
observer.next(evt);
}
},
error(error) { observer.error(error); }
});
return () => subscription.unsubscribe();
});
}

/**
* (Internal > Stream Methods [6]) Updates Config
* @param {observable} source - The Source Observable
*/
streamFns.updateConfig = function ConfigurePipelineFnsUpdateConfig(source){
return new Observable(observer => {
const subscription = source.subscribe({
next(evt) {
if(evt.command == "list") {
log("debug", "Blackrock Configure > [6] Updating System Config...", {}, "CONFIGURE_UPDATING_CONFIG");
observer.next(evt);
} else {
observer.next(evt);
}
},
error(error) { observer.error(error); }
});
return () => subscription.unsubscribe();
});
}


Expand Down
2 changes: 1 addition & 1 deletion modules/containers/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

);
stream1.subscribe(function ContainersSetupPipelineSubscribe(res) {
//console.log(res);
null;
});
}
});
Expand Down
Loading

0 comments on commit ec59ac9

Please sign in to comment.