abstract class Amber::Jobs::Job

Overview

Abstract base class for all background jobs.

To define a job, inherit from this class, include JSON::Serializable, and implement the #perform method. Any instance variables decorated with JSON::Field will be serialized when the job is enqueued and deserialized when the job is executed.

Example

class SendWelcomeEmail < Amber::Jobs::Job
  include JSON::Serializable

  property user_id : Int64

  def initialize(@user_id : Int64)
  end

  def perform
    user = User.find(user_id)
    Mailer.send_welcome(user)
  end

  def self.queue : String
    "mailers"
  end
end

Enqueueing

SendWelcomeEmail.new(user_id: 42_i64).enqueue
SendWelcomeEmail.new(user_id: 42_i64).enqueue(delay: 5.minutes)
SendWelcomeEmail.new(user_id: 42_i64).enqueue(queue: "critical")

Configuration

Override class methods to customize job behavior:

Defined in:

amber/jobs/job.cr

Class Method Summary

Instance Method Summary

Class Method Detail

def self.max_retries : Int32 #

Maximum number of retry attempts before the job is marked as dead.


[View source]
def self.queue : String #

The queue name this job class should be enqueued to by default.


[View source]
def self.retry_backoff(attempt : Int32) : Time::Span #

Calculates the retry backoff duration for the given attempt number. Uses exponential backoff by default: 2^attempt seconds.

Override this to implement custom backoff strategies.


[View source]

Instance Method Detail

def enqueue(queue : String | Nil = nil, delay : Time::Span | Nil = nil) : JobEnvelope #

Enqueues this job for background processing.

  • .queue - Override the default queue for this specific enqueue
  • delay - Delay execution by the specified duration

[View source]
abstract def perform #

Override this method to define the job's work.


[View source]