Module: Postburner::Insertion
- Extended by:
- ActiveSupport::Concern
- Included in:
- Job
- Defined in:
- app/concerns/postburner/insertion.rb
Overview
Concern providing queue insertion methods for Postburner jobs.
Handles enqueuing jobs to Beanstalkd (or test strategies), including scheduling, re-queueing, and the internal insertion mechanics via after_save_commit callbacks.
Instance Method Summary collapse
-
#queue!(options = {}) ⇒ true
Enqueues the job to Beanstalkd for processing.
-
#requeue!(options = {}) ⇒ true
Re-queues an existing job by removing it from Beanstalkd and queueing again.
-
#will_insert? ⇒ Boolean
private
Checks if job is flagged for insertion into Beanstalkd.
Instance Method Details
#queue!(options = {}) ⇒ true
Enqueues the job to Beanstalkd for processing.
Sets queued_at timestamp and optionally run_at for scheduled execution. Triggers enqueue callbacks and inserts job into Beanstalkd via after_save_commit hook. In test mode, executes immediately instead of queueing to Beanstalkd.
71 72 73 74 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 105 106 107 108 109 |
# File 'app/concerns/postburner/insertion.rb', line 71 def queue!(={}) return true if self.queued_at.present? && self.bkid.present? raise ActiveRecord::RecordInvalid, "Can't queue unless valid." unless self.valid? raise AlreadyProcessed, "Processed at #{self.processed_at}" if self.processed_at # Extract and set instance-level overrides self.priority = .delete(:priority) if .key?(:priority) self.ttr = .delete(:ttr) if .key?(:ttr) self.queue_name = .delete(:queue) if .key?(:queue) at = .delete(:at) now = Time.current self.queued_at = now self.run_at = case when at.present? # this is rudimentary, add error handling # # Ceil, don't floor. Beanstalkd delays are whole seconds, # so some rounding is unavoidable -- but it must round # *away* from now. Flooring both sides (`at.to_i - # now.to_i`) drops frac(at) and frac(now) independently, # releasing the job early by frac(at) - frac(now) # whenever that is positive. Consumers then see a job # arrive before its own run_at. [:delay] ||= [(at.to_time - now).ceil, 0].max at when [:delay].present? now + [:delay].seconds end @_insert_options = run_callbacks :enqueue do self.save! end true end |
#requeue!(options = {}) ⇒ true
Re-queues an existing job by removing it from Beanstalkd and queueing again.
Calls #delete! to remove from Beanstalkd, resets queuing metadata, then calls #queue! with new options.
134 135 136 137 138 139 |
# File 'app/concerns/postburner/insertion.rb', line 134 def requeue!(={}) self.delete! self.bkid, self.queued_at = nil, nil self.queue! end |
#will_insert? ⇒ Boolean
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.
Checks if job is flagged for insertion into Beanstalkd.
Set internally by #queue! to trigger insertion via after_save_commit hook.
148 149 150 |
# File 'app/concerns/postburner/insertion.rb', line 148 def will_insert? @_insert_options.is_a? Hash end |