Class: Postburner::StrictQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/postburner/strategies/strict_queue.rb

Overview

Note:

This is NOT the default strategy - use DefaultQueue for production

Strict production queue strategy that queues jobs to Beanstalkd asynchronously.

This is the strict production strategy that raises an exception if a job is executed before its scheduled run_at time. Unlike DefaultQueue, it does not automatically re-insert premature jobs.

## When to Use StrictQueue

Choose this strategy when you want strict enforcement of job scheduling:

  • You want jobs to fail loudly if executed prematurely

  • You’re debugging scheduling issues

  • You want to catch configuration errors in production

For most production use cases, use DefaultQueue instead, which handles premature execution gracefully by re-inserting with appropriate delay.

## Strategy Behavior

  • Execution: Asynchronous via Beanstalkd workers

  • **Testing mode:** Returns false

  • **Premature execution:** Raises Job::PrematurePerform exception

  • Beanstalkd: Required and used for all job queueing

## Usage

Examples:

Activate StrictQueue strategy

Postburner.strict_strategy!
job = MyJob.create!(args: { user_id: 123 })
job.queue!(delay: 1.hour)

Premature execution raises exception

Postburner.strict_strategy!
job = MyJob.create!(args: {})
job.queue!(delay: 1.hour)
# Worker picks up job too early...
# => raises PrematurePerform: "Job has future run_at: ..."

See Also:

Direct Known Subclasses

DefaultQueue, InlineTestQueue

Class Method Summary collapse

Class Method Details

.handle_perform!(job) ⇒ Object



106
107
108
# File 'lib/postburner/strategies/strict_queue.rb', line 106

def handle_perform!(job)
  job.perform!(job.args)
end

.handle_premature_perform(job) ⇒ void

This method returns an undefined value.

Handles jobs executed before their scheduled run_at time.

This strategy raises an exception to fail loudly on premature execution. Override in subclasses (like NiceQueue) to handle differently.

Parameters:

Raises:



121
122
123
# File 'lib/postburner/strategies/strict_queue.rb', line 121

def handle_premature_perform(job)
  raise Postburner::Job::PrematurePerform, "Job has future run_at: #{job.run_at}"
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.

Inserts job into Beanstalkd queue asynchronously.

Called automatically via after_save_commit hook when Job#queue! is invoked. Enqueues the job to Beanstalkd and returns the response. The bkid is updated by Job#insert! after this returns.

Parameters:

  • job (Postburner::Job)

    The job to insert

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

    Beanstalkd options

Options Hash (options):

  • :delay (Integer)

    Seconds to delay execution

  • :pri (Integer)

    Priority (lower = higher priority)

  • :ttr (Integer)

    Time-to-run before job times out

Returns:

  • (Hash)

    Response with :id (beanstalkd job id) and :status

Raises:

  • (Beaneater::NotConnected)

    if Beanstalkd connection fails



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/postburner/strategies/strict_queue.rb', line 75

def insert(job, options = {})
  #debugger
  Postburner::Job.transaction do
    Postburner.connected do |conn|
      tube_name = job.tube_name
      data = { class: job.class.name, args: [job.id] }

      # Get priority, TTR from job instance (respects instance overrides) or options
      pri = options[:pri] || job.priority || Postburner.configuration.default_priority
      delay = options[:delay].present? ? [0, options[:delay].to_i].max : 0
      ttr = options[:ttr] || job.ttr || Postburner.configuration.default_ttr
      payload = JSON.generate(data)

      begin
        response = conn.tubes[tube_name].put(
          payload,
          pri: pri,
          delay: delay,
          ttr: ttr
        )
      rescue Beaneater::BadFormatError => e
        raise Postburner::Job::BadFormat.new(
          "Beanstalkd BAD_FORMAT: tube=#{tube_name} payload=#{payload} pri=#{pri.inspect} delay=#{delay.inspect} ttr=#{ttr.inspect}"
        ), cause: e
      end

      response
    end
  end
end

.testingBoolean

Returns whether this strategy is for testing.

Returns:

  • (Boolean)

    Always false for production strategies



53
54
55
# File 'lib/postburner/strategies/strict_queue.rb', line 53

def testing
  false
end