Class: DeferredJob::Job
- Inherits:
-
Object
- Object
- DeferredJob::Job
- Defined in:
- lib/deferred_job.rb
Class Attribute Summary collapse
- .adapter ⇒ Object
-
.key_lambda ⇒ Object
writeonly
Sets the attribute key_lambda.
-
.redis ⇒ Object
writeonly
Sets the attribute redis.
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#set_key ⇒ Object
readonly
Returns the value of attribute set_key.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Class Method Summary collapse
-
.create(id, klass, *args) ⇒ DeferredJob
Create a new DeferredJob.
-
.exists?(id) ⇒ Boolean
Determine if a give job exists.
-
.find(id) ⇒ DeferredJob
Find an existing DeferredJob.
-
.key_for(id) ⇒ String
The way we turn id into set_key.
-
.with_redis(&block) ⇒ Redis::Client
Our own redis instance in case people want to separate from the message processor.
Instance Method Summary collapse
-
#clear ⇒ Object
Clear all entries in the set.
-
#count ⇒ Fixnum
Count the number of elements in the set.
-
#destroy ⇒ Object
Clear and then remove the key for this job.
-
#done(*things) ⇒ Fixnum
Mark a thing as finished NOTE >= 2.4 should use srem with multiple things.
-
#empty? ⇒ Boolean
Determine if the set is empty.
-
#initialize(id, klass, *args) ⇒ Job
constructor
Initialize a new DeferredJob.
-
#wait_for(*things) ⇒ Fixnum
Wait for a thing before continuing NOTE >= 2.4 should use sadd with multiple things.
- #waiting_for ⇒ Object
- #waiting_for?(thing) ⇒ Boolean
Constructor Details
#initialize(id, klass, *args) ⇒ Job
Initialize a new DeferredJob
28 29 30 31 32 33 |
# File 'lib/deferred_job.rb', line 28 def initialize(id, klass, *args) @id = id @set_key = self.class.key_for id @klass = klass.is_a?(String) ? klass.constantize : klass @args = args end |
Class Attribute Details
.adapter ⇒ Object
196 197 198 199 200 201 202 203 204 |
# File 'lib/deferred_job.rb', line 196 def adapter @adapter_instance ||= begin if @adapter.nil? || @adapter == :resque ResqueAdapter.new elsif @adapter == :sidekiq SidekiqAdapter.new end end end |
.key_lambda=(value) ⇒ Object (writeonly)
Sets the attribute key_lambda
141 142 143 |
# File 'lib/deferred_job.rb', line 141 def key_lambda=(value) @key_lambda = value end |
.redis=(value) ⇒ Object (writeonly)
Sets the attribute redis
141 142 143 |
# File 'lib/deferred_job.rb', line 141 def redis=(value) @redis = value end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
22 23 24 |
# File 'lib/deferred_job.rb', line 22 def args @args end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
22 23 24 |
# File 'lib/deferred_job.rb', line 22 def id @id end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
22 23 24 |
# File 'lib/deferred_job.rb', line 22 def klass @klass end |
#set_key ⇒ Object (readonly)
Returns the value of attribute set_key.
22 23 24 |
# File 'lib/deferred_job.rb', line 22 def set_key @set_key end |
#verbose ⇒ Object
Returns the value of attribute verbose.
21 22 23 |
# File 'lib/deferred_job.rb', line 21 def verbose @verbose end |
Class Method Details
.create(id, klass, *args) ⇒ DeferredJob
Create a new DeferredJob
156 157 158 159 160 161 162 163 |
# File 'lib/deferred_job.rb', line 156 def create(id, klass, *args) plan = [klass.to_s, args] with_redis { |redis| redis.set(id, MultiJson.encode(plan)) } # Return the job job = new(id, klass, *args) job.clear job end |
.exists?(id) ⇒ Boolean
Determine if a give job exists
182 183 184 |
# File 'lib/deferred_job.rb', line 182 def exists?(id) with_redis { |redis| !redis.get(id).nil? } end |
.find(id) ⇒ DeferredJob
Find an existing DeferredJob
168 169 170 171 172 173 174 175 176 177 |
# File 'lib/deferred_job.rb', line 168 def find(id) plan_data = with_redis { |redis| redis.get(id) } # If found, return the job, otherwise raise NoSuchJob if plan_data.nil? raise NoSuchJob.new "No Such DeferredJob: #{id}" else plan = MultiJson.decode(plan_data) new(id, plan.first, *plan.last) end end |
.key_for(id) ⇒ String
The way we turn id into set_key
146 147 148 149 |
# File 'lib/deferred_job.rb', line 146 def key_for(id) lamb = @key_lambda || lambda { |id| "deferred-job:#{id}" } lamb.call id end |
.with_redis(&block) ⇒ Redis::Client
Our own redis instance in case people want to separate from the message processor
188 189 190 191 192 193 194 |
# File 'lib/deferred_job.rb', line 188 def with_redis(&block) if @redis block.call(@redis) else adapter.with_redis(&block) end end |
Instance Method Details
#clear ⇒ Object
Clear all entries in the set
36 37 38 |
# File 'lib/deferred_job.rb', line 36 def clear with_redis { |redis| redis.del(@set_key) } end |
#count ⇒ Fixnum
Count the number of elements in the set
56 57 58 |
# File 'lib/deferred_job.rb', line 56 def count with_redis { |redis| redis.scard(@set_key).to_i } end |
#destroy ⇒ Object
Clear and then remove the key for this job
41 42 43 44 45 46 |
# File 'lib/deferred_job.rb', line 41 def destroy with_redis do |redis| redis.del(@set_key) redis.del(@id) end end |
#done(*things) ⇒ Fixnum
Mark a thing as finished NOTE >= 2.4 should use srem with multiple things
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 |
# File 'lib/deferred_job.rb', line 83 def done(*things) results = nil with_redis do |redis| results = redis.multi do redis.scard @set_key things.each do |thing| log "DeferredJob #{id} done with #{thing.inspect}" redis.srem @set_key, thing end redis.scard @set_key end end if results.first > 0 if results.last.zero? log "DeferredJob #{id} all conditions met; will now self-destruct" begin execute ensure destroy end else log "DeferredJob #{id} waiting on #{results.last} conditions" end end end |
#empty? ⇒ Boolean
Determine if the set is empty
50 51 52 |
# File 'lib/deferred_job.rb', line 50 def empty? count == 0 end |
#wait_for(*things) ⇒ Fixnum
Wait for a thing before continuing NOTE >= 2.4 should use sadd with multiple things
64 65 66 67 68 69 |
# File 'lib/deferred_job.rb', line 64 def wait_for(*things) things.each do |thing| log "DeferredJob #{@id} will wait for #{thing.inspect}" with_redis { |redis| redis.sadd @set_key, thing } end end |
#waiting_for ⇒ Object
75 76 77 |
# File 'lib/deferred_job.rb', line 75 def waiting_for with_redis { |redis| redis.smembers(@set_key) } end |
#waiting_for?(thing) ⇒ Boolean
71 72 73 |
# File 'lib/deferred_job.rb', line 71 def waiting_for?(thing) with_redis { |redis| redis.sismember(@set_key, thing) } end |