How to use the cron.CronTime function in cron

To help you get started, weโ€™ve selected a few cron examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github agenda / agenda / lib / job.js View on Github external
const computeFromInterval = () => {
    debug('[%s:%s] computing next run via interval [%s]', this.attrs.name, this.attrs._id, interval);
    let lastRun = this.attrs.lastRunAt || new Date();
    lastRun = dateForTimezone(lastRun);
    try {
      const cronTime = new CronTime(interval);
      let nextDate = cronTime._getNextDateFrom(lastRun);
      if (nextDate.valueOf() === lastRun.valueOf()) {
        // Handle cronTime giving back the same date for the next run time
        nextDate = cronTime._getNextDateFrom(dateForTimezone(new Date(lastRun.valueOf() + 1000)));
      }
      this.attrs.nextRunAt = nextDate;
      debug('[%s:%s] nextRunAt set to [%s]', this.attrs.name, this.attrs._id, this.attrs.nextRunAt.toISOString());
    } catch (e) {
      // Nope, humanInterval then!
      try {
        if (!this.attrs.lastRunAt && humanInterval(interval)) {
          this.attrs.nextRunAt = lastRun.valueOf();
          debug('[%s:%s] nextRunAt set to [%s]', this.attrs.name, this.attrs._id, this.attrs.nextRunAt.toISOString());
        } else {
          this.attrs.nextRunAt = lastRun.valueOf() + humanInterval(interval);
          debug('[%s:%s] nextRunAt set to [%s]', this.attrs.name, this.attrs._id, this.attrs.nextRunAt.toISOString());
github agenda / agenda / lib / job / compute-next-run-at.js View on Github external
const computeFromInterval = () => {
    debug('[%s:%s] computing next run via interval [%s]', this.attrs.name, this.attrs._id, interval);
    let lastRun = this.attrs.lastRunAt || new Date();
    lastRun = dateForTimezone(lastRun);
    try {
      const cronTime = new CronTime(interval);
      let nextDate = cronTime._getNextDateFrom(lastRun);
      if (nextDate.valueOf() === lastRun.valueOf() || nextDate.valueOf() <= previousNextRunAt.valueOf()) {
        // Handle cronTime giving back the same date for the next run time
        nextDate = cronTime._getNextDateFrom(dateForTimezone(new Date(lastRun.valueOf() + 1000)));
      }

      this.attrs.nextRunAt = nextDate;
      debug('[%s:%s] nextRunAt set to [%s]', this.attrs.name, this.attrs._id, new Date(this.attrs.nextRunAt).toISOString());
    // Either `xo` linter or Node.js 8 stumble on this line if it isn't just ignored
    } catch (error) { // eslint-disable-line no-unused-vars
      // Nope, humanInterval then!
      try {
        if (!this.attrs.lastRunAt && humanInterval(interval)) {
          this.attrs.nextRunAt = lastRun.valueOf();
          debug('[%s:%s] nextRunAt set to [%s]', this.attrs.name, this.attrs._id, new Date(this.attrs.nextRunAt).toISOString());
        } else {
github stelace / stelace / src / util / time.js View on Github external
function computeRecurringDates (pattern, { startDate, endDate, timezone = 'UTC' } = {}) {
  if (_.isNil(timezone)) timezone = 'UTC'

  if (!isDateString(startDate) || !isDateString(endDate)) {
    throw new Error('Expected start and end dates')
  }
  if (endDate < startDate) {
    throw new Error('Invalid dates')
  }

  const cronTime = new CronTime(pattern, timezone)

  let continueLoop = true
  const dates = []
  let cronISODate = new Date(new Date(startDate).getTime() - 1) // start from `startDate` minus 1 millisecond

  while (continueLoop) {
    // `_getNextDateFrom` is a private method from `CronTime.prototype`
    // Please check its availability when upgrading the library `cron`
    const cronMomentDate = cronTime._getNextDateFrom(cronISODate, timezone)

    cronISODate = cronMomentDate.toISOString()
    continueLoop = cronISODate < endDate

    if (continueLoop) {
      dates.push(cronISODate)
    }
github stelace / stelace / test / unit / util / time.spec.js View on Github external
test('checks the existence of the private function _getNextDateFrom from the library cron', (t) => {
  const cronTime = new CronTime('* * * * *', 'UTC')
  t.is(typeof cronTime._getNextDateFrom, 'function')
})
github jamieweavis / streaker / src / app.js View on Github external
function activateNotifications(event, isEnabled) {
    store.set('notification.isEnabled', isEnabled);
    if (isEnabled) {
      const time = store.get('notification.time');
      const timeArray = time.split(':');
      const cronTime = `0 ${timeArray[1]} ${timeArray[0]} * * *`;
      job.setTime(new CronTime(cronTime));
      job.start();
    } else {
      job.stop();
    }
    event.sender.send('activateNotificationsSet');
  }
github naimo84 / node-red-contrib-ical-events / dist / ical-events.js View on Github external
countdown: helper_1.countdown(new Date(ev.start))
                                };
                                if (config.offset) {
                                    eventStart.setMinutes(eventEnd.getMinutes() + parseInt(config.offset));
                                }
                                else {
                                    eventStart.setMinutes(eventEnd.getMinutes() - 1);
                                }
                                var job2 = new cron_1.CronJob(eventEnd, cronJobEnd.bind(null, event_2, node));
                                var startedCronJobs_2 = node.context().get('startedCronJobs') || {};
                                if (!newCronJobs.has(uid) && !startedCronJobs_2[uid]) {
                                    newCronJobs.set(uid, job2);
                                    node.debug("new - " + uid);
                                }
                                else if (startedCronJobs_2[uid]) {
                                    startedCronJobs_2[uid].setTime(new cron_2.CronTime(eventEnd));
                                    startedCronJobs_2[uid].start();
                                    node.context().set('startedCronJobs', startedCronJobs_2);
                                    node.debug("started - " + uid);
                                }
                            }
                        }
                    }
                }
                if (newCronJobs) {
                    newCronJobs.forEach(function (job, key) {
                        try {
                            job.start();
                            node.debug("starting - " + key);
                            var startedCronJobs = node.context().get('startedCronJobs') || {};
                            startedCronJobs[key] = job;
                            node.context().set('startedCronJobs', startedCronJobs);
github jsreport / jsreport / extension / scheduling / lib / scheduling.js View on Github external
Scheduling.prototype._beforeUpdateHandler = function (query, update) {
    var entity = update.$set;

    if (entity.name)
        entity.modificationDate = new Date();

    if (entity.cron) {
        entity.modificationDate = new Date();
        var cron = new CronTime(entity.cron);
        entity.nextRun = cron._getNextDateFrom(new Date()).toDate();
        entity.state = "planned";
    }
};
github lykmapipo / kue-scheduler / index.js View on Github external
cron: function (after) {
      try {
        //compute next date from the cron interval
        var cronTime = new CronTime(interval, timezone);
        var nextRun = cronTime.sendAt();

        //return computed time
        after(null, nextRun.toDate());

      } catch (ex) {
        //to allow parallel run with other interval parser
        after(null, null);
      }
    },
github naimo84 / node-red-contrib-ical-events / dist / ical-events.js View on Github external
countdown: helper_1.countdown(new Date(ev.start))
                                };
                                if (config.offset) {
                                    eventStart.setMinutes(eventStart.getMinutes() + parseInt(config.offset));
                                }
                                else {
                                    eventStart.setMinutes(eventStart.getMinutes() - 1);
                                }
                                var job2 = new cron_1.CronJob(eventStart, cronJobStart.bind(null, event_1, node));
                                var startedCronJobs_1 = node.context().get('startedCronJobs') || {};
                                if (!newCronJobs.has(uid) && !startedCronJobs_1[uid]) {
                                    newCronJobs.set(uid, job2);
                                    node.debug("new - " + uid);
                                }
                                else if (startedCronJobs_1[uid]) {
                                    startedCronJobs_1[uid].setTime(new cron_2.CronTime(eventStart));
                                    startedCronJobs_1[uid].start();
                                    node.context().set('startedCronJobs', startedCronJobs_1);
                                    node.debug("started - " + uid);
                                }
                            }
                            if (eventEnd > dateNow) {
                                var uid = crypto.MD5(ev.created + ev.summary + "end").toString();
                                if (ev.uid) {
                                    uid = ev.uid + "end";
                                }
                                possibleUids.push(uid);
                                var event_2 = {
                                    summary: ev.summary,
                                    topic: ev.summary,
                                    id: uid,
                                    location: ev.location,

cron

Cron jobs for your node

MIT
Latest version published 6 days ago

Package Health Score

95 / 100
Full package analysis