Module: Gzr::Cron

Included in:
Gzr::Commands::Alert::Randomize, Gzr::Commands::Plan::Randomize
Defined in:
lib/gzr/modules/cron.rb

Instance Method Summary collapse

Instance Method Details

#randomize_cron(crontab, window = 60) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gzr/modules/cron.rb', line 26

def randomize_cron(crontab, window=60)
  cronfields = crontab.split(%r{\s+})
  minute = cronfields[0].to_i
  hour = cronfields[1].to_i
  factor = rand(window) - (window/2)
  minute = minute + factor
  if minute < 0
    hour = hour - 1
    minute = minute + 60
  end
  if hour < 0
    hour = 23
  end
  if minute > 59
    hour = hour + 1
    minute = minute - 60
  end
  if hour > 23
    hour = 0
  end
  cronfields[0] = minute
  cronfields[1] = hour if /^[[:digit:]]+$/.match? cronfields[1]
  return cronfields.join(' ')
end