UNPKG

openhim-core

Version:

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

91 lines (76 loc) 2.54 kB
var Channel, Channels, Q, authorisation, config, logger, removePollingChannel, request, utils; Channels = require('./model/channels'); Channel = Channels.Channel; request = require('request'); config = require('./config/config'); config.polling = config.get('polling'); logger = require('winston'); Q = require('q'); authorisation = require('./middleware/authorisation'); utils = require('./utils'); exports.agendaGlobal = null; exports.registerPollingChannel = function(channel, callback) { logger.info("Registering polling channel: " + channel._id); if (!channel.pollingSchedule) { return callback(new Error('no polling schedule set on this channel')); } return exports.agendaGlobal.cancel({ name: "polling-job-" + channel._id }, function(err) { if (err) { return callback(err); } exports.agendaGlobal.define("polling-job-" + channel._id, function(job, done) { var options; logger.info("Polling channel " + channel._id); options = { url: "http://" + config.polling.host + ":" + config.polling.pollingPort + "/trigger", headers: { 'channel-id': channel._id, 'X-OpenHIM-LastRunAt': job.attrs.lastRunAt } }; return request(options, function() { return done(); }); }); exports.agendaGlobal.every(channel.pollingSchedule, "polling-job-" + channel._id, null, { timezone: utils.serverTimezone() }); return callback(null); }); }; exports.removePollingChannel = removePollingChannel = function(channel, callback) { logger.info("Removing polling schedule for channel: " + channel._id); return exports.agendaGlobal.cancel({ name: "polling-job-" + channel._id }, function(err) { if (err) { return callback(err); } return callback(null); }); }; exports.setupAgenda = function(agenda, callback) { var registerPollingChannelPromise; logger.info("Starting polling server..."); registerPollingChannelPromise = Q.denodeify(exports.registerPollingChannel); exports.agendaGlobal = agenda; return Channel.find({ type: 'polling' }, function(err, channels) { var channel, i, len, promises; if (err) { return err; } promises = []; for (i = 0, len = channels.length; i < len; i++) { channel = channels[i]; if (Channels.isChannelEnabled(channel)) { promises.push(registerPollingChannelPromise(channel)); } } return (Q.all(promises)).done(callback); }); }; //# sourceMappingURL=polling.js.map