UNPKG

openhim-core

Version:

The OpenHIM core application that provides logging and routing of http requests

104 lines (82 loc) 3.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.registerPollingChannel = registerPollingChannel; exports.removePollingChannel = removePollingChannel; exports.setupAgenda = setupAgenda; exports.agendaGlobal = void 0; var _request = _interopRequireDefault(require("request")); var _winston = _interopRequireDefault(require("winston")); var Channels = _interopRequireWildcard(require("./model/channels")); var _config = require("./config"); var utils = _interopRequireWildcard(require("./utils")); var _util = require("util"); function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const { ChannelModel } = Channels; _config.config.polling = _config.config.get('polling'); let agendaGlobal = null; exports.agendaGlobal = agendaGlobal; async function registerPollingChannel(channel, callback) { _winston.default.info(`Registering polling channel: ${channel._id}`); if (!channel.pollingSchedule) { return callback(new Error('no polling schedule set on this channel')); } try { await exports.agendaGlobal.cancel({ name: `polling-job-${channel._id}` }); exports.agendaGlobal.define(`polling-job-${channel._id}`, (job, done) => { _winston.default.info(`Polling channel ${channel._id}`); const options = { url: `http://${_config.config.polling.host}:${_config.config.polling.pollingPort}/trigger`, headers: { 'channel-id': channel._id, 'X-OpenHIM-LastRunAt': job.attrs.lastRunAt } }; return (0, _request.default)(options, () => done()); }); exports.agendaGlobal.every(channel.pollingSchedule, `polling-job-${channel._id}`, null, { timezone: utils.serverTimezone() }); return callback(null); } catch (err) { return callback(err); } } async function removePollingChannel(channel, callback) { _winston.default.info(`Removing polling schedule for channel: ${channel._id}`); try { await exports.agendaGlobal.cancel({ name: `polling-job-${channel._id}` }); return callback(null); } catch (err) { return callback(err); } } function setupAgenda(agenda, callback) { _winston.default.info('Starting polling server...'); const registerPollingChannelPromise = (0, _util.promisify)(registerPollingChannel); exports.agendaGlobal = agendaGlobal = agenda; return ChannelModel.find({ type: 'polling' }, (err, channels) => { if (err) { return err; } const promises = []; for (const channel of Array.from(channels)) { if (Channels.isChannelEnabled(channel)) { promises.push(registerPollingChannelPromise(channel)); } } return Promise.all(promises).then(callback); }); } //# sourceMappingURL=polling.js.map