Example #1
0
var addToSchedule = function(service, record) {
	if (record && record.schedule) {
		record.jobId = schedule.scheduleJob({
				start: Date.now() + record.delay,
				period: record.interval
			},
			doScheduleJob, {
				service: service,
				record: record
			});
	}
};
Example #2
0
/**
 * Schedule crons
 */
function _scheduleCrons(server, crons) {
  const handlers = server.cronHandlers;
  let i;

  for (i = 0; i < crons.length; i++) {
    const cronInfo = crons[i];
    const time = cronInfo.time;
    const action = cronInfo.action;
    const jobId = cronInfo.id;

    if (!time || !action || !jobId) {
      logger.error('cron miss necessary parameters: %j', cronInfo);
      continue;
    }

    if (action.indexOf('.') === -1) {
      logger.error('cron action is error format: %j', cronInfo);
      continue;
    }

    const cron = action.split('.')[0];
    const job = action.split('.')[1];
    const handler = handlers[cron];

    if (!handler) {
      logger.error('could not find cron: %j', cronInfo);
      continue;
    }

    if (typeof handler[job] !== 'function') {
      logger.error('could not find cron job: %j, %s', cronInfo, job);
      continue;
    }

    const id = schedule.scheduleJob(time, handler[job].bind(handler));
    server.jobs[jobId] = id;
  }
}