Module: SidekiqSimpleDelay::DelayMethods

Defined in:
lib/sidekiq_simple_delay/delay_methods.rb

Overview

Aliased class methods to be added to Class

Instance Method Summary collapse

Instance Method Details

#simple_delayed_workerObject

Tell SidekiqSimpleDelay::DelayMethods which delayed worker to use



107
108
109
# File 'lib/sidekiq_simple_delay/delay_methods.rb', line 107

def simple_delayed_worker
  SimpleDelayedWorker
end

#simple_sidekiq_delay(options = {}) ⇒ Object Also known as: simple_delay

Immediately enqueue a job to handle the delayed action

Parameters:

  • options (Hash) (defaults to: {})

    options similar to Sidekiq’s ‘perform_async`



13
14
15
# File 'lib/sidekiq_simple_delay/delay_methods.rb', line 13

def simple_sidekiq_delay(options = {})
  Proxy.new(simple_delayed_worker, self, options)
end

#simple_sidekiq_delay_for(interval, options = {}) ⇒ Object Also known as: simple_delay_for

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

Parameters:

  • interval (#to_f)

    Number of seconds to wait. ‘to_f` will be called on this argument to convert to seconds.

  • options (Hash) (defaults to: {})

    options similar to Sidekiq’s ‘perform_in`



22
23
24
# 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

#simple_sidekiq_delay_spread(options = {}) ⇒ Object Also known as: simple_delay_spread

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

Parameters:

  • timestamp (#to_f)

    Timestamp to execute job at. ‘to_f` will be called on this argument to convert to a timestamp.

  • options (Hash) (defaults to: {})

    options similar to Sidekiq’s ‘perform_at`

Options Hash (options):

  • :spread_duration (Number)

    Size of window to spread workers out over

  • :spread_in (Number)

    Start of window offset from now

  • :spread_at (Number)

    Start of window offset timestamp

  • :spread_method (rand|mod)

    perform either a random or modulo spread, default: :rand

  • :spread_mod_value (Number)

    value to use for determining mod offset

  • :spread_mod_method (Symbol)

    method to call to get the value to use for determining mod offset



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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

#simple_sidekiq_delay_until(timestamp, options = {}) ⇒ Object Also known as: simple_delay_until

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

Parameters:

  • timestamp (#to_f)

    Timestamp to execute job at. ‘to_f` will be called on this argument to convert to a timestamp.

  • options (Hash) (defaults to: {})

    options similar to Sidekiq’s ‘perform_at`



31
32
33
# 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