abstract class
Amber::Jobs::Job
- Amber::Jobs::Job
- Reference
- Object
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:
self.queue- The queue name (default: "default")self.max_retries- Maximum retry attempts (default: 3)self.retry_backoff(attempt)- Backoff duration per attempt (default: exponential)
Defined in:
amber/jobs/job.crClass Method Summary
-
.max_retries : Int32
Maximum number of retry attempts before the job is marked as dead.
-
.queue : String
The queue name this job class should be enqueued to by default.
-
.retry_backoff(attempt : Int32) : Time::Span
Calculates the retry backoff duration for the given attempt number.
Instance Method Summary
-
#enqueue(queue : String | Nil = nil, delay : Time::Span | Nil = nil) : JobEnvelope
Enqueues this job for background processing.
-
#perform
Override this method to define the job's work.
Class Method Detail
The queue name this job class should be enqueued to by default.
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.
Instance Method Detail
Enqueues this job for background processing.
.queue- Override the default queue for this specific enqueuedelay- Delay execution by the specified duration