jobScheduler

Cron-based scheduler that queues jobs.

Interface

this.jobScheduler.start()    // Start scheduler loop
this.jobScheduler.stop()     // Stop scheduler gracefully
this.jobScheduler.destroy()  // Alias for stop()
this.jobScheduler.scheduleJobs(unixTime)  // Queue jobs for timestamp

Job Meta Interface

this.schedule(crontab)              // Schedule job with cron expression
this.schedule(crontab, params)      // Schedule with parameters

Description

The jobScheduler service runs a continuous loop checking for scheduled jobs every second. When a job's cron expression matches the current time, the scheduler pushes the job to jobQueue. The scheduler starts automatically with the server unless --without-jobs is specified.

Examples

Job Definition

// lib/jobs/cleanup.js
export default {
    meta() {
        this.schedule('*/5 * * * *');  // Every 5 minutes
    },

    async run() {
        await this.database.sessions.where({
            lastAccessedAtLt: new Date(Date.now() - 30 * 60 * 1000)
        }).delete();
    }
};

Multiple Schedules

export default {
    meta() {
        this.schedule('*/5 * * * *');    // Every 5 minutes
        this.schedule('0 0 * * *');       // Daily at midnight
        this.schedule('0 9 * * 1');       // Monday at 9 AM
    },

    async run() {
        // Job logic
    }
};

Schedule with Parameters

// lib/jobs/generate_report.js
export default {
    meta() {
        this.schedule('0 6 * * *', { type: 'daily', format: 'pdf' });
        this.schedule('0 0 1 * *', { type: 'monthly', format: 'csv' });
    },

    async run() {
        const { type, format } = this.params;
        await this.generateReport(type, format);
    }
};

Parameters passed to schedule() are available via this.params in the run() method. This allows a single job to handle multiple schedules with different configurations.

Manual Control

// Start manually
const loop = this.jobScheduler.start();

// Stop gracefully
await this.jobScheduler.stop();

Cron Syntax

minute (0-59)
| hour (0-23)
| | day of month (1-31)
| | | month (1-12)
| | | | day of week (0-6)
* * * * *

Common patterns:

  • * * * * * - Every minute
  • */5 * * * * - Every 5 minutes
  • 0 2 * * * - Daily at 2 AM
  • 0 0 1 * * - First of month at midnight

Notes

  • Scheduler only queues jobs; jobWorker executes them
  • Uses cron-parser for expression parsing
  • Loop checks every 1 second