wisper-sidekiq 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb37a450ee5d55ff37de43045cd8353fc06b7d67efe5897e64d3030b46ab7928
4
- data.tar.gz: f260f1226757bb161ac085c335eebed6fc3769439cd9b5cb65cb2e3f183c8ffc
3
+ metadata.gz: c1c3632db962602de97c98913fb5148c6c4e6c461360663f5f06a839c9aea465
4
+ data.tar.gz: 10c3e25b504ca1760b62742102ac435c3407020cd02b675644e6fa3e2a08ea6f
5
5
  SHA512:
6
- metadata.gz: 3fa35e0482ac3c97176af2544573667a564254c84ace2d85cd6053ec5f44c898d79d741c3665dba67c91d9d82162b8e6b70ea7c8811fc8e9ee3ba07ac40efbb1
7
- data.tar.gz: 65052d9323080e139c55f481edc9e8be0647468466198260e6fd9ea71d411c59c1d266c75238e24d6985c55088aa1355367c91a878986b9939deb6cc6a501ad6
6
+ metadata.gz: cc4efae8cbd60c7bbbb23d3091607538e0b0208d2e7ad430d25cb232a29b6a8abef73fe7eb32ca0382aaca00d97d8ce77a5f5749a12ac10bcd8c6ff7e1ef8d2a
7
+ data.tar.gz: eb59410050d965537b3113289edb6c9796745c6e0b70161a756f89d6eaf734cabbf34ac2f80edf83beb45b28d0271832aa3ae94513836de4c437295ec2dd8761
data/.gitignore CHANGED
@@ -23,4 +23,5 @@ tmp
23
23
  mkmf.log
24
24
  .ruby-version
25
25
  .ruby-gemset
26
- .idea
26
+ .idea
27
+ .gs
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [1.3.0] - 25/Nov/2019
4
+
5
+ - Add the ability to pass `sidekiq_schedule_options` options in order to schedule jobs to be run in the future
6
+
3
7
  ## [1.2.0]
4
8
 
5
9
  - fixes: gemspec does not allow sidekiq 5.x to be used
data/Gemfile CHANGED
@@ -7,6 +7,8 @@ gem 'rake'
7
7
  gem 'rspec'
8
8
  gem 'coveralls', require: false
9
9
 
10
+ gem 'redis', '<= 4.0.3' if RUBY_VERSION < '2.3'
11
+
10
12
  gem 'psych', platforms: :rbx
11
13
 
12
14
  group :extras do
data/README.md CHANGED
@@ -10,8 +10,16 @@ publishing using [Sidekiq](https://github.com/mperham/sidekiq).
10
10
 
11
11
  ## Installation
12
12
 
13
+ ### Sidekiq 5+
14
+
13
15
  ```ruby
14
- gem 'wisper-sidekiq'
16
+ gem 'wisper-sidekiq', '~> 1.0'
17
+ ```
18
+
19
+ ### Sidekiq 4-
20
+
21
+ ```ruby
22
+ gem 'wisper-sidekiq', '~> 0.0'
15
23
  ```
16
24
 
17
25
  ## Usage
@@ -32,7 +40,7 @@ class MyListener
32
40
  end
33
41
  ```
34
42
 
35
- When publshing events the arguments must be simple as they need to be
43
+ When publishing events the arguments must be simple as they need to be
36
44
  serialized. For example instead of sending an `ActiveRecord` model as an argument
37
45
  use its id instead.
38
46
 
@@ -43,6 +51,40 @@ for more information.
43
51
 
44
52
  In order to define custom [sidekiq_options](https://github.com/mperham/sidekiq/wiki/Advanced-Options#workers) you can add `sidekiq_options` class method in your subscriber definition - those options will be passed to Sidekiq's `set` method just before scheduling the asynchronous worker.
45
53
 
54
+ ### Passing down schedule options
55
+
56
+ In order be able to schedule jobs to be run in the future following [Scheduled Jobs](https://github.com/mperham/sidekiq/wiki/Scheduled-Jobs) you can add `sidekiq_schedule_options` class method in your subscriber definition - those options will be passed to Sidekiq's `perform_in` method when the worker is called.
57
+
58
+ This feature is not as powerfull as Sidekiq's API that allows you to set this on every job enqueue, in this case you're able to set this for the hole listener class like:
59
+ ```ruby
60
+ class MyListener
61
+ #...
62
+
63
+ def self.sidekiq_schedule_options
64
+ { perform_in: 5 }
65
+ end
66
+
67
+ #...
68
+ end
69
+ ```
70
+ Or you can set this per event (method called on the listener), like so:
71
+ ```ruby
72
+ class MyListener
73
+ #...
74
+
75
+ def self.sidekiq_schedule_options
76
+ { event_name: { perform_in: 5 } }
77
+ end
78
+
79
+ def self.event_name
80
+ #...
81
+ end
82
+
83
+ #...
84
+ end
85
+ ```
86
+ In both cases there is also available the `perform_at` option.
87
+
46
88
  ## Compatibility
47
89
 
48
90
  The same Ruby versions as Sidekiq are offically supported, but it should work
@@ -59,5 +101,5 @@ bundle exec rspec
59
101
 
60
102
  ## Contributing
61
103
 
62
- To run sidekiq use `scripts/sidekiq`. This wraps sidekiq in [rerun](https://github.com/alexch/rerun)
104
+ To run sidekiq use `scripts/sidekiq`. This wraps sidekiq in [rerun](https://github.com/alexch/rerun)
63
105
  which will restart sidekiq when `specs/dummy_app` changes.
data/Rakefile CHANGED
@@ -1,2 +1 @@
1
1
  require "bundler/gem_tasks"
2
-
@@ -28,7 +28,12 @@ module Wisper
28
28
 
29
29
  def broadcast(subscriber, publisher, event, args)
30
30
  options = sidekiq_options(subscriber)
31
- Worker.set(options).perform_async(::YAML.dump([subscriber, event, args]))
31
+ schedule_options = sidekiq_schedule_options(subscriber, event)
32
+
33
+ Worker.set(options).perform_in(
34
+ schedule_options.fetch(:delay, 0),
35
+ ::YAML.dump([subscriber, event, args])
36
+ )
32
37
  end
33
38
 
34
39
  private
@@ -36,6 +41,24 @@ module Wisper
36
41
  def sidekiq_options(subscriber)
37
42
  subscriber.respond_to?(:sidekiq_options) ? subscriber.sidekiq_options : {}
38
43
  end
44
+
45
+ def sidekiq_schedule_options(subscriber, event)
46
+ return {} unless subscriber.respond_to?(:sidekiq_schedule_options)
47
+
48
+ options = subscriber.sidekiq_schedule_options
49
+
50
+ if options.has_key?(event.to_sym)
51
+ delay_option(options[event.to_sym])
52
+ else
53
+ delay_option(options)
54
+ end
55
+ end
56
+
57
+ def delay_option(options)
58
+ return {} unless options.key?(:perform_in) || options.key?(:perform_at)
59
+
60
+ { delay: options[:perform_in] || options[:perform_at] }
61
+ end
39
62
  end
40
63
  end
41
64
 
@@ -1,5 +1,5 @@
1
1
  module Wisper
2
2
  module Sidekiq
3
- VERSION = "1.2.0"
3
+ VERSION = "1.3.0"
4
4
  end
5
5
  end
@@ -23,6 +23,60 @@ RSpec.describe Wisper::SidekiqBroadcaster do
23
23
  end
24
24
  end
25
25
 
26
+ class CustomizedScheduleInJobSubscriberUnderTest
27
+ def self.it_happened
28
+ end
29
+
30
+ def self.sidekiq_schedule_options
31
+ { perform_in: 5 }
32
+ end
33
+ end
34
+
35
+ class CustomizedEventScheduleInJobSubscriberUnderTest
36
+ def self.it_happened
37
+ end
38
+
39
+ def self.sidekiq_schedule_options
40
+ { it_happened: { perform_in: 5 } }
41
+ end
42
+ end
43
+
44
+ class CustomizedScheduleAtJobSubscriberUnderTest
45
+ def self.it_happened
46
+ end
47
+
48
+ def self.sidekiq_schedule_options
49
+ { perform_at: Time.now + 5 }
50
+ end
51
+ end
52
+
53
+ class CustomizedEventScheduleAtJobSubscriberUnderTest
54
+ def self.it_happened
55
+ end
56
+
57
+ def self.sidekiq_schedule_options
58
+ { it_happened: { perform_at: Time.now + 5 } }
59
+ end
60
+ end
61
+
62
+ class CustomizedBadScheduleInJobSubscriberUnderTest
63
+ def self.it_happened
64
+ end
65
+
66
+ def self.sidekiq_schedule_options
67
+ { perform_in: 'not a number', delay: 5 }
68
+ end
69
+ end
70
+
71
+ class CustomizedBadDefaultScheduleInWithEventScheduleAtJobSubscriberUnderTest
72
+ def self.it_happened
73
+ end
74
+
75
+ def self.sidekiq_schedule_options
76
+ { perform_in: 'not a number', delay: 5, it_happened: { perform_at: Time.now + 5 } }
77
+ end
78
+ end
79
+
26
80
  let(:publisher) { PublisherUnderTest.new }
27
81
 
28
82
  before { Sidekiq::Testing.fake! }
@@ -36,6 +90,38 @@ RSpec.describe Wisper::SidekiqBroadcaster do
36
90
  .to change(Sidekiq::Queues["default"], :size).by(1)
37
91
  end
38
92
 
93
+ it 'schedules to run in some time a sidekiq job' do
94
+ publisher.subscribe(CustomizedScheduleInJobSubscriberUnderTest, async: described_class.new)
95
+
96
+ # In order to look into Sidekiq::ScheduledSet we need to hit redis
97
+ expect { publisher.run }
98
+ .to change { Sidekiq::Queues["default"].select{|job| job.key?('at')}.size }.by(1)
99
+ end
100
+
101
+ it 'schedules to run in some time a sidekiq job for an event' do
102
+ publisher.subscribe(CustomizedEventScheduleInJobSubscriberUnderTest, async: described_class.new)
103
+
104
+ # In order to look into Sidekiq::ScheduledSet we need to hit redis
105
+ expect { publisher.run }
106
+ .to change { Sidekiq::Queues["default"].select{|job| job.key?('at')}.size }.by(1)
107
+ end
108
+
109
+ it 'schedules to run at some time a sidekiq job' do
110
+ publisher.subscribe(CustomizedEventScheduleAtJobSubscriberUnderTest, async: described_class.new)
111
+
112
+ # In order to look into Sidekiq::ScheduledSet we need to hit redis
113
+ expect { publisher.run }
114
+ .to change { Sidekiq::Queues["default"].select{|job| job.key?('at')}.size }.by(1)
115
+ end
116
+
117
+ it 'schedules to run at some time a sidekiq job for an event' do
118
+ publisher.subscribe(CustomizedEventScheduleInJobSubscriberUnderTest, async: described_class.new)
119
+
120
+ # In order to look into Sidekiq::ScheduledSet we need to hit redis
121
+ expect { publisher.run }
122
+ .to change { Sidekiq::Queues["default"].select{|job| job.key?('at')}.size }.by(1)
123
+ end
124
+
39
125
  it 'can respect custom sidekiq_options' do
40
126
  publisher.subscribe(CustomizedSubscriberUnderTest, async: described_class.new)
41
127
 
@@ -43,6 +129,22 @@ RSpec.describe Wisper::SidekiqBroadcaster do
43
129
  .to change(Sidekiq::Queues["my_queue"], :size).by(1)
44
130
  end
45
131
 
132
+ it 'schedules a sidekiq job with bad sidekiq_schedule_options' do
133
+ publisher.subscribe(CustomizedBadScheduleInJobSubscriberUnderTest, async: described_class.new)
134
+
135
+ expect { publisher.run }
136
+ .to change(Sidekiq::Queues["default"], :size).by(1)
137
+ expect { publisher.run }
138
+ .not_to change { Sidekiq::Queues["default"].select{|job| job.key?('at')}.size }
139
+ end
140
+
141
+ it 'schedules a sidekiq job with bad sidekiq_schedule_options' do
142
+ publisher.subscribe(CustomizedBadDefaultScheduleInWithEventScheduleAtJobSubscriberUnderTest, async: described_class.new)
143
+
144
+ expect { publisher.run }
145
+ .to change { Sidekiq::Queues["default"].select{|job| job.key?('at')}.size }.by(1)
146
+ end
147
+
46
148
  context 'when provides subscriber with args' do
47
149
  let(:subscriber) { RegularSubscriberUnderTest }
48
150
  let(:event) { 'it_happened' }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wisper-sidekiq
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kris Leech
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-02 00:00:00.000000000 Z
11
+ date: 2019-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: wisper
@@ -84,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
84
  version: '0'
85
85
  requirements: []
86
86
  rubyforge_project:
87
- rubygems_version: 2.7.6
87
+ rubygems_version: 2.7.6.2
88
88
  signing_key:
89
89
  specification_version: 4
90
90
  summary: Async publishing for Wisper using Sidekiq