A scheduled task runner for Node.js built on top of schyntax.
npm install schtick
Run a task every minute:
var Schtick = ;var schtick = ; // best practice is to keep a singleton Schtick instanceschtick;
schtick ( name, schedule [, options], callback )
The constructor can be called with or without the
new operator. It returns a ScheduledTask object.
nameA unique name for the task. If you don't care about human-readable task names, you can pass in
nulland a UUID will be assigned as the name.
scheduleEither a schyntax format string, or an
schyntaxobject. See the schyntax library for documentation.
optionsAn optional object with some or all of the following properties:
true) If true,
startSchedule()is automatically called, otherwise the task won't be run until start is explicitly called.
new Date()) The last event time (see the second argument to the callback) when the task is known to have run. Used for Task Windows.
0) The period of time (in milliseconds) after an event should have run where it would still be appropriate to run it. See Task Windows for more details.
callbackThe function which will be called for each event in the schedule. The function is passed two arguments:
taskThe ScheduledTask object.
eventTimeThe time the callback was intended to be called based on defined schedule. Note: this may not be the actual time the callback was called.
addTask() except that the callback function will receive a third argument. This is the
done argument which is a function. It should be called after an asynchronous task has been completed. Events in the schedule will be skipped until
done is called.
For example, here's a schedule which is defined to run every second (
sec(*)), but the callback waits 1.5 seconds before calling
done. Therefore, every other event will be skipped, and we'll only write to the console once every two seconds.
You can also pass an error to
done which will get propagated to the Schtick error handlers.
addErrorHandler ( callback )
Adds a callback for when scheduled tasks produce an error.
callbackA function which accepts two arguments:
ScheduledTaskwhich generated the error.
errorThe error itself.
shutdown ( )
.stopSchedule() on all tasks. Tasks cannot be added, removed, or restarted after shutdown has been called.
startSchedule ( [lastKnownEvent] )
Starts the task runner (if it is not already running).
lastKnownEvent(Date) Serves the same purpose as
options.lastKnownEventdescribed in the
stopSchedule ( )
Stops the task runner and clears any existing timeout.
updateSchedule ( schedule )
Updates the schyntax schedule being used by the task runner.
schedule(Schedule|string) Either a string or a Schyntax Schedule object.
unref() on the underlying timer, allowing the program to exit if this task is the only thing in the event loop. Please see node's
timers#unref for details.
If you had previously called
schtick#unref, you can call
schtick#ref to request the instance hold the program open. If the instance is already
refd then calling
schtick#ref again will have no effect.
Task Windows are useful for compensating with interruptions. Imagine you have a task set to run every day at noon, but a deployment or restart causes schtick to be offline from 11:59 to 12:01. You can use a task window to say "if we're offline at noon, but come back before 1pm, run the task immediately after coming online."
In order for this to work, you need to provide schtick with the last
eventTime when a task ran. You have to keep track of this yourself (a redis/memcache key would be one way).
var options =lastKnownEvent: eventTimewindow: 60 * 60 * 1000 // one hour window;schtick;