serverapm2.js 967 Bytes
const bb = require('bluebird');
const compose = require('koa-compose');

const logger = global.yoho.logger;

const {
  handleErrorReport,
  handleProcessInfo,
  handleWebServerDuration,
  handleAlertInfo
} = require('./serverapm-service2');

const riskService = require('./risk-service/index2');
const handleRisk = riskService();

const handlers = compose([
  handleWebServerDuration,
  handleRisk,
  handleErrorReport,
  handleProcessInfo,
  handleAlertInfo
]);

const server = {
  async handle(msgs) {
    try {
      await bb.map(msgs, async(m) => {
        if (!m.type) {
          return;
        }

        await handlers(m);

      }, {concurrency: 2});

      logger.debug('[server] handle OK [%j]', msgs);
    } catch (e) {
      logger.error('[server] all handle ERROR [%s]', e);
    }
  }
};

module.exports = function(req, res) {
  let data = req.body || {};

  server.handle(data).catch(err => {
    logger.error(err);
  });
  res.status(204).json();
};