How to use the cron.CronJob 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 zeit / pkg / test / test-79-npm / cron / cron.js View on Github external
'use strict';

var CronJob = require('cron').CronJob;

var counter = 0;

new CronJob('* * * * * *', function () { // eslint-disable-line no-new
  counter += 1;
}, null, true, 'America/Los_Angeles');

setTimeout(function () {
  if (counter === 4) console.log('ok');
  process.exit();
}, 4500);
github kalisio / krawler / src / cli.js View on Github external
.catch(error => {
        console.error(error.message)
        Healthcheck.isRunning = false
        Healthcheck.error = error
        // When not running job continuously stop the server
        if (!options.cron) {
          server.close()
        }
      })
  }

  // Setup CRON job if required
  let cronJob
  if (options.cron) {
    console.log('Scheduling job with cron pattern ' + options.cron)
    cronJob = new CronJob(options.cron, () => {
      // If last job has not yet finished skip this call as we are late
      if (!Healthcheck.isRunning) {
        Healthcheck.nbSkippedJobs = 0
        runJobWithOptions()
      } else {
        console.log('Skipping scheduled job as previous one is not yet finished')
        Healthcheck.nbSkippedJobs++
      }
    })
    // In case the server is forced to exit stop the job as well
    server.on('close', () => {
      cronJob.stop()
      console.log('Stopped scheduled job with cron pattern')
    })
  }
  // Run job
github bh-lay / blog / sys / cronJob.js View on Github external
// ๆฏๆ™šไธ‰็‚น
	new CronJob('01 01 03 * * *', function() {
		// ๆ›ดๆ–ฐๅฎž้ชŒๅฎค้‡Œ็š„Githubๆ•ฐๆฎ
		updateLabsDataFromGithub.all()
		// ๆ›ดๆ–ฐไธชไบบGithubไฟกๆฏ
		myGithubData.update()
		// ๆ›ดๆ–ฐๅ‰งไธญไบบ็š„ๆœ‹ๅ‹ๅœˆๆ•ฐๆฎ
		updateMoment.sync()
		// ๆ›ดๆ–ฐ720ไบ‘ๆ•ฐๆฎ
		my720Data.update()
		// ๆ›ดๆ–ฐๅ›พ่™ซๆ•ฐๆฎ
		myTuchongData.update()
	}, null, true, 'Asia/Hong_Kong')

	// ๆฏๆ™šไธ‰็‚น้›ถๅๅˆ†
	new CronJob('01 10 03 * * *', function() {
		// ๆธ…้™คๅ…จ้ƒจ็ผ“ๅญ˜
		app.cache.clear()
	}, null, true, 'Asia/Hong_Kong')
}
github adarqui / test-x / OTP.js View on Github external
OTP.timer = function() {
		OTP.state.cron = new cron('0 * * * * *', function() {
			OTP.printPin();
		}, null, true, "America/New_York");
	}
github fosslife / delta / core / cron.js View on Github external
file,
        size: parseInt(stat.size / 1000.0),
        date: stat.mtime,
        retention: parseInt(retention)
    };
};

const getRetentionPeriod = stat => {
    return (
        MIN_AGE +
        (-MAX_AGE + MIN_AGE) *
            Math.pow(parseInt(stat.size / 1000.0) / MAX_SIZE - 1, 3)
    );
};

const job = new CronJob(
    cron.schedule,
    async () => {
        logger.info('Running Cron job for deleting files');
        const files = await readDirAsync(uploadsPath());
        for (const file of files) {
            const fileStats = getFileStats(file);
            const diff = differenceInDays(new Date(), fileStats.date);
            if (diff > fileStats.retention) {
                await deleteAsync(uploadsPath(fileStats.file));
                await db.sadd(`deleted`, fileStats.file);
                const shortName = fileStats.file.split('.')[0];
                await db.del(`short:${shortName}`);
                logger.info('Successsfully deleted the file ' + fileStats.file);
            }
        }
    },
github Zooz / predator / src / jobs / models / jobManager.js View on Github external
function addCron(jobId, job, cronExpression, configData) {
    const jobConnector = require(`./${configData.job_platform.toLowerCase()}/jobConnector`);
    let scheduledJob = new CronJob(cronExpression, async function () {
        try {
            if (job.enabled === false) {
                logger.info(`Skipping job with id: ${jobId} as it's currently disabled`);
            } else {
                let latestDockerImage = await dockerHubConnector.getMostRecentRunnerTag();
                let runId = Date.now();
                let jobSpecificPlatformConfig = createJobRequest(jobId, runId, job, latestDockerImage, configData);
                await jobConnector.runJob(jobSpecificPlatformConfig);
            }
        } catch (error) {
            logger.error({ id: jobId, error: error }, 'Unable to run scheduled job.');
        }
    }, function () {
        logger.info('Job: ' + jobId + ' completed.');
    }, true);
    cronJobs[jobId] = scheduledJob;
github metaspace2020 / metaspace / metaspace / graphql / server.js View on Github external
await Promise.all(
        projects.map(async project => {
          project.members.map(member => {
            sendPublishProjectNotificationEmail(member.user.email, project);
          });
          await entityManager.update(ProjectModel, project.id,
            { publishNotificationsSent: project.publishNotificationsSent + 1 });
        })
      );
    }
    catch (error) {
      logger.error(error);
    }
  };

  new CronJob('00 00 14 * * 1-5', emailNotificationsHandler, null, true);
  logger.info('Cron job started');
};
github naimo84 / node-red-contrib-ical-events / dist / ical-sensor.js View on Github external
case 'seconds':
                        cron = "*/" + config.timeout + " * * * * *";
                        break;
                    case 'minutes':
                        cron = "0 */" + config.timeout + " * * * *";
                        break;
                    case 'hours':
                        cron = "0 0 */" + config.timeout + " * * *";
                        break;
                    case 'days':
                        cron = "0 0 0 */" + config.timeout + " * *";
                        break;
                    default:
                        break;
                }
                node.job = new cron_1.CronJob(cron, cronCheckJob.bind(null, node, config));
                node.job.start();
                node.on('close', function () {
                    node.job.stop();
                });
            }
            cronCheckJob(this, config);
        }
        catch (err) {
            node.error('Error: ' + err.message);
            node.status({ fill: "red", shape: "ring", text: err.message });
        }
    }
    function cronCheckJob(node, config) {
github naimo84 / node-red-contrib-ical-events / src / ical-sensor.ts View on Github external
case 'seconds':
                        cron = `*/${config.timeout} * * * * *`;
                        break;
                    case 'minutes':
                        cron = `0 */${config.timeout} * * * *`;
                        break;
                    case 'hours':
                        cron = `0 0 */${config.timeout} * * *`;
                        break;
                    case 'days':
                        cron = `0 0 0 */${config.timeout} * *`;
                        break;
                    default:
                        break;
                }
                node.job = new CronJob(cron, cronCheckJob.bind(null, node, config));
                node.job.start();

                node.on('close', () => {
                    node.job.stop();
                });
            }

            cronCheckJob(this, config);
        }
        catch (err) {
            node.error('Error: ' + err.message);
            node.status({ fill: "red", shape: "ring", text: err.message })
        }
    }

cron

Cron jobs for your node

MIT
Latest version published 9 days ago

Package Health Score

97 / 100
Full package analysis