-
-
Save virtualstaticvoid/4110001 to your computer and use it in GitHub Desktop.
track meta data with resque jobs, like when it was queued.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Resque | |
def push_with_meta(queue, item) | |
if item.respond_to?(:[]=) | |
item[:meta] = {:queued_at => Time.now.to_f} | |
end | |
push_without_meta(queue, item) | |
end | |
class Job | |
# Returns a Hash of the meta data related to this Job. | |
def metadata | |
@metadata ||= payload['meta'] || {} | |
end | |
# Returns the Time that this Job was queued, or nil. | |
def queued_at | |
@queued_at ||= metadata.key?('queued_at') ? Time.at(metadata['queued_at']) : nil | |
end | |
# Ugly way of wrapping around #perform. Resque has a before_perform hook, | |
# but it only passes the arguments instead of the job object. There's no | |
# other way to get to the job metadata. | |
def perform_with_metrics | |
if ts = queued_at | |
ActiveSupport::Notifications.publish("queued.resque", | |
ts, Time.now, SecureRandom.hex(10), | |
:queue => queue, :class => self.class.name) | |
end | |
perform_without_metrics | |
end | |
alias perform_without_metrics perform | |
alias perform perform_with_metrics | |
end | |
end | |
# Doing this in the model affected the module's instance methods, even though | |
# they're used like class methods because of `extend self`. | |
class << Resque | |
alias push_without_meta push | |
alias push push_with_meta | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment