rschmukler/agenda

View on GitHub
docs/agenda/2.0.0/index.html

Summary

Maintainability
Test Coverage
<!DOCTYPE html>
<html lang="en">
<head>

  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>
      Home - Documentation
  </title>

  <link href="https://www.braintreepayments.com/images/favicon-ccda0b14.png" rel="icon" type="image/png">

  <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.2/highlight.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

  <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">

  

  <!-- start Mixpanel -->
  <script type="text/javascript">(function(e,a){if(!a.__SV){var b=window;try{var c,l,i,j=b.location,g=j.hash;c=function(a,b){return(l=a.match(RegExp(b+"=([^&]*)")))?l[1]:null};g&&c(g,"state")&&(i=JSON.parse(decodeURIComponent(c(g,"state"))),"mpeditor"===i.action&&(b.sessionStorage.setItem("_mpcehash",g),history.replaceState(i.desiredHash||"",e.title,j.pathname+j.search)))}catch(m){}var k,h;window.mixpanel=a;a._i=[];a.init=function(b,c,f){function e(b,a){var c=a.split(".");2==c.length&&(b=b[c[0]],a=c[1]);b[a]=function(){b.push([a].concat(Array.prototype.slice.call(arguments,
  0)))}}var d=a;"undefined"!==typeof f?d=a[f]=[]:f="mixpanel";d.people=d.people||[];d.toString=function(b){var a="mixpanel";"mixpanel"!==f&&(a+="."+f);b||(a+=" (stub)");return a};d.people.toString=function(){return d.toString(1)+".people (stub)"};k="disable time_event track track_pageview track_links track_forms register register_once alias unregister identify name_tag set_config reset people.set people.set_once people.increment people.append people.union people.track_charge people.clear_charges people.delete_user".split(" ");
  for(h=0;h<k.length;h++)e(d,k[h]);a._i.push([b,c,f])};a.__SV=1.2;b=e.createElement("script");b.type="text/javascript";b.async=!0;b.src="undefined"!==typeof MIXPANEL_CUSTOM_LIB_URL?MIXPANEL_CUSTOM_LIB_URL:"file:"===e.location.protocol&&"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js".match(/^\/\//)?"https://cdn.mxpnl.com/libs/mixpanel-2-latest.min.js":"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js";c=e.getElementsByTagName("script")[0];c.parentNode.insertBefore(b,c)}})(document,window.mixpanel||[]);
  mixpanel.init("1919205b2da72e4da3b9b6639b444d59");</script>
  <!-- end Mixpanel -->
</head>

<body>
  <svg style="display: none;">
    <defs>
      <symbol id="linkIcon" fill="#706d77" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
          <path d="M0 0h24v24H0z" fill="none"/>
          <path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71 0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71 0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76 0 5-2.24 5-5s-2.24-5-5-5z"/>
      </symbol>
    </defs>
  </svg>

  <input type="checkbox" id="nav-trigger" class="nav-trigger" />
  <label for="nav-trigger" class="navicon-button x">
    <div class="navicon"></div>
  </label>

  <label for="nav-trigger" class="overlay"></label>

  <div class="top-nav-wrapper">
    <ul>
      <li  class="active" >
        <a href="index.html">
          
          
            <svg fill="#0095dd" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
              <path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/>
              <path d="M0 0h24v24H0z" fill="none"/>
            </svg>
          
        </a>
      </li>

      

    </ul>
  </div>

  <nav>
    <h3 class="reference-title">
      Agenda
    </h3>

    <h3>Classes</h3><ul><li id="Agenda-nav"><a href="Agenda.html">Agenda</a><ul class='methods'><li data-type="method" id="Agenda-cancel-nav"><a href="Agenda.html#cancel">cancel</a></li><li data-type="method" id="Agenda-create-nav"><a href="Agenda.html#create">create</a></li><li data-type="method" id="Agenda-database-nav"><a href="Agenda.html#database">database</a></li><li data-type="method" id="Agenda-dbInit-nav"><a href="Agenda.html#dbInit">dbInit</a></li><li data-type="method" id="Agenda-defaultConcurrency-nav"><a href="Agenda.html#defaultConcurrency">defaultConcurrency</a></li><li data-type="method" id="Agenda-defaultLockLifetime-nav"><a href="Agenda.html#defaultLockLifetime">defaultLockLifetime</a></li><li data-type="method" id="Agenda-defaultLockLimit-nav"><a href="Agenda.html#defaultLockLimit">defaultLockLimit</a></li><li data-type="method" id="Agenda-define-nav"><a href="Agenda.html#define">define</a></li><li data-type="method" id="Agenda-every-nav"><a href="Agenda.html#every">every</a></li><li data-type="method" id="Agenda-findAndLockNextJob-nav"><a href="Agenda.html#findAndLockNextJob">findAndLockNextJob</a></li><li data-type="method" id="Agenda-jobs-nav"><a href="Agenda.html#jobs">jobs</a></li><li data-type="method" id="Agenda-locklimit-nav"><a href="Agenda.html#locklimit">locklimit</a></li><li data-type="method" id="Agenda-maxConcurrency-nav"><a href="Agenda.html#maxConcurrency">maxConcurrency</a></li><li data-type="method" id="Agenda-mongo-nav"><a href="Agenda.html#mongo">mongo</a></li><li data-type="method" id="Agenda-name-nav"><a href="Agenda.html#name">name</a></li><li data-type="method" id="Agenda-now-nav"><a href="Agenda.html#now">now</a></li><li data-type="method" id="Agenda-processEvery-nav"><a href="Agenda.html#processEvery">processEvery</a></li><li data-type="method" id="Agenda-purge-nav"><a href="Agenda.html#purge">purge</a></li><li data-type="method" id="Agenda-saveJob-nav"><a href="Agenda.html#saveJob">saveJob</a></li><li data-type="method" id="Agenda-schedule-nav"><a href="Agenda.html#schedule">schedule</a></li><li data-type="method" id="Agenda-sort-nav"><a href="Agenda.html#sort">sort</a></li><li data-type="method" id="Agenda-start-nav"><a href="Agenda.html#start">start</a></li><li data-type="method" id="Agenda-stop-nav"><a href="Agenda.html#stop">stop</a></li></ul></li><li id="Job-nav"><a href="Job.html">Job</a><ul class='methods'><li data-type="method" id="Job-computeNextRunAt-nav"><a href="Job.html#computeNextRunAt">computeNextRunAt</a></li><li data-type="method" id="Job-disable-nav"><a href="Job.html#disable">disable</a></li><li data-type="method" id="Job-enable-nav"><a href="Job.html#enable">enable</a></li><li data-type="method" id="Job-fail-nav"><a href="Job.html#fail">fail</a></li><li data-type="method" id="Job-isRunning-nav"><a href="Job.html#isRunning">isRunning</a></li><li data-type="method" id="Job-priority-nav"><a href="Job.html#priority">priority</a></li><li data-type="method" id="Job-remove-nav"><a href="Job.html#remove">remove</a></li><li data-type="method" id="Job-repeatAt-nav"><a href="Job.html#repeatAt">repeatAt</a></li><li data-type="method" id="Job-repeatEvery-nav"><a href="Job.html#repeatEvery">repeatEvery</a></li><li data-type="method" id="Job-run-nav"><a href="Job.html#run">run</a></li><li data-type="method" id="Job-schedule-nav"><a href="Job.html#schedule">schedule</a></li><li data-type="method" id="Job-toJSON-nav"><a href="Job.html#toJSON">toJSON</a></li><li data-type="method" id="Job-touch-nav"><a href="Job.html#touch">touch</a></li><li data-type="method" id="Job-unique-nav"><a href="Job.html#unique">unique</a></li></ul></li></ul><h3 id="global-nav">Global</h3><ul><li><a href="global.html#parsePriority">parsePriority</a></li></ul>
  </nav>

  <div id="main">
    

    
      



    

      


  <section class="readme">
    <article>
      <p align="center">
  <img src="https://cdn.rawgit.com/agenda/agenda/master/agenda.svg" alt="Agenda" width="100" height="100">
</p>
<p align="center">
  A light-weight job scheduling library for Node.js
</p>
<p align="center">
  <a href="https://slackin-ekwifvcwbr.now.sh/"><img src="https://slackin-ekwifvcwbr.now.sh/badge.svg" alt="Slack Status"></a>
  <a href="http://travis-ci.org/agenda/agenda"><img src="https://api.travis-ci.org/agenda/agenda.svg?branch=master" alt="Build Status"></a>
  <a href="https://david-dm.org/agenda/agenda"><img src="https://david-dm.org/agenda/agenda/status.svg" alt="dependencies Status"></a>
  <a href="https://david-dm.org/agenda/agenda?type=dev"><img src="https://david-dm.org/agenda/agenda/dev-status.svg" alt="devDependencies Status"></a>
  <a href="https://coveralls.io/github/agenda/agenda?branch=master"><img src="https://coveralls.io/repos/github/agenda/agenda/badge.svg?branch=master" alt="Coverage Status"></a>
    <br>
    <br>
    <br>
</p>

<h1>Agenda offers</h1><ul>
<li>Minimal overhead. Agenda aims to keep its code base small.</li>
<li>Mongo backed persistence layer.</li>
<li>Scheduling with configurable priority, concurrency, and repeating</li>
<li>Scheduling via cron or human readable syntax.</li>
<li>Event backed job queue that you can hook into.</li>
<li><a href="https://github.com/agenda/agendash">Agendash</a>: optional standalone web-interface</li>
<li><a href="https://github.com/agenda/agenda-rest">Agenda-rest</a>: optional standalone REST API</li>
</ul>
<h1>Installation</h1><p>Install via NPM</p>
<pre class="prettyprint source"><code>npm install agenda</code></pre><p>You will also need a working <a href="https://www.mongodb.com/">Mongo</a> database (v3) to point it to.</p>
<h1>Example Usage</h1><pre class="prettyprint source lang-js"><code>const mongoConnectionString = 'mongodb://127.0.0.1/agenda';

const agenda = new Agenda({db: {address: mongoConnectionString}});

// Or override the default collection name:
// const agenda = new Agenda({db: {address: mongoConnectionString, collection: 'jobCollectionName'}});

// or pass additional connection options:
// const agenda = new Agenda({db: {address: mongoConnectionString, collection: 'jobCollectionName', options: {ssl: true}}});

// or pass in an existing mongodb-native MongoClient instance
// const agenda = new Agenda({mongo: myMongoClient});

agenda.define('delete old users', (job, done) => {
  User.remove({lastLogIn: {$lt: twoDaysAgo}}, done);
});

(async function() { // IIFE to give access to async/await
  await agenda.start();

  await agenda.every('3 minutes', 'delete old users');

  // Alternatively, you could also do:
  await agenda.every('*/3 * * * *', 'delete old users');
})();</code></pre><pre class="prettyprint source lang-js"><code>agenda.define('send email report', {priority: 'high', concurrency: 10}, (job, done) => {
  const {to} = job.attrs.data;
  emailClient.send({
    to,
    from: 'example@example.com',
    subject: 'Email Report',
    body: '...'
  }, done);
});

(async function() {
  await agenda.start();
  await agenda.schedule('in 20 minutes', 'send email report', {to: 'admin@example.com'});
})();</code></pre><pre class="prettyprint source lang-js"><code>(async function() {
  const weeklyReport = agenda.create('send email report', {to: 'example@example.com'});
  await agenda.start();
  await weeklyReport.repeatEvery('1 week').save();
})();</code></pre><h1>Full documentation</h1><p>Agenda's basic control structure is an instance of an agenda. Agenda's are
mapped to a database collection and load the jobs from within.</p>
<h2>Table of Contents</h2><ul>
<li><a href="#configuring-an-agenda">Configuring an agenda</a></li>
<li><a href="#agenda-events">Agenda Events</a></li>
<li><a href="#defining-job-processors">Defining job processors</a></li>
<li><a href="#creating-jobs">Creating jobs</a></li>
<li><a href="#managing-jobs">Managing jobs</a></li>
<li><a href="#starting-the-job-processor">Starting the job processor</a></li>
<li><a href="#multiple-job-processors">Multiple job processors</a></li>
<li><a href="#manually-working-with-a-job">Manually working with jobs</a></li>
<li><a href="#job-queue-events">Job Queue Events</a></li>
<li><a href="#frequently-asked-questions">Frequently asked questions</a></li>
<li><a href="#example-project-structure">Example Project structure</a></li>
<li><a href="#known-issues">Known Issues</a></li>
<li><a href="#debugging-issues">Debugging Issues</a></li>
<li><a href="#acknowledgements">Acknowledgements</a></li>
</ul>
<h2>Configuring an agenda</h2><p>All configuration methods are chainable, meaning you can do something like:</p>
<pre class="prettyprint source lang-js"><code>const agenda = new Agenda();
agenda
  .database(...)
  .processEvery('3 minutes')
  ...;</code></pre><p>Agenda uses <a href="http://github.com/rschmukler/human-interval">Human Interval</a> for specifying the intervals. It supports the following units:</p>
<p><code>seconds</code>, <code>minutes</code>, <code>hours</code>, <code>days</code>,<code>weeks</code>, <code>months</code> -- assumes 30 days, <code>years</code> -- assumes 365 days</p>
<p>More sophisticated examples</p>
<pre class="prettyprint source lang-js"><code>agenda.processEvery('one minute');
agenda.processEvery('1.5 minutes');
agenda.processEvery('3 days and 4 hours');
agenda.processEvery('3 days, 4 hours and 36 seconds');</code></pre><h3>database(url, [collectionName])</h3><p>Specifies the database at the <code>url</code> specified. If no collection name is given,
<code>agendaJobs</code> is used.</p>
<pre class="prettyprint source lang-js"><code>agenda.database('localhost:27017/agenda-test', 'agendaJobs');</code></pre><p>You can also specify it during instantiation.</p>
<pre class="prettyprint source lang-js"><code>const agenda = new Agenda({db: {address: 'localhost:27017/agenda-test', collection: 'agendaJobs'}});</code></pre><p>Agenda will emit a <code>ready</code> event (see <a href="#agenda-events">Agenda Events</a>) when properly connected to the database.
It is safe to call <code>agenda.start()</code> without waiting for this event, as this is handled internally.
If you're using the <code>db</code> options, or call <code>database</code>, then you may still need to listen for <code>ready</code> before saving jobs.</p>
<h3>mongo(mongoClientInstance)</h3><p>Use an existing mongodb-native MongoClient instance. This can help consolidate connections to a
database. You can instead use <code>.database</code> to have agenda handle connecting for
you.</p>
<p>Please note that this must be a <em>collection</em>. Also, you will want to run the following
afterwards to ensure the database has the proper indexes:</p>
<pre class="prettyprint source lang-js"><code>(async () => {
  await agenda._ready;

  try {
    agenda._collection.createIndex({
      disabled: 1,
      lockedAt: 1,
      name: 1,
      nextRunAt: 1,
      priority: -1
    }, {
      name: 'findAndLockNextJobIndex'
    });
  } catch (err) {
    console.log('Failed to create Agenda index!');
    console.error(err);

    throw err;
  }

  console.log('Agenda index created.');
})();</code></pre><p>You can also specify it during instantiation.</p>
<pre class="prettyprint source lang-js"><code>const agenda = new Agenda({mongo: mongoClientInstance});</code></pre><h3>name(name)</h3><p>Takes a string <code>name</code> and sets <code>lastModifiedBy</code> to it in the job database.
Useful for if you have multiple job processors (agendas) and want to see which
job queue last ran the job.</p>
<pre class="prettyprint source lang-js"><code>agenda.name(os.hostname + '-' + process.pid);</code></pre><p>You can also specify it during instantiation</p>
<pre class="prettyprint source lang-js"><code>const agenda = new Agenda({name: 'test queue'});</code></pre><h3>processEvery(interval)</h3><p>Takes a string <code>interval</code> which can be either a traditional javascript number,
or a string such as <code>3 minutes</code></p>
<p>Specifies the frequency at which agenda will query the database looking for jobs
that need to be processed. Agenda internally uses <code>setTimeout</code> to guarantee that
jobs run at (close to ~3ms) the right time.</p>
<p>Decreasing the frequency will result in fewer database queries, but more jobs
being stored in memory.</p>
<p>Also worth noting is that if the job 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.</p>
<pre class="prettyprint source lang-js"><code>agenda.processEvery('1 minute');</code></pre><p>You can also specify it during instantiation</p>
<pre class="prettyprint source lang-js"><code>const agenda = new Agenda({processEvery: '30 seconds'});</code></pre><h3>maxConcurrency(number)</h3><p>Takes a <code>number</code> which specifies the max number of jobs that can be running at
any given moment. By default it is <code>20</code>.</p>
<pre class="prettyprint source lang-js"><code>agenda.maxConcurrency(20);</code></pre><p>You can also specify it during instantiation</p>
<pre class="prettyprint source lang-js"><code>const agenda = new Agenda({maxConcurrency: 20});</code></pre><h3>defaultConcurrency(number)</h3><p>Takes a <code>number</code> which specifies the default number of a specific job that can be running at
any given moment. By default it is <code>5</code>.</p>
<pre class="prettyprint source lang-js"><code>agenda.defaultConcurrency(5);</code></pre><p>You can also specify it during instantiation</p>
<pre class="prettyprint source lang-js"><code>const agenda = new Agenda({defaultConcurrency: 5});</code></pre><h3>lockLimit(number)</h3><p>Takes a <code>number</code> which specifies the max number jobs that can be locked at any given moment. By default it is <code>0</code> for no max.</p>
<pre class="prettyprint source lang-js"><code>agenda.lockLimit(0);</code></pre><p>You can also specify it during instantiation</p>
<pre class="prettyprint source lang-js"><code>const agenda = new Agenda({lockLimit: 0});</code></pre><h3>defaultLockLimit(number)</h3><p>Takes a <code>number</code> which specifies the default number of a specific job that can be locked at any given moment. By default it is <code>0</code> for no max.</p>
<pre class="prettyprint source lang-js"><code>agenda.defaultLockLimit(0);</code></pre><p>You can also specify it during instantiation</p>
<pre class="prettyprint source lang-js"><code>const agenda = new Agenda({defaultLockLimit: 0});</code></pre><h3>defaultLockLifetime(number)</h3><p>Takes a <code>number</code> which specifies the default lock lifetime in milliseconds. By
default it is 10 minutes. This can be overridden by specifying the
<code>lockLifetime</code> option to a defined job.</p>
<p>A job will unlock if it is finished (ie. <code>done</code> is called) before the <code>lockLifetime</code>.
The lock is useful if the job crashes or times out.</p>
<pre class="prettyprint source lang-js"><code>agenda.defaultLockLifetime(10000);</code></pre><p>You can also specify it during instantiation</p>
<pre class="prettyprint source lang-js"><code>const agenda = new Agenda({defaultLockLifetime: 10000});</code></pre><h3>sort(query)</h3><p>Takes a <code>query</code> which specifies the sort query to be used for finding and locking the next job.</p>
<p>By default it is <code>{ nextRunAt: 1, priority: -1 }</code>, which obeys a first in first out approach, with respect to priority.</p>
<h2>Agenda Events</h2><p>An instance of an agenda will emit the following events:</p>
<ul>
<li><code>ready</code> - called when Agenda mongo connection is successfully opened and indices created.<pre class="prettyprint source"><code>  If you're passing agenda an existing connection, you shouldn't need to listen for this, as `agenda.start()` will not resolve until indices have been created.
  If you're using the `db` options, or call `database`, then you may still need to listen for the `ready` event before saving jobs. `agenda.start()` will still wait for the connection to be opened.</code></pre></li>
<li><code>error</code> - called when Agenda mongo connection process has thrown an error</li>
</ul>
<pre class="prettyprint source lang-js"><code>await agenda.start();</code></pre><h2>Defining Job Processors</h2><p>Before you can use a job, you must define its processing behavior.</p>
<h3>define(jobName, [options], fn)</h3><p>Defines a job with the name of <code>jobName</code>. When a job of <code>jobName</code> gets run, it
will be passed to <code>fn(job, done)</code>. To maintain asynchronous behavior, you must
call <code>done()</code> when you are processing the job. If your function is synchronous,
you may omit <code>done</code> from the signature.</p>
<p><code>options</code> is an optional argument which can overwrite the defaults. It can take
the following:</p>
<ul>
<li><code>concurrency</code>: <code>number</code> maximum number of that job that can be running at once (per instance of agenda)</li>
<li><code>lockLimit</code>: <code>number</code> maximum number of that job that can be locked at once (per instance of agenda)</li>
<li><code>lockLifetime</code>: <code>number</code> interval in ms of how long the job stays locked for (see <a href="#multiple-job-processors">multiple job processors</a> for more info).
A job will automatically unlock if <code>done()</code> is called.</li>
<li><code>priority</code>: <code>(lowest|low|normal|high|highest|number)</code> specifies the priority
of the job. Higher priority jobs will run first. See the priority mapping
below</li>
</ul>
<p>Priority mapping:</p>
<pre class="prettyprint source"><code>{
  highest: 20,
  high: 10,
  normal: 0,
  low: -10,
  lowest: -20
}</code></pre><p>Async Job:</p>
<pre class="prettyprint source lang-js"><code>agenda.define('some long running job', (job, done) => {
  doSomelengthyTask(data => {
    formatThatData(data);
    sendThatData(data);
    done();
  });
});</code></pre><p>Sync Job:</p>
<pre class="prettyprint source lang-js"><code>agenda.define('say hello', job => {
  console.log('Hello!');
});</code></pre><h2>Creating Jobs</h2><h3>every(interval, name, [data], [options], [cb])</h3><p>Runs job <code>name</code> at the given <code>interval</code>. Optionally, data and options can be passed in.
Every creates a job of type <code>single</code>, 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 <code>webserver.js</code> which may
reboot from time to time.</p>
<p><code>interval</code> can be a human-readable format <code>String</code>, a cron format <code>String</code>, or a <code>Number</code>.</p>
<p><code>data</code> is an optional argument that will be passed to the processing function
under <code>job.attrs.data</code>.</p>
<p><code>options</code> is an optional argument that will be passed to <code>job.repeatEvery</code>. In order to use
this argument, <code>data</code> must also be specified.</p>
<p><code>cb</code> is an optional callback function which will be called when the job has been
persisted in the database.</p>
<p>Returns the <code>job</code>.</p>
<pre class="prettyprint source lang-js"><code>agenda.define('printAnalyticsReport', (job, done) => {
  User.doSomethingReallyIntensive((err, users) => {
    processUserData();
    console.log('I print a report!');
    done();
  });
});

agenda.every('15 minutes', 'printAnalyticsReport');</code></pre><p>Optionally, <code>name</code> could be array of job names, which is convenient for scheduling
different jobs for same <code>interval</code>.</p>
<pre class="prettyprint source lang-js"><code>agenda.every('15 minutes', ['printAnalyticsReport', 'sendNotifications', 'updateUserRecords']);</code></pre><p>In this case, <code>every</code> returns array of <code>jobs</code>.</p>
<h3>schedule(when, name, [data], [cb])</h3><p>Schedules a job to run <code>name</code> once at a given time. <code>when</code> can be a <code>Date</code> or a
<code>String</code> such as <code>tomorrow at 5pm</code>.</p>
<p><code>data</code> is an optional argument that will be passed to the processing function
under <code>job.attrs.data</code>.</p>
<p><code>cb</code> is an optional callback function which will be called when the job has been
persisted in the database.</p>
<p>Returns the <code>job</code>.</p>
<pre class="prettyprint source lang-js"><code>agenda.schedule('tomorrow at noon', 'printAnalyticsReport', {userCount: 100});</code></pre><p>Optionally, <code>name</code> could be array of job names, similar to <code>every</code> method.</p>
<pre class="prettyprint source lang-js"><code>agenda.schedule('tomorrow at noon', ['printAnalyticsReport', 'sendNotifications', 'updateUserRecords']);</code></pre><p>In this case, <code>schedule</code> returns array of <code>jobs</code>.</p>
<h3>now(name, [data], [cb])</h3><p>Schedules a job to run <code>name</code> once immediately.</p>
<p><code>data</code> is an optional argument that will be passed to the processing function
under <code>job.attrs.data</code>.</p>
<p><code>cb</code> is an optional callback function which will be called when the job has been
persisted in the database.</p>
<p>Returns the <code>job</code>.</p>
<pre class="prettyprint source lang-js"><code>agenda.now('do the hokey pokey');</code></pre><h3>create(jobName, data)</h3><p>Returns an instance of a <code>jobName</code> with <code>data</code>. This does <em>NOT</em> save the job in
the database. See below to learn how to manually work with jobs.</p>
<pre class="prettyprint source lang-js"><code>const job = agenda.create('printAnalyticsReport', {userCount: 100});
job.save(err => {
  console.log('Job successfully saved');
});</code></pre><h2>Managing Jobs</h2><h3>jobs(mongodb-native query)</h3><p>Lets you query all of the jobs in the agenda job's database. This is a full <a href="https://github.com/mongodb/node-mongodb-native">mongodb-native</a>
<code>find</code> query. See mongodb-native's documentation for details.</p>
<pre class="prettyprint source lang-js"><code>agenda.jobs({name: 'printAnalyticsReport'}, (err, jobs) => {
  // Work with jobs (see below)
});</code></pre><h3>cancel(mongodb-native query, cb)</h3><p>Cancels any jobs matching the passed mongodb-native query, and removes them from the database.</p>
<pre class="prettyprint source lang-js"><code>agenda.cancel({name: 'printAnalyticsReport'}, (err, numRemoved) => {
  // ...
});</code></pre><p>This functionality can also be achieved by first retrieving all the jobs from the database using <code>agenda.jobs()</code>, looping through the resulting array and calling <code>job.remove()</code> on each. It is however preferable to use <code>agenda.cancel()</code> for this use case, as this ensures the operation is atomic.</p>
<h3>purge(cb)</h3><p>Removes all jobs in the database without defined behaviors. Useful if you change a definition name and want to remove old jobs.</p>
<p><em>IMPORTANT:</em> Do not run this before you finish defining all of your jobs. If you do, you will nuke your database of jobs.</p>
<pre class="prettyprint source lang-js"><code>agenda.purge((err, numRemoved) => {
  // ...
});</code></pre><h2>Starting the job processor</h2><p>To get agenda to start processing jobs from the database you must start it. This
will schedule an interval (based on <code>processEvery</code>) to check for new jobs and
run them. You can also stop the queue.</p>
<h3>start</h3><p>Starts the job queue processing, checking <code>processEvery</code> time to see if there
are new jobs.</p>
<h3>stop</h3><p>Stops the job queue processing. Unlocks currently running jobs.</p>
<p>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.</p>
<pre class="prettyprint source lang-js"><code>async function graceful() {
  await agenda.stop();
  process.exit(0);
}

process.on('SIGTERM', graceful);
process.on('SIGINT' , graceful);</code></pre><h2>Multiple job processors</h2><p>Sometimes you may want to have multiple node instances / machines process from
the same queue. Agenda supports a locking mechanism to ensure that multiple
queues don't process the same job.</p>
<p>You can configure the locking mechanism by specifying <code>lockLifetime</code> as an
interval when defining the job.</p>
<pre class="prettyprint source lang-js"><code>agenda.define('someJob', {lockLifetime: 10000}, (job, cb) => {
  // Do something in 10 seconds or less...
});</code></pre><p>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.</p>
<p>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.</p>
<p>When a job is finished (ie. <code>done</code> is called), it will automatically unlock.</p>
<h2>Manually working with a job</h2><p>A job instance has many instance methods. All mutating methods must be followed
with a call to <code>job.save()</code> in order to persist the changes to the database.</p>
<h3>repeatEvery(interval, [options])</h3><p>Specifies an <code>interval</code> on which the job should repeat. The job runs at the time of defining as well in configured intervals, that is &quot;run <em>now</em> and in intervals&quot;.</p>
<p><code>interval</code> can be a human-readable format <code>String</code>, a cron format <code>String</code>, or a <code>Number</code>.</p>
<p><code>options</code> is an optional argument containing:</p>
<p><code>options.timezone</code>: should be a string as accepted by <a href="https://momentjs.com/timezone/">moment-timezone</a> and is considered when using an interval in the cron string format.</p>
<p><code>options.skipImmediate</code>: <code>true</code> | <code>false</code> (default) Setting this <code>true</code> will skip the immediate run. The first run will occur only in configured interval.</p>
<pre class="prettyprint source lang-js"><code>job.repeatEvery('10 minutes');
job.save();</code></pre><pre class="prettyprint source lang-js"><code>job.repeatEvery('3 minutes', {
  skipImmediate: true
});
job.save();</code></pre><pre class="prettyprint source lang-js"><code>job.repeatEvery('0 6 * * *', {
  timezone: 'America/New_York'
});
job.save();</code></pre><h3>repeatAt(time)</h3><p>Specifies a <code>time</code> when the job should repeat. <a href="https://github.com/matthewmueller/date#examples">Possible values</a></p>
<pre class="prettyprint source lang-js"><code>job.repeatAt('3:30pm');
job.save();</code></pre><h3>schedule(time)</h3><p>Specifies the next <code>time</code> at which the job should run.</p>
<pre class="prettyprint source lang-js"><code>job.schedule('tomorrow at 6pm');
job.save();</code></pre><h3>priority(priority)</h3><p>Specifies the <code>priority</code> weighting of the job. Can be a number or a string from
the above priority table.</p>
<pre class="prettyprint source lang-js"><code>job.priority('low');
job.save();</code></pre><h3>unique(properties, [options])</h3><p>Ensure that only one instance of this job exists with the specified properties</p>
<p><code>options</code> is an optional argument which can overwrite the defaults. It can take
the following:</p>
<ul>
<li><code>insertOnly</code>: <code>boolean</code> will prevent any properties from persisting if job already exists. Defaults to false.</li>
</ul>
<pre class="prettyprint source lang-js"><code>job.unique({'data.type': 'active', 'data.userId': '123', nextRunAt(date)});
job.save();</code></pre><p><em>IMPORTANT:</em> To avoid high CPU usage by MongoDB, Make sure to create an index on the used fields, like: <code>data.type</code> and <code>data.userId</code> for the example above.</p>
<h3>fail(reason)</h3><p>Sets <code>job.attrs.failedAt</code> to <code>now</code>, and sets <code>job.attrs.failReason</code>
to <code>reason</code>.</p>
<p>Optionally, <code>reason</code> can be an error, in which case <code>job.attrs.failReason</code> will
be set to <code>error.message</code></p>
<pre class="prettyprint source lang-js"><code>job.fail('insuficient disk space');
// or
job.fail(new Error('insufficient disk space'));
job.save();</code></pre><h3>run(callback)</h3><p>Runs the given <code>job</code> and calls <code>callback(err, job)</code> upon completion. Normally
you never need to call this manually.</p>
<pre class="prettyprint source lang-js"><code>job.run((err, job) => {
  console.log('I don\'t know why you would need to do this...');
});</code></pre><h3>save(callback)</h3><p>Saves the <code>job.attrs</code> into the database.</p>
<pre class="prettyprint source lang-js"><code>job.save(err => {
  if (!err) {
    console.log('Successfully saved job to collection');
  }
});</code></pre><h3>remove(callback)</h3><p>Removes the <code>job</code> from the database.</p>
<pre class="prettyprint source lang-js"><code>job.remove(err => {
  if (!err) {
    console.log('Successfully removed job from collection');
  }
});</code></pre><h3>disable()</h3><p>Disables the <code>job</code>. Upcoming runs won't execute.</p>
<h3>enable()</h3><p>Enables the <code>job</code> if it got disabled before. Upcoming runs will execute.</p>
<h3>touch(callback)</h3><p>Resets the lock on the job. Useful to indicate that the job hasn't timed out
when you have very long running jobs.</p>
<pre class="prettyprint source lang-js"><code>agenda.define('super long job', (job, done) => {
  doSomeLongTask(() => {
    job.touch(() => {
      doAnotherLongTask(() => {
        job.touch(() => {
          finishOurLongTasks(done);
        });
      });
    });
  });
});</code></pre><h2>Job Queue Events</h2><p>An instance of an agenda will emit the following events:</p>
<ul>
<li><code>start</code> - called just before a job starts</li>
<li><code>start:job name</code> - called just before the specified job starts</li>
</ul>
<pre class="prettyprint source lang-js"><code>agenda.on('start', job => {
  console.log('Job %s starting', job.attrs.name);
});</code></pre><ul>
<li><code>complete</code> - called when a job finishes, regardless of if it succeeds or fails</li>
<li><code>complete:job name</code> - called when a job finishes, regardless of if it succeeds or fails</li>
</ul>
<pre class="prettyprint source lang-js"><code>agenda.on('complete', job => {
  console.log(`Job ${job.attrs.name} finished`);
});</code></pre><ul>
<li><code>success</code> - called when a job finishes successfully</li>
<li><code>success:job name</code> - called when a job finishes successfully</li>
</ul>
<pre class="prettyprint source lang-js"><code>agenda.on('success:send email', job => {
  console.log(`Sent Email Successfully to ${job.attrs.data.to}`);
});</code></pre><ul>
<li><code>fail</code> - called when a job throws an error</li>
<li><code>fail:job name</code> - called when a job throws an error</li>
</ul>
<pre class="prettyprint source lang-js"><code>agenda.on('fail:send email', (err, job) => {
  console.log('Job failed with error: ${err.message}');
});</code></pre><h2>Frequently Asked Questions</h2><h3>What is the order in which jobs run?</h3><p>Jobs are run with priority in a first in first out order (so they will be run in the order they were scheduled AND with respect to highest priority).</p>
<p>For example, if we have two jobs named &quot;send-email&quot; queued (both with the same priority), and the first job is queued at 3:00 PM and second job is queued at 3:05 PM with the same <code>priority</code> value, then the first job will run first if we start to send &quot;send-email&quot; jobs at 3:10 PM. However if the first job has a priority of <code>5</code> and the second job has a priority of <code>10</code>, then the second will run first (priority takes precedence) at 3:10 PM.</p>
<p>The default <a href="https://docs.mongodb.com/manual/reference/method/cursor.sort/">MongoDB sort object</a> is <code>{ nextRunAt: 1, priority: -1 }</code> and can be changed through the option <code>sort</code> when configuring Agenda.</p>
<h3>What is the difference between <code>lockLimit</code> and <code>maxConcurrency</code>?</h3><p>Agenda will lock jobs 1 by one, setting the <code>lockedAt</code> property in mongoDB, and creating an instance of the <code>Job</code> class which it caches into the <code>_lockedJobs</code> array. This defaults to having no limit, but can be managed using lockLimit. If all jobs will need to be run before agenda's next interval (set via <code>agenda.processEvery</code>), then agenda will attempt to lock all jobs.</p>
<p>Agenda will also pull jobs from <code>_lockedJobs</code> and into <code>_runningJobs</code>. These jobs are actively being worked on by user code, and this is limited by <code>maxConcurrency</code> (defaults to 20).</p>
<p>If you have multiple instances of agenda processing the same job definition with a fast repeat time you may find they get unevenly loaded. This is because they will compete to lock as many jobs as possible, even if they don't have enough concurrency to process them. This can be resolved by tweaking the <code>maxConcurrency</code> and <code>lockLimit</code> properties.</p>
<h3>Sample Project Structure?</h3><p>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
<a href="#example-project-structure">example project structure</a> below.</p>
<h3>Can I Donate?</h3><p>Thanks! I'm flattered, but it's really not necessary. If you really want to, you can find my <a href="https://www.gittip.com/rschmukler/">gittip here</a>.</p>
<h3>Web Interface?</h3><p>Agenda itself does not have a web interface built in but we do offer stand-alone web interface <a href="https://github.com/agenda/agendash">Agendash</a>:</p>
<p><a href="https://raw.githubusercontent.com/agenda/agendash/master/job-details.png"><img src="https://raw.githubusercontent.com/agenda/agendash/master/job-details.png" style="max-width:100%" alt="Agendash interface"></a></p>
<h3>Mongo vs Redis</h3><p>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).</p>
<p>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).</p>
<p>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.</p>
<h3>Spawning / forking processes.</h3><p>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, <a href="http://github.com/bars3s">Bars3s</a> has written up a fantastic example of how one might do it:</p>
<pre class="prettyprint source lang-js"><code>const cluster = require('cluster');
const os = require('os');

const httpServer = require('./app/http-server');
const jobWorker = require('./app/job-worker');

const jobWorkers = [];
const webWorkers = [];

if (cluster.isMaster) {
  const cpuCount = os.cpus().length;
  // Create a worker for each CPU
  for (let i = 0; i &lt; cpuCount; i += 1) {
    addJobWorker();
    addWebWorker();
  }

  cluster.on('exit', (worker, code, signal) => {
    if (jobWorkers.indexOf(worker.id) !== -1) {
      console.log(`job worker ${worker.process.pid} exited (signal: ${signal}). Trying to respawn...`);
      removeJobWorker(worker.id);
      addJobWorker();
    }

    if (webWorkers.indexOf(worker.id) !== -1) {
      console.log(`http worker ${worker.process.pid} exited (signal: ${signal}). Trying to respawn...`);
      removeWebWorker(worker.id);
      addWebWorker();
    }
  });
} else {
  if (process.env.web) {
    console.log(`start http server: ${cluster.worker.id}`);
    // Initialize the http server here
    httpServer.start();
  }

  if (process.env.job) {
    console.log(`start job server: ${cluster.worker.id}`);
    // Initialize the Agenda here
    jobWorker.start();
  }
}

function addWebWorker() {
  webWorkers.push(cluster.fork({web: 1}).id);
}

function addJobWorker() {
  jobWorkers.push(cluster.fork({job: 1}).id);
}

function removeWebWorker(id) {
  webWorkers.splice(webWorkers.indexOf(id), 1);
}

function removeJobWorker(id) {
  jobWorkers.splice(jobWorkers.indexOf(id), 1);
}</code></pre><h3>Recovering lost Mongo connections (&quot;auto_reconnect&quot;)</h3><p>Agenda is configured by default to automatically reconnect indefinitely, emitting an <a href="#agenda-events">error event</a>
when no connection is available on each <a href="#processeveryinterval">process tick</a>, allowing you to restore the Mongo
instance without having to restart the application.</p>
<p>However, if you are using an <a href="#mongomongoclientinstance">existing Mongo client</a>
you'll need to configure the <code>reconnectTries</code> and <code>reconnectInterval</code> <a href="http://mongodb.github.io/node-mongodb-native/3.0/reference/connecting/connection-settings/">connection settings</a>
manually, otherwise you'll find that Agenda will throw an error with the message &quot;MongoDB connection is not recoverable,
application restart required&quot; if the connection cannot be recovered within 30 seconds.</p>
<h1>Example Project Structure</h1><p>Agenda will only process jobs that it has definitions for. This allows you to
selectively choose which jobs a given agenda will process.</p>
<p>Consider the following project structure, which allows us to share models with
the rest of our code base, and specify which jobs a worker processes, if any at
all.</p>
<pre class="prettyprint source"><code>- server.js
- worker.js
lib/
  - agenda.js
  controllers/
    - user-controller.js
  jobs/
    - email.js
    - video-processing.js
    - image-processing.js
   models/
     - user-model.js
     - blog-post.model.js</code></pre><p>Sample job processor (eg. <code>jobs/email.js</code>)</p>
<pre class="prettyprint source lang-js"><code>let email = require('some-email-lib'),
  User = require('../models/user-model.js');

module.exports = function(agenda) {
  agenda.define('registration email', (job, done) => {
    User.get(job.attrs.data.userId, (err, user) => {
      if (err) {
        return done(err);
      }
      email(user.email(), 'Thanks for registering', 'Thanks for registering ' + user.name(), done);
    });
  });

  agenda.define('reset password', (job, done) => {
    // Etc
  });

  // More email related jobs
};</code></pre><p>lib/agenda.js</p>
<pre class="prettyprint source lang-js"><code>const Agenda = require('agenda');

const connectionOpts = {db: {address: 'localhost:27017/agenda-test', collection: 'agendaJobs'}};

const agenda = new Agenda(connectionOpts);

const jobTypes = process.env.JOB_TYPES ? process.env.JOB_TYPES.split(',') : [];

jobTypes.forEach(type => {
  require('./lib/jobs/' + type)(agenda);
});

if (jobTypes.length) {
  agenda.start(); // Returns a promise, which should be handled appropriately
}

module.exports = agenda;</code></pre><p>lib/controllers/user-controller.js</p>
<pre class="prettyprint source lang-js"><code>let app = express(),
  User = require('../models/user-model'),
  agenda = require('../worker.js');

app.post('/users', (req, res, next) => {
  const user = new User(req.body);
  user.save(err => {
    if (err) {
      return next(err);
    }
    agenda.now('registration email', {userId: user.primary()});
    res.send(201, user.toJson());
  });
});</code></pre><p>worker.js</p>
<pre class="prettyprint source lang-js"><code>require('./lib/agenda.js');</code></pre><p>Now you can do the following in your project:</p>
<pre class="prettyprint source lang-bash"><code>node server.js</code></pre><p>Fire up an instance with no <code>JOB_TYPES</code>, giving you the ability to process jobs,
but not wasting resources processing jobs.</p>
<pre class="prettyprint source lang-bash"><code>JOB_TYPES=email node server.js</code></pre><p>Allow your http server to process email jobs.</p>
<pre class="prettyprint source lang-bash"><code>JOB_TYPES=email node worker.js</code></pre><p>Fire up an instance that processes email jobs.</p>
<pre class="prettyprint source lang-bash"><code>JOB_TYPES=video-processing,image-processing node worker.js</code></pre><p>Fire up an instance that processes video-processing/image-processing jobs. Good for a heavy hitting server.</p>
<h1>Known Issues</h1><h4>&quot;Multiple order-by items are not supported. Please specify a single order-by item.&quot;</h4><p>When running Agenda on Azure cosmosDB, you might run into this issue caused by Agenda's sort query used for finding and locking the next job. To fix this, you can pass <a href="https://github.com/agenda/agenda#sortquery">custom sort option</a>: <code>sort: { nextRunAt: 1 }</code></p>
<h2>Versions &lt;= 0.9.1</h2><h4>Cron string parsing (<a href="https://github.com/agenda/agenda/pull/475">PR</a>)</h4><p>The current versions of Agenda parse cron dates as follows using this library:
<a href="https://github.com/kelektiv/node-cron">node-cron</a></p>
<p><em>This library treats months as 0-11 where as normally, cron months are parsed as 1-12.</em></p>
<pre class="prettyprint source"><code>* * * * * *
| | | | | |
| | | | | +-- Year              (range: 1900-3000)
| | | | +---- Day of the Week   (range: 1-7, 1 standing for Monday)
| | | +------ Month of the Year (range: 0-11) NOTE: Difference here
| | +-------- Day of the Month  (range: 1-31)
| +---------- Hour              (range: 0-23)
+------------ Minute            (range: 0-59)</code></pre><p>Starting in version <code>1.0.0</code>, cron will be parsed in the standard UNIX style:</p>
<pre class="prettyprint source"><code>* * * * * *
| | | | | |
| | | | | +-- Year              (range: 1900-3000)
| | | | +---- Day of the Week   (range: 1-7, 1 standing for Monday)
| | | +------ Month of the Year (range: 1-12) NOTE: Difference here
| | +-------- Day of the Month  (range: 1-31)
| +---------- Hour              (range: 0-23)
+------------ Minute            (range: 0-59)</code></pre><h1>Debugging Issues</h1><p>If you think you have encountered a bug, please feel free to report it here:</p>
<p><a href="https://github.com/agenda/agenda/issues/new">Submit Issue</a></p>
<p>Please provide us with as much details as possible such as:</p>
<ul>
<li>Agenda version</li>
<li>Environment (OSX, Linux, Windows, etc)</li>
<li>Small description of what happened</li>
<li>Any relevant stack track</li>
<li>Agenda logs (see below)</li>
</ul>
<h4>To turn on logging, please set your DEBUG env variable like so:</h4><ul>
<li>OSX: <code>DEBUG=&quot;agenda:*&quot; node index.js</code></li>
<li>Linux: <code>DEBUG=&quot;agenda:*&quot; node index.js</code></li>
<li>Windows CMD: <code>set DEBUG=agenda:*</code></li>
<li>Windows PowerShell: <code>$env:DEBUG = &quot;agenda:*&quot;</code></li>
</ul>
<p>While not necessary, attaching a text file with this debug information would
be extremely useful in debugging certain issues and is encouraged.</p>
<h1>Acknowledgements</h1><ul>
<li>Agenda was originally created by <a href="https://github.com/rschmukler">@rschmukler</a>.</li>
<li><a href="https://github.com/agenda/agendash">Agendash</a> was originally created by <a href="https://github.com/joeframbach">@joeframbach</a>.</li>
<li>These days Agenda has a great community of <a href="https://github.com/agenda/agenda/graphs/contributors">contributors</a> around it. <a href="https://github.com/agenda/agenda/wiki">Join us!</a></li>
</ul>
<h1>License</h1><p><a href="LICENSE.md">The MIT License</a></p>
    </article>
  </section>


    


  </div>

  <br class="clear">

  <footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a>
  </footer>

  <script src="scripts/linenumber.js"></script>
  <script src="scripts/pagelocation.js"></script>

  
  
</body>
</html>