Module: Resque::Plugins::Meta
- Defined in:
- lib/resque/plugins/meta.rb,
lib/resque/plugins/meta/version.rb,
lib/resque/plugins/meta/metadata.rb
Overview
If you want to be able to add metadata for a job to track anything you want, extend it with this module.
For example:
require 'resque-meta'
class MyJob
extend Resque::Plugins::Meta
def self.perform(, *args)
heavy_lifting
end
end
= MyJob.enqueue('stuff')
.enqueued_at # => 'Wed May 19 13:42:41 -0600 2010'
. # => '03c9e1a045ad012dd20500264a19273c'
['foo'] = 'bar' # => 'bar'
.save
# later
= MyJob.('03c9e1a045ad012dd20500264a19273c')
.job_class # => MyJob
.enqueued_at # => 'Wed May 19 13:42:41 -0600 2010'
['foo'] # => 'bar'
Defined Under Namespace
Classes: Metadata
Constant Summary collapse
- Version =
'2.0.1'
Class Method Summary collapse
-
.get_meta(meta_id) ⇒ Object
Retrieve the metadata for a given job.
Instance Method Summary collapse
- #after_perform_meta(meta_id, *args) ⇒ Object
-
#before_finish_expire_in ⇒ Object
Override in your job to control how many seconds a job's metadata will live after it is enqueued and before it finishes.
- #before_perform_meta(meta_id, *args) ⇒ Object
-
#enqueue(*args) ⇒ Object
Enqueues a job in Resque and return the association metadata.
-
#expire_meta_in ⇒ Object
Override in your job to control the how many seconds a job's metadata will live after it finishes.
-
#get_meta(meta_id) ⇒ Object
Retrieve the metadata for a given job.
-
#meta_id(*args) ⇒ Object
Override in your job to control the metadata id.
- #on_failure_meta(e, meta_id, *args) ⇒ Object
- #store_meta(meta) ⇒ Object
Class Method Details
.get_meta(meta_id) ⇒ Object
Retrieve the metadata for a given job. If you call this from a class that extends Meta, then the metadata will only be returned if the metadata for that id is for the same class. Explicitly, calling Meta.get_meta(some_id) will return the metadata for a job of any type.
87 88 89 90 91 92 93 94 95 |
# File 'lib/resque/plugins/meta.rb', line 87 def () key = "meta:#{}" if json = Resque.redis.get(key) data = Resque.decode(json) if self == Meta || self.to_s == data['job_class'] Metadata.new(data) end end end |
Instance Method Details
#after_perform_meta(meta_id, *args) ⇒ Object
105 106 107 108 109 |
# File 'lib/resque/plugins/meta.rb', line 105 def (, *args) if = () .finish! end end |
#before_finish_expire_in ⇒ Object
Override in your job to control how many seconds a job's metadata will live after it is enqueued and before it finishes. This includes the condition where the job and metadata are never finished because of a shutdown or server crash. Defaults to 0 (i.e. forever). Return nil or 0 to set them to never expire. Use caution: this value should be longer than your jobs take to run!
57 58 59 |
# File 'lib/resque/plugins/meta.rb', line 57 def before_finish_expire_in 0 end |
#before_perform_meta(meta_id, *args) ⇒ Object
99 100 101 102 103 |
# File 'lib/resque/plugins/meta.rb', line 99 def (, *args) if = () .start! end end |
#enqueue(*args) ⇒ Object
Enqueues a job in Resque and return the association metadata. The meta_id in the returned object can be used to fetch the metadata again in the future.
64 65 66 67 68 69 |
# File 'lib/resque/plugins/meta.rb', line 64 def enqueue(*args) = Metadata.new({'meta_id' => (args), 'job_class' => self.to_s}) .save Resque.enqueue(self, ., *args) end |
#expire_meta_in ⇒ Object
Override in your job to control the how many seconds a job's metadata will live after it finishes. Defaults to 24 hours. Return nil or 0 to set them to never expire.
46 47 48 |
# File 'lib/resque/plugins/meta.rb', line 46 def 24 * 60 * 60 end |
#get_meta(meta_id) ⇒ Object
Retrieve the metadata for a given job. If you call this from a class that extends Meta, then the metadata will only be returned if the metadata for that id is for the same class. Explicitly, calling Meta.get_meta(some_id) will return the metadata for a job of any type.
87 88 89 90 91 92 93 94 95 |
# File 'lib/resque/plugins/meta.rb', line 87 def () key = "meta:#{}" if json = Resque.redis.get(key) data = Resque.decode(json) if self == Meta || self.to_s == data['job_class'] Metadata.new(data) end end end |
#meta_id(*args) ⇒ Object
Override in your job to control the metadata id. It is
passed the same arguments as perform, that is, your job's
payload.
39 40 41 |
# File 'lib/resque/plugins/meta.rb', line 39 def (*args) Digest::SHA1.hexdigest([ Time.now.to_f, rand, self, args ].join) end |
#on_failure_meta(e, meta_id, *args) ⇒ Object
111 112 113 114 115 |
# File 'lib/resque/plugins/meta.rb', line 111 def (e, , *args) if = () .fail! end end |