Class: Postburner::NullQueue

Inherits:
TestQueue
  • Object
show all
Extended by:
TimeHelpers
Defined in:
lib/postburner/strategies/null_queue.rb

Overview

Note:

This is a test-mode strategy (returns true for testing)

Null queue strategy for creating jobs without queueing to Beanstalkd.

This strategy creates job records in the database but does NOT queue them to Beanstalkd. Jobs can be manually executed later using NullQueue.handle_perform!, which includes automatic time travel for scheduled jobs.

## When to Use NullQueue

Choose this strategy when you want:

  • **Deferred batch processing:** Create many jobs upfront, execute them manually later

  • **Conditional execution:** Queue jobs that may or may not execute based on runtime conditions

  • Testing/debugging: Inspect jobs before execution without auto-execution

  • **Manual control:** Explicitly control when jobs execute

  • **No Beanstalkd:** Create jobs without requiring Beanstalkd server

## Strategy Behavior

  • Execution: Manual via NullQueue.handle_perform! call

  • **Testing mode:** Returns true

  • Queueing: Jobs created in database but NOT sent to Beanstalkd

  • Beanstalkd: Not used (bkid remains nil)

  • **Time travel:** Automatic for scheduled jobs during manual execution

## Usage

Examples:

Create jobs without queueing

Postburner.null_strategy!
job1 = MyJob.create!(args: { id: 1 })
job2 = MyJob.create!(args: { id: 2 })

job1.queue!
job2.queue!(delay: 1.hour)

# Jobs created but NOT queued to Beanstalkd
job1.bkid  # => nil
job2.bkid  # => nil

Manually execute jobs later

# Later, execute manually
Postburner::Job.perform(job1.id)  # Executes immediately
Postburner::Job.perform(job2.id)  # Time travels to scheduled time

job1.reload.processed_at  # => present
job2.reload.processed_at  # => present

Batch processing pattern

Postburner.null_strategy!

# Create 1000 jobs
jobs = 1000.times.map do |i|
  job = ProcessRecord.create!(args: { record_id: i })
  job.queue!
  job
end

# Execute in batches
jobs.each_slice(100) do |batch|
  batch.each { |job| Postburner::Job.perform(job.id) }
  sleep 1  # Rate limiting
end

See Also:

Class Method Summary collapse

Methods included from TimeHelpers

travel_to

Class Method Details

.handle_perform!(job) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Executes a job with automatic time travel for scheduled jobs.

Called by Job.perform when NullQueue strategy is active. If the job has a future run_at, it automatically travels to that time before execution. Otherwise executes immediately.

Users should call Job.perform instead of this method directly.

Examples:

Execute via Job.perform (recommended)

Postburner.null_strategy!
job = MyJob.create!(args: {})
job.queue!(delay: 1.hour)
Postburner::Job.perform(job.id)  # Delegates to this method
job.reload.processed_at  # => present

Parameters:

Raises:

  • (RuntimeError)

    if ActiveSupport::Testing::TimeHelpers not available

See Also:



120
121
122
123
124
125
126
127
128
129
# File 'lib/postburner/strategies/null_queue.rb', line 120

def handle_perform!(job)
  if job.run_at && job.run_at > Time.current
    travel_to(job.run_at) do
      job.perform!(job.args)
    end
  else
    # No future run_at, execute normally
    job.perform!(job.args)
  end
end

.insert(job, options = {}) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Does NOT insert job into Beanstalkd queue.

Called automatically via after_save_commit hook when Job#queue! is invoked. Unlike other strategies, this does NOT queue to Beanstalkd - it only creates the job record in the database.

Parameters:

  • job (Postburner::Job)

    The job (not queued)

  • options (Hash) (defaults to: {})

    Unused in null mode

Returns:

  • (Hash)

    Status hash with :status => ‘NULL’, :id => nil



90
91
92
93
# File 'lib/postburner/strategies/null_queue.rb', line 90

def insert(job, options = {})
  # Return format matching Beanstalkd response (symbol keys)
  { status: 'NULL', id: nil }
end