chatra:synced-cron
This package is a fork of littledata:synced-cron, maintained under the name chatra:synced-cron
.
Define and run scheduled jobs across multiple servers in a synchronized manner using Meteor and MongoDB.
Table of Contents
Introduction
The chatra:synced-cron
package allows you to schedule and run cron-like jobs in a Meteor application, synchronized across multiple server instances. It uses MongoDB to store job history and ensures that jobs are not executed multiple times across different servers.
Installation
Install the package using Meteor's package manager:
meteor add chatra:synced-cron
Usage
Adding Jobs
Define a job by providing a unique name, a schedule, and a job function:
1import { SyncedCron } from 'meteor/chatra:synced-cron'; 2 3SyncedCron.add({ 4 name: 'My Job', 5 schedule(parser) { 6 // Parser is Later.js parser 7 return parser.cron('0 * * * *'); // Runs every hour at minute 0 8 }, 9 job(intendedAt) { 10 // Your job logic here 11 console.log('Running my job at', intendedAt); 12 // Return a result if needed 13 return 'Job completed'; 14 }, 15});
- Parameters:
name
(string): Unique name for the job.schedule
(function): Function that returns a Later.js schedule.job
(function): The function to execute at scheduled times.persist
(boolean, optional): Whether to persist job history. Defaults totrue
.
Starting the Scheduler
Start processing the added jobs:
1SyncedCron.start();
This should typically be called on the server during startup.
Pausing and Stopping
-
Pause: Temporarily stop processing jobs without removing them.
1SyncedCron.pause();
-
Stop: Stop processing and remove all jobs.
1SyncedCron.stop();
Removing Jobs
Remove a specific job by its name:
1SyncedCron.remove('My Job');
Configuration
Configure SyncedCron
by setting options:
1SyncedCron.config({ 2 log: true, // Enable or disable logging 3 logger: null, // Provide a custom logger function 4 collectionName: 'cronHistory', // Name of the MongoDB collection 5 utc: false, // Use UTC time or local time 6 collectionTTL: 172800, // Time in seconds for history records to expire (e.g., 48 hours) 7});
- Options:
log
(boolean): Enable or disable logging. Defaults totrue
.logger
(function): Custom logger function. Defaults toconsole
.collectionName
(string): Name of the MongoDB collection for job history.utc
(boolean): Use UTC time for scheduling. Defaults tofalse
(local time).collectionTTL
(number): Time-to-live in seconds for job history records. Set tonull
to disable expiration.
Examples
Scheduling a Job Every Day at Midnight
1SyncedCron.add({ 2 name: 'Midnight Job', 3 schedule(parser) { 4 return parser.cron('0 0 * * *'); // Every day at midnight 5 }, 6 job() { 7 // Job logic 8 console.log('Running midnight job'); 9 }, 10});
Using a Custom Logger
1SyncedCron.config({ 2 logger(opts) { 3 // opts: { level, message, tag } 4 console.log(`[${opts.level}] ${opts.tag}: ${opts.message}`); 5 }, 6});
Disabling Job Persistence
1SyncedCron.add({ 2 name: 'Transient Job', 3 persist: false, // Do not store job history 4 schedule(parser) { 5 return parser.cron('*/5 * * * *'); // Every 5 minutes 6 }, 7 job() { 8 console.log('Running transient job'); 9 }, 10});
Logging
By default, SyncedCron
logs job start, completion, and errors to the console. You can customize logging behavior:
-
Disable Logging:
1SyncedCron.config({ 2 log: false, 3});
-
Custom Logger Function:
Provide a function to handle log messages:
1SyncedCron.config({ 2 logger({ level, message, tag }) { 3 // Handle log messages 4 myCustomLogger.log(level, `${tag}: ${message}`); 5 }, 6});
- Parameters:
level
(string): Log level (info
,warn
,error
,debug
).message
(string): Log message.tag
(string): Tag identifying the source ('SyncedCron'
).
- Parameters:
Tests
The package includes a comprehensive test suite. To run the tests:
meteor test-packages ./
License
This package is licensed under the MIT License