@angstone/monostone
Version:
monolitic event-sourced framework
164 lines • 6.09 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
// Util functions used mainly to change color
const ast = require("@angstone/node-util");
// error handler
const error_1 = require("./error");
// framework modules
const modules_1 = require("./modules");
// import { CronjobController } from "./cronjob_controller";
// framework store
const store_1 = require("./store");
// framework tools
const tools_1 = require("./tools");
// framework system commands
const SystemCommands = require("./system_commands");
class App {
/**
* Creates the main application
*/
constructor() {
// public cronjobController: CronjobController;
this.features = [];
/**
* used when application stops
*/
this.stopped = false;
this.stopping = false;
/**
* time in milliseconds gmt 0 since aplication was loaded
*/
this.timeLoaded = null;
this.rethinkDbConnected = false;
this.systemTools = tools_1.SystemTools;
this.customFeaturesPath = undefined;
this.timeStarted = Date.now();
this.config();
this.monoModules = [
modules_1.EventModule,
modules_1.ReducerModule,
modules_1.ViewModule,
modules_1.EffectModule,
modules_1.PortalModule,
modules_1.CronjobModule
];
this.monoModules.forEach(monoModule => monoModule.config());
global.monoApp = this;
}
connectStore() {
return __awaiter(this, void 0, void 0, function* () {
yield store_1.connectStore();
});
}
start() {
return __awaiter(this, void 0, void 0, function* () {
yield tools_1.EventTools.send({ command: SystemCommands.starting });
yield this.connectStore();
this.features = this.loadFeatures();
this.monoModules.forEach(controller => controller.loadFeatures(this.features));
yield tools_1.GeneralTools.asyncForEach(this.monoModules, (monoModule) => __awaiter(this, void 0, void 0, function* () { return yield monoModule.start(); }));
this.timeLoaded = Date.now();
const systemInfo = { load_time: this.timeLoaded - this.timeStarted };
yield this.sendCommand(SystemCommands.started, systemInfo);
});
}
stop() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.stopping && !this.stopped) {
this.stopping = true;
while (!(yield this.isFree())) {
yield ast.delay(App.FREE_REST_TIME);
}
yield tools_1.GeneralTools.asyncForEach(this.monoModules.reverse(), (monoModule) => __awaiter(this, void 0, void 0, function* () { return yield monoModule.stop(); }));
yield store_1.closeStore();
this.stopped = true;
}
});
}
sendCommand(command, request) {
return __awaiter(this, void 0, void 0, function* () {
return yield tools_1.EventTools.send({ command, request });
});
}
config() {
if (process.env.NODE_ENV === "production"
|| process.env.NODE_ENV === "development")
ast.success("configuration loaded");
else
error_1.error.fatal("configuration failed - please verify .env file");
ast.info("configured environment: " + process.env.NODE_ENV);
this.customFeaturesPath = process.env.FEATURES_PATH;
}
loadFeatures() {
ast.log("loadind features");
let BasicFeatures = [];
try {
BasicFeatures = require('./features').features;
}
catch (e) {
BasicFeatures = tools_1.FeatureTools.getRecipesFromFolderStructure();
}
finally {
let features = [...BasicFeatures];
if (this.customFeaturesPath) {
features =
tools_1.FeatureTools.getRecipesFromFolderStructure(this.customFeaturesPath)
.concat(features);
}
return tools_1.FeatureTools.createFeatures(features);
}
}
/**
* check if it does not have ongoing tasks
* @return boolean
*/
isFree() {
return __awaiter(this, void 0, void 0, function* () {
for (const controller of this.monoModules) {
if (!(yield controller.isFree()))
return false;
}
return true;
});
}
getExpressPortal() {
return modules_1.PortalModule.getExpressApp();
}
}
App.FREE_REST_TIME = 10;
exports.App = App;
exports.devWipeAll = (done) => {
if (process.env.NODE_ENV == 'production')
throw new Error("not allowed in production");
else {
let appWiped = new App();
appWiped.connectStore().then(() => {
appWiped.systemTools.dbDrop().then(() => {
appWiped.systemTools.eventClear().then(() => {
done();
});
});
});
}
};
exports.devWipeDb = (done) => {
if (process.env.NODE_ENV == 'production')
throw new Error("not allowed in production");
else {
let appWiped = new App();
appWiped.connectStore().then(() => {
appWiped.systemTools.dbDrop().then(() => {
done();
});
});
}
};
//# sourceMappingURL=app.js.map
;