class JoobQ::Scheduler
- JoobQ::Scheduler
- Reference
- Object
Overview
The Scheduler
class is responsible for managing job scheduling. It supports scheduling jobs to run at specific
intervals, delaying jobs, and running jobs based on cron patterns. The Scheduler
ensures that jobs are executed
at the right time and provides mechanisms for recurring and delayed job execution.
Properties
jobs : Hash(String, RecurringJobs | CronParser)
- A hash that stores scheduled jobs and their intervals or cron patterns.store : Store
- The store instance used for job storage and retrieval.
Usage
Delaying a Job
To delay a job for a specific timespan, use the #delay
method:
scheduler = JoobQ.scheduler
job = ExampleJob.new(x: 1)
scheduler.delay(job, for: 2.minutes)
Scheduling a Recurring Job
To schedule a job to run at a specific interval, use the #every
method:
scheduler = JoobQ.scheduler
scheduler.every(5.minutes, ExampleJob, x: 1)
Scheduling a Cron Job
To schedule a job based on a cron pattern, use the #cron
method:
scheduler = JoobQ.scheduler
scheduler.cron("*/5 * * * *") do
ExampleJob.new(x: 1).perform
end
Scheduler Workflow
- Initialization:
- The
Scheduler
class is initialized as a singleton instance using the.instance
method. - The
#jobs
hash is used to store scheduled jobs and their intervals or cron patterns.
- Delaying Jobs:
- The
#delay
method schedules a job to be executed after a specified timespan. - The job is added to the store with a delay in milliseconds.
- Scheduling Recurring Jobs:
- The
#every
method schedules a job to run at a specified interval. - A new job instance is created and stored in the
#jobs
hash. - A loop is spawned to execute the job at the specified interval.
- Scheduling Cron Jobs:
- The
#cron
method schedules a job based on a cron pattern. - A
CronParser
instance is created and stored in the#jobs
hash. - A loop is spawned to execute the job based on the cron pattern.
- Job Execution:
- For recurring jobs, the job is executed at the specified interval.
- For cron jobs, the job is executed based on the cron pattern.
- The
perform
method of the job is called to execute the job logic.
Example
Here is a complete example demonstrating how to use the Scheduler
class:
require "joobq"
# Define a job
struct ExampleJob
include JoobQ::Job
property x : Int32
def initialize(@x : Int32)
end
def perform
puts "Performing job with x = #{x}"
end
end
# Get the scheduler instance
scheduler = JoobQ.scheduler
# Delay a job
job = ExampleJob.new(x: 1)
scheduler.delay(job, for: 2.minutes)
# Schedule a recurring job
scheduler.every(5.minutes, ExampleJob, x: 1)
# Schedule a cron job
scheduler.cron("*/5 * * * *") do
ExampleJob.new(x: 1).perform
end
This example sets up a scheduler, delays a job, schedules a recurring job, and schedules a cron job.
Defined in:
joobq/scheduler.crClass Method Summary
Instance Method Summary
- #clear
- #cron(pattern, &block : -> )
- #delay(job : Job, for till : Time::Span)
- #enqueue(current_time = Time.local)
- #every(interval : Time::Span, job : Job.class, **args)
- #jobs : Hash(String, CronParser | JoobQ::Scheduler::RecurringJobs)
- #run