mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
Refactor Worker.set impl so it can be memoized, fixes #3602
This commit is contained in:
parent
de701c4863
commit
39532f8674
4 changed files with 22 additions and 7 deletions
|
@ -5,6 +5,7 @@
|
|||
HEAD
|
||||
-----------
|
||||
|
||||
- Refactor Worker.set so it can be memoized [#3602]
|
||||
- Fix display of Redis URL in web footer, broken in 5.0.3 [#3560]
|
||||
|
||||
5.0.4
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# frozen_string_literal: true
|
||||
module Sidekiq
|
||||
VERSION = "5.0.4"
|
||||
VERSION = "5.0.5"
|
||||
end
|
||||
|
|
|
@ -41,12 +41,13 @@ module Sidekiq
|
|||
# SomeWorker.set(queue: 'foo').perform_async(....)
|
||||
#
|
||||
class Setter
|
||||
def initialize(opts)
|
||||
def initialize(klass, opts)
|
||||
@klass = klass
|
||||
@opts = opts
|
||||
end
|
||||
|
||||
def perform_async(*args)
|
||||
@opts['class'.freeze].client_push(@opts.merge!('args'.freeze => args))
|
||||
@klass.client_push(@opts.merge('args'.freeze => args, 'class'.freeze => @klass))
|
||||
end
|
||||
|
||||
# +interval+ must be a timestamp, numeric or something that acts
|
||||
|
@ -56,10 +57,10 @@ module Sidekiq
|
|||
now = Time.now.to_f
|
||||
ts = (int < 1_000_000_000 ? now + int : int)
|
||||
|
||||
@opts.merge! 'args'.freeze => args, 'at'.freeze => ts
|
||||
payload = @opts.merge('class'.freeze => @klass, 'args'.freeze => args, 'at'.freeze => ts)
|
||||
# Optimization to enqueue something now that is scheduled to go out now or in the past
|
||||
@opts.delete('at'.freeze) if ts <= now
|
||||
@opts['class'.freeze].client_push(@opts)
|
||||
payload.delete('at'.freeze) if ts <= now
|
||||
@klass.client_push(payload)
|
||||
end
|
||||
alias_method :perform_at, :perform_in
|
||||
end
|
||||
|
@ -79,7 +80,7 @@ module Sidekiq
|
|||
end
|
||||
|
||||
def set(options)
|
||||
Setter.new(options.merge!('class'.freeze => self))
|
||||
Setter.new(self, options)
|
||||
end
|
||||
|
||||
def perform_async(*args)
|
||||
|
|
|
@ -240,6 +240,19 @@ class TestClient < Sidekiq::Test
|
|||
Sidekiq.redis {|c| c.flushdb }
|
||||
end
|
||||
|
||||
it 'can be memoized' do
|
||||
q = Sidekiq::Queue.new('bar')
|
||||
assert_equal 0, q.size
|
||||
set = SetWorker.set(queue: :bar, foo: 'qaaz')
|
||||
set.perform_async(1)
|
||||
set.perform_async(1)
|
||||
set.perform_async(1)
|
||||
set.perform_async(1)
|
||||
assert_equal 4, q.size
|
||||
assert_equal 4, q.map{|j| j['jid'] }.uniq.size
|
||||
set.perform_in(10, 1)
|
||||
end
|
||||
|
||||
it 'allows option overrides' do
|
||||
q = Sidekiq::Queue.new('bar')
|
||||
assert_equal 0, q.size
|
||||
|
|
Loading…
Reference in a new issue