Class: Postburner::DefaultQueue

Inherits:
StrictQueue show all
Defined in:
lib/postburner/strategies/default_queue.rb

Overview

Note:

This is the DEFAULT strategy set on Postburner initialization

Default production queue strategy with graceful handling of premature execution.

This is the recommended production strategy and the default for Postburner. Unlike the strict StrictQueue strategy, DefaultQueue automatically re-inserts jobs that are executed before their scheduled run_at time, ensuring they run at the correct time without raising exceptions.

## When to Use DefaultQueue

Choose this strategy for production environments:

  • **Default choice:** This should be your go-to production strategy

  • **Graceful recovery:** Automatically handles scheduling edge cases

  • **Worker restarts:** Handles jobs picked up during worker restarts

  • **Clock drift:** Tolerates minor time synchronization issues

  • **Robust operation:** Prevents job failures due to timing issues

This strategy provides the most forgiving and robust behavior for production systems where you want scheduled jobs to execute correctly even if workers pick them up slightly early.

## Strategy Behavior

  • Execution: Asynchronous via Beanstalkd workers

  • **Testing mode:** Returns false

  • **Premature execution:** Re-inserts job with calculated delay, logs the event

  • Beanstalkd: Required and used for all job queueing

## How Premature Execution Works

When a job is executed before its run_at time:

  1. Calculates remaining delay: ‘job.run_at - Time.zone.now`

  2. Re-inserts job to Beanstalkd with calculated delay

  3. Logs “PREMATURE; RE-INSERTED” message to job logs

  4. Returns without executing the job

  5. Job executes normally when picked up at the correct time

## Usage

Examples:

Default behavior (automatically set)

# No configuration needed - DefaultQueue is the default
job = MyJob.create!(args: { user_id: 123 })
job.queue!(delay: 1.hour)

Explicitly activate DefaultQueue

Postburner.default_strategy!
job = MyJob.create!(args: {})
job.queue!(at: Time.zone.now + 2.days)

Premature execution is handled gracefully

job = MyJob.create!(args: {})
job.queue!(delay: 1.hour)
# Worker picks up job 5 minutes early...
# Job is automatically re-inserted with 55-minute delay
# "PREMATURE; RE-INSERTED" logged to job.logs

See Also:

Class Method Summary collapse

Methods inherited from StrictQueue

handle_perform!, insert, testing

Class Method Details

.handle_premature_perform(job) ⇒ void

Note:

Logs “PREMATURE; RE-INSERTED” message to job’s audit trail

This method returns an undefined value.

Handles jobs executed before their scheduled run_at time.

Unlike the parent StrictQueue strategy, DefaultQueue gracefully handles premature execution by re-inserting the job with the appropriate delay instead of raising an exception.

Parameters:



79
80
81
82
# File 'lib/postburner/strategies/default_queue.rb', line 79

def handle_premature_perform(job)
  response = job.insert! delay: job.run_at - Time.current
  job.log! "PREMATURE; RE-INSERTED: #{response}"
end