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(meta_id, *args)
    heavy_lifting
  end
end

meta0 = MyJob.enqueue('stuff')
meta0.enqueued_at # => 'Wed May 19 13:42:41 -0600 2010'
meta0.meta_id # => '03c9e1a045ad012dd20500264a19273c'
meta0['foo'] = 'bar' # => 'bar'
meta0.save

# later
meta1 = MyJob.get_meta('03c9e1a045ad012dd20500264a19273c')
meta1.job_class # => MyJob
meta1.enqueued_at # => 'Wed May 19 13:42:41 -0600 2010'
meta1['foo'] # => 'bar'

Defined Under Namespace

Classes: Metadata

Constant Summary collapse

Version =
'2.0.1'

Class Method Summary collapse

Instance Method Summary collapse

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 get_meta(meta_id)
  key = "meta:#{meta_id}"
  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 after_perform_meta(meta_id, *args)
  if meta = get_meta(meta_id)
    meta.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 before_perform_meta(meta_id, *args)
  if meta = get_meta(meta_id)
    meta.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)
  meta = Metadata.new({'meta_id' => meta_id(args), 'job_class' => self.to_s})
  meta.save
  Resque.enqueue(self, meta.meta_id, *args)
  meta
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 expire_meta_in
  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 get_meta(meta_id)
  key = "meta:#{meta_id}"
  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 meta_id(*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 on_failure_meta(e, meta_id, *args)
  if meta = get_meta(meta_id)
    meta.fail!
  end
end

#store_meta(meta) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/resque/plugins/meta.rb', line 71

def store_meta(meta)
  key = "meta:#{meta.meta_id}"
  json = Resque.encode(meta.data)
  if meta.expire_at > 0
    Resque.redis.setex(key, meta.expire_at - Time.now.to_i, json)
  else
    Resque.redis.set(key, json)
  end
  meta
end