Module: SidekiqUtils::RedisMonitorStorage

Defined in:
lib/sidekiq_utils/redis_monitor_storage.rb

Class Method Summary collapse

Class Method Details

.active_job?(job) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/sidekiq_utils/redis_monitor_storage.rb', line 76

def active_job?(job)
  job['class'] == "ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper"
end

.add_first_argument_to_job_key(*klasses) ⇒ Object



4
5
6
7
# File 'lib/sidekiq_utils/redis_monitor_storage.rb', line 4

def add_first_argument_to_job_key(*klasses)
  @first_argument_to_job_key_for ||= []
  @first_argument_to_job_key_for |= klasses
end

.arguments(job) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/sidekiq_utils/redis_monitor_storage.rb', line 68

def arguments(job)
  if active_job?(job)
    job['args'].first['arguments']
  else
    job['args']
  end
end

.full_prefix(job, prefix = nil, last_prefix = nil) ⇒ Object



39
40
41
42
43
# File 'lib/sidekiq_utils/redis_monitor_storage.rb', line 39

def full_prefix(job, prefix = nil, last_prefix = nil)
  job_prefix = job_prefix(job)
  full_prefix = [job_prefix, prefix, Date.today.to_s(:medium), last_prefix]
  full_prefix.compact.join('||')
end

.job_prefix(job, unwrap_arguments: false) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sidekiq_utils/redis_monitor_storage.rb', line 45

def job_prefix(job, unwrap_arguments: false)
  arguments = arguments(job)
  if unwrap_arguments
    arguments = arguments.
      map {|arg| SidekiqUtils::AdditionalSerialization.unwrap_argument(arg) }
  end

  if active_job?(job)
    job_prefix = job['wrapped']
  else
    job_prefix = job['class']
  end

  case job_prefix
  when 'ActionMailer::DeliveryJob'
    job_prefix += "[#{arguments[0..1].join('#')}]"
  when *(@first_argument_to_job_key_for.to_a)
    job_prefix += "[#{arguments[0]}]"
  end

  job_prefix
end

.retrieve(top_level_key, prefix) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sidekiq_utils/redis_monitor_storage.rb', line 18

def retrieve(top_level_key, prefix)
  data = {}
  Sidekiq.redis {|r| r.hgetall(top_level_key) }.each do |key, value|
    (job, prefix_type, date, value_type) = key.split('||')
    next unless prefix_type == prefix

    if Date.parse(date) < 1.week.ago
      # expired data, get rid of it
      Sidekiq.redis {|r| r.hdel(top_level_key, key) }
    else
      data[job] ||= { 'sum' => 0, 'count' => 0 }
      data[job][value_type] += value.to_i
    end
  end

  data.each do |job, values|
    values['average'] = (values['sum'].to_f / values['count'].to_i).round
  end
  data
end

.store(key, prefix, job, value) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/sidekiq_utils/redis_monitor_storage.rb', line 9

def store(key, prefix, job, value)
  Sidekiq.redis do |redis|
    redis.multi do
      redis.hincrby(key, full_prefix(job, prefix, 'sum'), value)
      redis.hincrby(key, full_prefix(job, prefix, 'count'), 1)
    end
  end
end