module JoobQ::Job

Overview

The JoobQ::Job module provides an abstract structure for defining jobs within the JoobQ asynchronous job processing framework. This module includes functionality for managing job statuses, scheduling, retries, timeouts, and more.

Example Usage

To define a custom job, include the JoobQ::Job module within your job class and implement the #perform method.

class ExampleJob
  include JoobQ::Job

  def perform
    puts "Executing job logic"
  end
end

You can then enqueue, delay, or schedule the job using provided methods.

Job Status

The JoobQ::Job::Status enum defines the possible states for a job:

Each status has corresponding predicate and setter methods for checking and updating job status.

Properties

Methods

Status Predicate and Setter Methods

The module automatically defines predicate and setter methods for each job status:

job = ExampleJob.new
job.running! # Sets the job's status to Running
job.running? # Checks if the job's status is Running

Enqueue and Execution Methods

ExampleJob.batch_enqueue([job1, job2, job3])
ExampleJob.enqueue(param: "value")
ExampleJob.perform(param: "value")

Delay and Scheduling

ExampleJob.enqueue_at(1.minute, param: "value")
ExampleJob.delay(2.minutes, param: "value")
ExampleJob.schedule(5.seconds, param: "value")

Timeout Handling

#with_timeout provides a way to enforce a timeout on the job's execution. If the block takes longer than the specified @timeout, it raises a Timeout::TimeoutError.

def perform
  with_timeout do
    # Simulate a long-running task
    puts "Starting a task that should timeout..."
    sleep 10.seconds
  rescue Timeout::TimeoutError => e
    puts e.message # => "execution expired after 5 seconds"
  end
end

Defined in:

joobq/job.cr

Instance Method Summary

Instance Method Detail

def <=>(other : T) #

[View source]
abstract def perform #

[View source]
def with_timeout(timeout : Time::Span = JoobQ.config.timeout, &block : -> ) #

With timeout method to run a block of code with a timeout limit set

@timeout = 5.seconds

with_timeout do
  # Simulate a long-running task
  puts "Starting a task that should timeout..."
  sleep 10.seconds
  puts "This should not print because the task should be timed out"
rescue Timeout::TimeoutError => e
  puts e.message # => "execution expired after 5 seconds"
end

[View source]