UNPKG

@dfeidao/service

Version:

Node.js Service, depending on RabbitMQ.

186 lines (185 loc) 6.87 kB
#!/usr/bin/env node "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const service_1 = __importDefault(require("@dfeidao/send-msg/service")); const body_parser_1 = __importDefault(require("body-parser")); const express_1 = __importDefault(require("express")); const log4js_1 = require("log4js"); const node_schedule_1 = __importDefault(require("node-schedule")); const uuid_1 = __importDefault(require("uuid")); const config_1 = __importDefault(require("./config")); const register_1 = __importDefault(require("./register")); const logger = log4js_1.getLogger(); async function init_http(port) { const app = express_1.default(); // parse application/x-www-form-urlencoded app.use(body_parser_1.default.urlencoded({ extended: true })); // parse application/json app.use(body_parser_1.default.json({ limit: '1gb', type: '*/json' })); // parse an HTML body into a string app.use(body_parser_1.default.text({ type: 'text/html' })); app.use(body_parser_1.default.raw({ limit: '5gb' })); // todo need test const server = app.use(async (req, res) => { const headers = req.headers; const { actionid, usersessionid } = headers; res.header('actionid', actionid); res.header('usersessionid', usersessionid); const tm = new Date(); const body = req.body; const dbgmsg = 'Body=' + (Buffer.isBuffer(body) ? 'Blob Data' : JSON.stringify(body)) + 'headers=' + JSON.stringify(headers); logger.info(`Message incomming:${dbgmsg}`); try { const content = body; const ret = await service_1.default(headers, content); const str = JSON.stringify(ret.data.toString()); logger.info(`Result:${str}`); res.header({ 'messagecontrol.code': 1, 'messagecontrol.code_msg': '' }); if ((ret.status_code)) { res.status(ret.status_code); } if (ret.headers) { for (const name in ret.headers) { if (!!name) { const vals = ret.headers[name]; vals.forEach((val) => { res.header(name, val); }); } } } if (ret.content_type) { res.contentType(ret.content_type); } if (ret.cookie) { const ck = ret.cookie; Object.keys(ret.cookie).forEach((key) => { res.cookie(key, ck[key]); }); } if (ret.redirect) { res.redirect(ret.redirect); return; } if (ret.attachment) { res.attachment(ret.attachment); } res.send(ret.data); } catch (err) { logger.trace(err); const e_msg = err.message; logger.error('Service Error:' + e_msg, actionid); res.header({ 'messagecontrol.code': 0, 'messagecontrol.code_msg': encodeURIComponent(e_msg) }); res.status(500).json({ detail: err.stack, msg: err.message }); } finally { const cost = new Date().getTime() - tm.getTime(); if (cost > 500) { logger.error(`Message outgoing:${dbgmsg},Costing ${cost}ms`); } else if (cost > 200) { logger.warn(`Message outgoing:${dbgmsg},Costing ${cost}ms`); } else { logger.info(`Message outgoing:${dbgmsg},Costing ${cost}ms`); } } }); server.listen(port); } function init_schedule() { const jobs = config_1.default.jobs || []; const spaceid = config_1.default.spaceid; jobs.forEach((job) => { const desc = JSON.stringify(job); const msgtype = job.msgtype; const condition = job.data; const { rule, start, end } = job; node_schedule_1.default.scheduleJob({ end, rule, start }, async (dt) => { const actionid = uuid_1.default(); const tm = new Date(); logger.info(`Start schedule job:<${desc}>, actionid=${actionid}, which is supposed to run at:${dt.toUTCString()}, but actually ran at ${tm.toUTCString()}`); try { const body = { ...condition, spaceid }; const appid = 'nodejs-server'; const jsappid = appid; const jsgroup = `${config_1.default.spaceid}-${config_1.default.type}`; const sessionid = 'jsschedule'; const usersessionid = 'jsschedule'; const msgid = uuid_1.default(); const headers = { actionid, appid, 'content-type': 'application/json', jsappid, jsgroup, msgid, msgtype, sessionid, spaceid, usersessionid }; const ret = await service_1.default(headers, body); const str = JSON.stringify(ret); logger.info(`Schedule job:<${desc}>, actionid=${actionid}, Result=${str}`); } catch (err) { logger.trace(err); const e_msg = err.message; logger.error('Service Error:' + e_msg, actionid); } finally { const cost = new Date().getTime() - tm.getTime(); if (cost > 500) { logger.error(`Finish schedule job:${desc}, actionid = ${actionid},Costing ${cost}ms`); } else if (cost > 200) { logger.warn(`Finish schedule job:${desc}, actionid = ${actionid},Costing ${cost}ms`); } else { logger.info(`Finish schedule job:${desc}, actionid = ${actionid},Costing ${cost}ms`); } } }); }); } async function main() { process.on('SIGINT', () => { process.exit(0); }); log4js_1.configure('./log4js.json'); logger.warn('Starting Node.js service...........^v^'); await init_http(config_1.default.port); register_1.default(); init_schedule(); logger.warn('Node.js service is started...........^v^'); } main();