agenda/Job.js

/**
 * @module flitter-agenda/Job
 */

const { Injectable } = require('flitter-di')

/**
 * Parent class to all flitter-agenda job definitions.
 * Specifies the method that should be executed when the job is run.
 * @extends module:flitter-di/src/Injectable~Injectable
 */
class Job extends Injectable {
    /**
     * Get the services required by this class. The 'jobs' service
     * is requested automatically.
     * @returns {Array<string>}
     */
    static get services() {
        return [...super.services, 'jobs']
    }

    /**
     * Set by the AgendaUnit when the job is loaded.
     * The Flitter canonical name of the job. This is used to
     * identify the job class to the Agenda scheduler.
     * @type {string}
     */
    static JOB_NAME = ''

    /**
     * Queue a request to run this job at the specific time with the data.
     * @param {string} [when = 'now'] - a time describing string - 'in 3 minutes'/'now'/'tomorrow'
     * @param {object} [data = {}] - optional data to be passed to the job
     * @returns {Promise<void>}
     */
    static async enqueue(when = 'now', data = {}) {
        return this.prototype.jobs.scheduler.schedule(when, this.JOB_NAME, data)
    }

    /**
     * Executed when the job is run.
     * See the Agenda package docs for more info.
     * 
     * @param {Job} job - Agenda job instance. See the Agenda package docs for more info.
     * @param {function} done - should be called when the job completes
     */
    exec(job, done){
        done()
    }
}

module.exports = exports = Job