@foxpage/foxpage-node-sdk
Version:
foxpage node sdk
80 lines (79 loc) • 2.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ignition = void 0;
const foxpage_shared_1 = require("@foxpage/foxpage-shared");
const common_1 = require("../common");
const logger_1 = require("../logger");
const manager_1 = require("../manager");
const parser_1 = require("../parser");
const pm2_1 = require("../pm2");
const reporter_1 = require("../reporter");
const lifecycle_1 = require("./lifecycle");
const logger = (0, logger_1.loggerCreate)('SDKIgnition');
/**
* SDK ignition
*
*/
async function SDKIgnition() {
// collect init errors
const errors = [];
const tryRun = async (fn, level = 'error', { retry = true } = {}) => {
try {
await fn();
}
catch (error) {
logger.error(`call "${fn.name}" fail:`, error);
if (retry) {
logger.info(`retry call "${fn.name}"`);
await tryRun(fn, level, { retry: false });
}
else if (level === 'error') {
errors.push(error);
}
}
};
const foxpageSDKInitCost = (0, reporter_1.igniteReporter)('igniteCost');
// init configs
const configInitCost = (0, reporter_1.igniteReporter)('configInitCost');
await tryRun(common_1.config.init);
configInitCost();
// init pm2
await tryRun(pm2_1.initPm2);
// init manager
const managerInitCost = (0, reporter_1.igniteReporter)('managerInitCost');
await tryRun(manager_1.initSourceManager);
managerInitCost();
// with pm2
await tryRun(pm2_1.withPm2);
// init parser
const parserInitCost = (0, reporter_1.igniteReporter)('parserInitCost');
await tryRun(parser_1.initResourceParser);
parserInitCost();
// init logger
await tryRun(logger_1.initLogger);
(0, reporter_1.runtimeReporter)();
foxpageSDKInitCost();
// do hooks
const { afterIgnite } = (0, manager_1.getHooks)() || {};
if (typeof afterIgnite === 'function') {
await tryRun(() => afterIgnite({ logger }));
}
// check errors
if (errors.length > 0) {
setTimeout(() => {
process.exit(-1);
}, 1000);
const msg = 'init foxpage sdk fail, please check errors log\n' + (0, foxpage_shared_1.formatMessages)(errors).join('\n');
logger.error(msg);
throw new Error(msg);
}
// update lifecycle
(0, lifecycle_1.success)();
}
/**
* Foxpage SDK ignition
*/
async function ignition() {
await (0, foxpage_shared_1.timeout)(SDKIgnition(), 1000 * 60 * 15);
}
exports.ignition = ignition;