Class: DeferredJob::Job

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, klass, *args) ⇒ Job

Initialize a new DeferredJob

Parameters:

  • id (String)
    • The ID of the job
  • klass (Class, String)
    • The class to run
  • args (Array)
    • The arguments for the job


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

.adapterObject



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

Parameters:

  • value

    the value to set the attribute key_lambda to.



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

Parameters:

  • value

    the value to set the attribute redis to.



141
142
143
# File 'lib/deferred_job.rb', line 141

def redis=(value)
  @redis = value
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



22
23
24
# File 'lib/deferred_job.rb', line 22

def args
  @args
end

#idObject (readonly)

Returns the value of attribute id.



22
23
24
# File 'lib/deferred_job.rb', line 22

def id
  @id
end

#klassObject (readonly)

Returns the value of attribute klass.



22
23
24
# File 'lib/deferred_job.rb', line 22

def klass
  @klass
end

#set_keyObject (readonly)

Returns the value of attribute set_key.



22
23
24
# File 'lib/deferred_job.rb', line 22

def set_key
  @set_key
end

#verboseObject

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

Parameters:

  • id (Object)
    • the id of the job
  • klass (Class, String)
    • The class of the job to run
  • args (Array)
    • The args to send to the job

Returns:



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

Parameters:

  • id (Object)
    • the id of the job to lookup

Returns:

  • (Boolean)
    • whether or not the 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

Parameters:

  • id (Object)
    • the id of the job

Returns:



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

Parameters:

  • id (Object)
    • the id of the job

Returns:

  • (String)
    • the set_key to use for the given id


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

Returns:

  • (Redis::Client)
    • a redis client


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

#clearObject

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

#countFixnum

Count the number of elements in the set

Returns:

  • (Fixnum)

    the count of the 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

#destroyObject

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

Parameters:

  • things (Array)
    • The things to remove

Returns:

  • (Fixnum)

    the number of things removed



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

Returns:

  • (Boolean)

    whether or not 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

Parameters:

  • things (Array)
    • The things to add

Returns:

  • (Fixnum)

    the number of things added



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_forObject



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

Returns:

  • (Boolean)


71
72
73
# File 'lib/deferred_job.rb', line 71

def waiting_for?(thing)
  with_redis { |redis| redis.sismember(@set_key, thing) }
end