class Sidekiq::Repeat::MiniIceCube::MainDsl

Public Class Methods

define_interval_method(name, base, *stars_prefix) click to toggle source
# File lib/sidekiq/repeat/mini_ice_cube.rb, line 51
def define_interval_method(name, base, *stars_prefix)
  define_method(name) do |*args|
    stars = stars_prefix.dup || []

    if args.any?
      interval = args.first.to_i
      unsupported("invalid interval: #{interval}") unless interval && interval >= 0 && interval <= base

      # Instead of always calculating the next() occurrence as ice_cube does,
      # we can only have fixed run times (as per cron syntax). Hence we calculate
      # the run times based on integer division of interval and time frame, using
      # a random start offset based on the remaining time.
      #
      # NOTE: This effectivly means that after the last run in the time frame the
      #       next run will be scheduled after interval+remainder.
      #
      # Example: minutely(17) will run 3 times per hour. After the last run each
      #          hour the skip will be 17 + 9 = 26 minutes. A random offset in
      #          [0,26) will be applied, so a possible cron line could be '4,21,38'.

      times = []
      nruns = (base / interval).floor
      rnoff = rand(interval + base % interval).floor
      runs  = nruns.times.map { |i| i * interval + rnoff }
      stars << runs.map(&:to_s).join(',')
    end

    stars.fill('*', stars.size..4)
    CronSyntax.new(*stars)
  end
end

Public Instance Methods

weekly(*args) click to toggle source
# File lib/sidekiq/repeat/mini_ice_cube.rb, line 84
def weekly(*args)
  unsupported('interval argument unsupported for weekly') unless args.empty?
  CronSyntax.new(0, 3, '*', '*', 0)             # Sundays at 3AM.
end