class JoobQ::Scheduler

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

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

  1. Initialization:
  1. Delaying Jobs:
  1. Scheduling Recurring Jobs:
  1. Scheduling Cron Jobs:
  1. Job Execution:

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.cr

Class Method Summary

Instance Method Summary

Class Method Detail

def self.instance #

[View source]

Instance Method Detail

def clear #

[View source]
def cron(pattern, &block : -> ) #

[View source]
def delay(job : Job, for till : Time::Span) #

[View source]
def enqueue(current_time = Time.local) #

[View source]
def every(interval : Time::Span, job : Job.class, **args) #

[View source]
def jobs : Hash(String, CronParser | JoobQ::Scheduler::RecurringJobs) #

[View source]
def run #

[View source]