Module: Resque::Plugins::Stats
- Includes:
- Helpers
- Defined in:
- lib/resque/plugins/stats.rb
Overview
If you want to keep track of the number of executions per Job, extend it with this module.
Example:
require 'resque/plugins/stats'
class HardJob
extend Resque::Plugins::Stats
@queue = :hard_job
def self.perform(something)
do_work
end
end
This will keep a historical count of jobs executed hourly, daily and monthly.
Hourly records will only persist for 24 hours, daily will be available for one month, and monthly counters will be there forever.
Instance Method Summary collapse
-
#after_perform_do_stats(*args) ⇒ Object
Hook that will increment the counters after the job has been executed.
-
#daily ⇒ Object
Returns an array of executed jobs daily for the current month.
-
#hourly ⇒ Object
Returns an array of executed jobs hourly for today.
-
#monthly ⇒ Object
Returns a hash where the key is the year/month pair and the value is the count of executed jobs during that month.
Instance Method Details
#after_perform_do_stats(*args) ⇒ Object
Hook that will increment the counters after the job has been executed. Also, will set expiration dates for the hourly and daily counters.
28 29 30 31 32 |
# File 'lib/resque/plugins/stats.rb', line 28 def after_perform_do_stats(*args) Stats.increx hourly_key, 3600 * 24 # 24h Stats.increx daily_key, 3600 * 24 * 31 # 31d redis.incr monthly_key end |
#daily ⇒ Object
Returns an array of executed jobs daily for the current month. Indexes go from 0 (unused) to current_month.days_in_month (max 31).
42 43 44 |
# File 'lib/resque/plugins/stats.rb', line 42 def daily (0..Stats.days_in_month).collect { |d| redis.get("#{prefix_daily}:#{Time.now.year}:#{Time.now.month}:#{d}").to_i } end |
#hourly ⇒ Object
Returns an array of executed jobs hourly for today. Indexes go from 0 to 23 hour.
36 37 38 |
# File 'lib/resque/plugins/stats.rb', line 36 def hourly (0..23).collect { |h| redis.get("#{prefix_hourly}:#{Time.now.year}:#{Time.now.month}:#{Time.now.day}:#{h}").to_i } end |
#monthly ⇒ Object
Returns a hash where the key is the year/month pair and the value is the count of executed jobs during that month. The format of the key is “year:month” with 4 digit year and 1/2 digit month.
49 50 51 52 |
# File 'lib/resque/plugins/stats.rb', line 49 def monthly keys = redis.keys("#{prefix_monthly}:*") keys.zip(redis.mget(keys)).inject({}) { |t,p| t.merge(p.first.sub("#{prefix_monthly}:", '') => p.last.to_i) } end |