Installation

Example Usage

varagenda=newAgenda({db:{address:'localhost:27017/agenda-example'}});agenda.define('delete old users',function(job,done){User.remove({lastLogIn:{$lt:twoDaysAgo}},done);});agenda.every('3 minutes','delete old users');// Alternatively, you could also do:agenda.every('*/3 * * * *','delete old users');agenda.start();

processEvery(interval)

Takes a string interval which can be either a traditional javascript number,
or a string such as 3 minutes

Specifies the frequency at which agenda will query the database looking for jobs
that need to be processed. Agenda internally uses setTimeout to guarantee that
jobs run at (close to ~3ms) the right time.

Decreasing the frequency will result in fewer database queries, but more jobs
being stored in memory.

Also worth noting is that if the job is queue is shutdown, any jobs stored in memory
that haven't run will still be locked, meaning that you may have to wait for the
lock to expire.

Defining Job Processors

Before you can use a job, you must define its processing behavior.

define(jobName, [options], fn)

Defines a job with the name of jobName. When a job of job name gets run, it
will be passed to fn(job, done). To maintain asynchronous behavior, you must
call done() when you are processing the job. If your function is synchronous,
you may omit done from the signature.

options is an optional argument which can overwrite the defaults. It can take
the following:

concurrency: number maxinum number of that job that can be running at once (per instance of agenda)

lockLifetime: number interval in ms of how long the job stays locked for (see multiple job processors for more info).
A job will automatically unlock if done() is called.

priority: (lowest|low|normal|high|highest|number) specifies the priority
of the job. Higher priority jobs will run first. See the priority mapping
below

Creating Jobs

every(interval, name, [data])

Runs job name at the given interval. Optionally, data can be passed in.
Every creates a job of type single, which means that it will only create one
job in the database, even if that line is run multiple times. This lets you put
it in a file that may get run multiple times, such as webserver.js which may
reboot from time to time.

interval can be a human-readable format String, a cron format String, or a Number.

data is an optional argument that will be passed to the processing function
under job.attrs.data.

cancel(mongoskin query, cb)

This functionality can also be achieved by first retrieving all the jobs from the database using agenda.jobs(), looping through the resulting array and calling job.remove() on each. It is however preferable to use agenda.cancel() for this use case, as this ensures the operation is atomic.

purge(cb)

Removes all jobs in the database without defined behaviors. Useful if you change a definition name and want to remove old jobs.

IMPORTANT: Do not run this before you finish defining all of your jobs. If you do, you will nuke your database of jobs.

Starting the job processor

To get agenda to start processing jobs from the database you must start it. This
will schedule an interval (based on processEvery) to check for new jobs and
run them. You can also stop the queue.

start

Starts the job queue processing, checking processEvery time to see if there
are new jobs.

stop

Stops the job queue processing. Unlocks currently running jobs.

This can be very useful for graceful shutdowns so that currently running/grabbed jobs are abandoned so that other
job queues can grab them / they are unlocked should the job queue start again. Here is an example of how to do a graceful
shutdown.

This will ensure that no other job processor (this one included) attempts to run the job again
for the next 10 seconds. If you have a particularly long running job, you will want to
specify a longer lockLifetime.

By default it is 10 minutes. Typically you shouldn't have a job that runs for 10 minutes,
so this is really insurance should the job queue crash before the job is unlocked.

When a job is finished (ie. done is called), it will automatically unlock.

Manually working with a job

A job instance has many instance methods. All mutating methods must be followed
with a call to job.save() in order to persist the changes to the database.

repeatEvery(interval)

Specifies an interval on which the job should repeat.

interval can be a human-readable format String, a cron format String, or a Number.

Frequently Asked Questions

Sample Project Structure?

Agenda doesn't have a preferred project structure and leaves it to the user to
choose how they would like to use it. That being said, you can check out the
example project structure below.

Can I Donate?

Thanks! I'm flattered, but it's really not necessary. If you really want to, you can find my gittip here.

Web Interface?

Agenda itself does not have a web interface built in. That being said, there is a stand-alone web interface in the form of agenda-ui.

Screenshot:

Mongo vs Redis

The decision to use Mongo instead of Redis is intentional. Redis is often used for
non-essential data (such as sessions) and without configuration doesn't
guarantee the same level of persistence as Mongo (should the server need to be
restarted/crash).

Agenda decides to focus on persistence without requiring special configuration
of Redis (thereby degrading the performance of the Redis server on non-critical
data, such as sessions).

Ultimately if enough people want a Redis driver instead of Mongo, I will write
one. (Please open an issue requesting it). For now, Agenda decided to focus on
guaranteed persistence.

Spawning / forking processes.

Ultimately Agenda can work from a single job queue across multiple machines, node processes, or forks. If you are interested in having more than one worker, Bars3s has written up a fantastic example of how one might do it:

License

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the 'Software'), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Resolving all dependencies.. This could take a while as our cache is not yet
fully warmed up.