• Stars
    star
    501
  • Rank 87,977 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple cron system for Meteor. It supports syncronizing jobs between multiple processes.

littledata:synced-cron

A simple cron system for Meteor. It supports syncronizing jobs between multiple processes. In other words, if you add a job that runs every hour and your deployment consists of multiple app servers, only one of the app servers will execute the job each time (whichever tries first).

Migrated from percolate:synced-cron littledata:synced-cron

Since the original creator of the project could no longer maintain it, we had to migrate the package to another organisation to allow further maintenance and updates.

To migrate you can simply run

$ meteor remove percolate:synced-cron && meteor add littledata:synced-cron

Installation

$ meteor add littledata:synced-cron

API

Basics

To write a cron job, give it a unique name, a schedule and a function to run like below. SyncedCron uses the fantastic later.js library behind the scenes. A Later.js parse object is passed into the schedule call that gives you a huge amount of flexibility for scheduling your jobs, see the documentation.

SyncedCron.add({
  name: 'Crunch some important numbers for the marketing department',
  schedule: function(parser) {
    // parser is a later.parse object
    return parser.text('every 2 hours');
  },
  job: function() {
    var numbersCrunched = CrushSomeNumbers();
    return numbersCrunched;
  }
});

To start processing your jobs, somewhere in your project add:

SyncedCron.start();

Advanced

SyncedCron uses a collection called cronHistory to syncronize between processes. This also serves as a useful log of when jobs ran along with their output or error. A sample item looks like:

{ _id: 'wdYLPBZp5zzbwdfYj',
  intendedAt: Sun Apr 13 2014 17:34:00 GMT-0700 (MST),
  finishedAt: Sun Apr 13 2014 17:34:01 GMT-0700 (MST),
  name: 'Crunch some important numbers for the marketing department',
  startedAt: Sun Apr 13 2014 17:34:00 GMT-0700 (MST),
  result: '1982 numbers crunched'
}

Call SyncedCron.nextScheduledAtDate(jobName) to find the date that the job referenced by jobName will run next.

Call SyncedCron.remove(jobName) to remove and stop running the job referenced by jobName.

Call SyncedCron.stop() to remove and stop all jobs.

Call SyncedCron.pause() to stop all jobs without removing them. The existing jobs can be rescheduled (i.e. restarted) with SyncedCron.start().

To schedule a once off (i.e not recurring) event, create a job with a schedule like this parser.recur().on(date).fullDate();

Configuration

You can configure SyncedCron with the config method. Defaults are:

  SyncedCron.config({
    // Log job run details to console
    log: true,

    // Use a custom logger function (defaults to Meteor's logging package)
    logger: null,

    // Name of collection to use for synchronisation and logging
    collectionName: 'cronHistory',

    // Default to using localTime
    utc: false,

    /*
      TTL in seconds for history records in collection to expire
      NOTE: Unset to remove expiry but ensure you remove the index from
      mongo by hand

      ALSO: SyncedCron can't use the `_ensureIndex` command to modify
      the TTL index. The best way to modify the default value of
      `collectionTTL` is to remove the index by hand (in the mongo shell
      run `db.cronHistory.dropIndex({startedAt: 1})`) and re-run your
      project. SyncedCron will recreate the index with the updated TTL.
    */
    collectionTTL: 172800
  });

Logging

SyncedCron uses Meteor's logging package by default. If you want to use your own logger (for sending to other consumers or similar) you can do so by configuring the logger option.

SyncedCron expects a function as logger, and will pass arguments to it for you to take action on.

var MyLogger = function(opts) {
  console.log('Level', opts.level);
  console.log('Message', opts.message);
  console.log('Tag', opts.tag);
}

SyncedCron.config({
  logger: MyLogger
});

SyncedCron.add({ name: 'Test Job', ... });
SyncedCron.start();

The opts object passed to MyLogger above includes level, message, and tag.

  • level will be one of info, warn, error, debug.
  • message is something like Scheduled "Test Job" next run @Fri Mar 13 2015 10:15:00 GMT+0100 (CET).
  • tag will always be "SyncedCron" (handy for filtering).

Caveats

Beware, SyncedCron probably won't work as expected on certain shared hosting providers that shutdown app instances when they aren't receiving requests (like Heroku's free dyno tier or Meteor free galaxy).

Contributing

Write some code. Write some tests. To run the tests, do:

$ meteor test-packages ./

License

MIT. (c) Percolate Studio, originally designed and built by Zoltan Olah (@zol), now community maintained.

Synced Cron was developed as part of the Verso project.

More Repositories

1

percolatestudio.com

JavaScript
249
star
2

meteor-migrations

Simple migration system for Meteor
JavaScript
245
star
3

publish-counts

Meteor package to help you publish the count of a cursor, in real time
JavaScript
200
star
4

ground-control

A next generation blog, built in Meteor.
CSS
183
star
5

meteor-momentum

Reactive animation package for Meteorjs
JavaScript
167
star
6

meteor-famous-demos

Demos from our devshop talk on Meteor+Famous
JavaScript
92
star
7

react-leaderboard

Meteor's leaderboard example app, rewritten in React.
JavaScript
74
star
8

atmosphere

Atmosphere - Meteor Packages
59
star
9

meteor-server-info

Query your Meteor app for diagnostics
JavaScript
59
star
10

meteor-google-api

A simple API encapsulating some common patterns regarding Google's APIs
JavaScript
48
star
11

meteor-segment.io

Basic Segment.io loader snippet Meteor package
JavaScript
29
star
12

famous-blog-examples

The code for the examples on our blog post http://blog.percolatestudio.com/engineering/the-future-of-javascript-animation-with-famous/
JavaScript
29
star
13

momentum-iron-router

JavaScript
27
star
14

league

CSS
26
star
15

development-guide

A set of (best?) practices that we follow at Percolate Studio.
Shell
23
star
16

mdg-localmarket

Recipes example app
17
star
17

cordova-plugin-safe-reload

Cordova plugin to watch and recover after a broken Meteor Hot Code Push.
JavaScript
14
star
18

amble

A proof of concept Apple Watch and Meteor application.
JavaScript
14
star
19

meteor-url-shortener

URL Shortener for a Meteor app.
JavaScript
13
star
20

meteor-wireframing

Wireframing tools for Meteor
JavaScript
13
star
21

safe-reload

Percolate Studio's Safe Reload Package for Meteor / Cordova
JavaScript
11
star
22

percolate-icons

Percolate Studio Icon Set Package
Less
8
star
23

meteor-detective

Snoop into your Meteor app
JavaScript
8
star
24

meteor-velocityjs

Meteor wrapper package for velocity.js
JavaScript
7
star
25

meteor-pack

Random odds and ends we use across our Meteor projects.
JavaScript
7
star
26

cordova-plugin-file-upload

Cordova plugin for non-multipart file uploads.
JavaScript
6
star
27

meteor-mixpanel

Light Mixpanel Loader package for Meteor.
JavaScript
5
star
28

mdg-todos

CSS
3
star
29

safe

Make method calls safely
JavaScript
3
star
30

collection-models

Simple model code for Meteor
JavaScript
3
star
31

meteor-factory-client

JavaScript
2
star
32

contact-form

JavaScript
2
star
33

tooltips

Tooltips for Meteor
JavaScript
2
star
34

collection-models-schemas

JavaScript
2
star
35

amble-victoria

Ruby
1
star
36

mdg-dashboard

JavaScript
1
star
37

cordova-plugin-file-transfer

Non Multipart version of the Cordova FileTransfer plugin
JavaScript
1
star
38

MeteorShower

JavaScript
1
star
39

mdg-leaderboard

Leaderboard example app redesign
1
star
40

mdg-recipes-landing

Landing page for recipes app
CSS
1
star
41

bound-form

A Meteor package for forms bound to their output via a reactive dictionary.
JavaScript
1
star
42

theme-percolate-blog

Percolate Studio blog WP theme
CSS
1
star