Class: Delayed::UniqueDelayedJob

Inherits:
Object
  • Object
show all
Defined in:
lib/unique_delayed_job.rb

Overview

allows for specifying additional columns on the delayed_jobs table to help prevent duplicate delayed jobs from being entered into the queue…but still keep an easy interface to enqueuing delayed jobs


Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



99
100
101
# File 'lib/unique_delayed_job.rb', line 99

def columns
  @columns
end

Class Method Details

.call_method(object, method, args_arr, columns = {}) ⇒ Object

factory method to create a new UniqueDelayedJob by specifying a method ton call. will use delayed jobs’ PerformableMethod class to enqueue the job

arguments:

- object: the object (or class or module) on which to call the method
- method: the method to call (specify a symbol or string)
- args_arr: an array of arguments to pass in the method call)
- columns: hash of column names and values to insert into the delayed
           jobs table in addition to the handler



34
35
36
37
38
39
# File 'lib/unique_delayed_job.rb', line 34

def self.call_method(object, method, args_arr, columns = {})
  job = self.new
  job.handler = Delayed::PerformableMethod.new(object, method, args_arr)

  job
end

.run_block(columns = {}, &block) ⇒ Object

factory method to create a UniqueDelayedJob by specifying a block to execute asynchronously arguments:

- columns: hash of column names and values to insert into the delayed
           jobs table in addition to the handler

NOTE: a block is expected




49
50
51
52
53
54
55
# File 'lib/unique_delayed_job.rb', line 49

def self.run_block(columns = {}, &block)
  raise "missing a block in call to run_block" if !block_given?
  job = self.new
  job.handler = Delayed::EvaledJob.new(&block)

  job
end

.use_handler(handler, columns = {}) ⇒ Object

factory method to create a new UniqueDelayedJob from a delayed job handler object (see delayed job documentation for requirements)

arguments:

- handler: the delayed jobs handler object you're using
- columns: hash of column names and values to insert into the delayed
           jobs table in addition to the handler



17
18
19
20
21
22
# File 'lib/unique_delayed_job.rb', line 17

def self.use_handler(handler, columns = {})
  job = self.new
  job.handler = handler

  job
end

Instance Method Details

#add_delayed_jobs_columns(new_columns) ⇒ Object

specify some additional columns to set in the delayed jobs table for this row. be sure that you’ve migrated to add these columns to the delayed jobs table. it is up to you to specify uniqueness constraints on any of the columns you’d like to use to prevent duplicate entries in the delayed jobs table. (it’s also fine for some of these columns to not have unique constraints, though this class will not prevent duplicate values for those and they’ll be for your use for other purposes.)




66
67
68
# File 'lib/unique_delayed_job.rb', line 66

def add_delayed_jobs_columns(new_columns)
  columns.merge! new_columns
end

#enqueue(priority = nil, run_at = nil) ⇒ Object

put the job on the delayed jobs queue. if there already is a row in the delayed jobs table with the same value in any of the unique columns (enforced in the db), then the row will not be inserted

arguments:

priority: 
run_at:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/unique_delayed_job.rb', line 78

def enqueue(priority = nil, run_at = nil)
  cols_to_insert = columns
  cols_to_insert.merge! :priority => priority if priority
  cols_to_insert.merge! :run_at => run_at if run_at
  cols_to_insert.merge! :handler => handler

  # try to catch if this raises an exception because of a duplicate key
  # error this should work for mysql and postgresql which both have the
  # word 'duplicate' followed (not necessarily immediately) by 'key'.
  # ignoring case cause case differs between the two cases
  # if doesn't look like a dupe key error, then reraise the exception
  begin
    Delayed::Job.create(cols_to_insert)
  rescue => e
    if /(duplicate).*(key)/i !~ e.message
      raise e
    end
  end

end