module SidekiqSimpleDelay::DelayMethods

Aliased class methods to be added to Class

Public Instance Methods

simple_delay(options = {})
simple_delay_for(interval, options = {})
simple_delay_spread(options = {})
simple_delay_until(timestamp, options = {})
simple_delayed_worker() click to toggle source

Tell {DelayMethods} which delayed worker to use

# File lib/sidekiq_simple_delay/delay_methods.rb, line 107
def simple_delayed_worker
  SimpleDelayedWorker
end
simple_sidekiq_delay(options = {}) click to toggle source

Immediately enqueue a job to handle the delayed action

@param options [Hash] options similar to Sidekiq's `perform_async`

# File lib/sidekiq_simple_delay/delay_methods.rb, line 13
def simple_sidekiq_delay(options = {})
  Proxy.new(simple_delayed_worker, self, options)
end
Also aliased as: simple_delay
simple_sidekiq_delay_for(interval, options = {}) click to toggle source

Enqueue a job to handle the delayed action after an elapsed interval

@param interval [#to_f] Number of seconds to wait. `to_f` will be called on

this argument to convert to seconds.

@param options [Hash] options similar to Sidekiq's `perform_in`

# File lib/sidekiq_simple_delay/delay_methods.rb, line 22
def simple_sidekiq_delay_for(interval, options = {})
  Proxy.new(simple_delayed_worker, self, options.merge('at' => Time.now.to_f + interval.to_f))
end
Also aliased as: simple_delay_for
simple_sidekiq_delay_spread(options = {}) click to toggle source

Enqueue a job to handle the delayed action in a given timeframe

@param timestamp [#to_f] Timestamp to execute job at. `to_f` will be called on

this argument to convert to a timestamp.

@param options [Hash] options similar to Sidekiq's `perform_at` @option options [Number] :spread_duration Size of window to spread workers out over @option options [Number] :spread_in Start of window offset from now @option options [Number] :spread_at Start of window offset timestamp @option options [rand|mod] :spread_method perform either a random or modulo spread,

default: *:rand*

@option options [Number] :spread_mod_value value to use for determining mod offset @option options [Symbol] :spread_mod_method method to call to get the value to use

for determining mod offset
# File lib/sidekiq_simple_delay/delay_methods.rb, line 48
def simple_sidekiq_delay_spread(options = {})
  local_opts = options.dup

  spread_duration = Utils.extract_option(local_opts, :spread_duration, 1.hour).to_f
  spread_in = Utils.extract_option(local_opts, :spread_in, 0).to_f
  spread_at = Utils.extract_option(local_opts, :spread_at)
  spread_method = Utils.extract_option(local_opts, :spread_method, :rand)
  spread_mod_value = Utils.extract_option(local_opts, :spread_mod_value)
  spread_mod_method = Utils.extract_option(local_opts, :spread_mod_method)

  spread_duration = 0 if spread_duration < 0
  spread_in = 0 if spread_in < 0

  spread =
    # kick of immediately if the duration is 0
    if spread_duration.zero?
      0
    else
      case spread_method.to_sym
      when :rand
        Utils.random_number(spread_duration)
      when :mod
        mod_value =
          # The mod value has been supplied
          if !spread_mod_value.nil?
            spread_mod_value
          # Call the supplied method on the target object to get mod value
          elsif !spread_mod_method.nil?
            send(spread_mod_method)
          # Call `spread_mod_method` on target object to get mod value
          elsif respond_to?(:spread_mod_method)
            send(send(:spread_mod_method))
          else
            raise ArgumentError, 'must specify `spread_mod_value` or `spread_mod_method` or taget must respond to `spread_mod_method`'
          end

        # calculate the mod based offset
        mod_value % spread_duration
      else
        raise ArgumentError, "spread_method must :rand or :mod, `#{spread_method} is invalid`"
      end
    end

  t =
    if !spread_at.nil?
      # add spread to a timestamp
      spread_at.to_f + spread
    elsif !spread_in.nil?
      # add spread to no plus constant offset
      Time.now.to_f + spread_in.to_f + spread
    else
      # add spread to current time
      Time.now.to_f + spread
    end

  Proxy.new(SimpleDelayedWorker, self, local_opts.merge('at' => t))
end
Also aliased as: simple_delay_spread
simple_sidekiq_delay_until(timestamp, options = {}) click to toggle source

Enqueue a job to handle the delayed action after at a certain time

@param timestamp [#to_f] Timestamp to execute job at. `to_f` will be called on

this argument to convert to a timestamp.

@param options [Hash] options similar to Sidekiq's `perform_at`

# File lib/sidekiq_simple_delay/delay_methods.rb, line 31
def simple_sidekiq_delay_until(timestamp, options = {})
  Proxy.new(simple_delayed_worker, self, options.merge('at' => timestamp.to_f))
end
Also aliased as: simple_delay_until