sqeduler 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,275 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+ require "./spec/fixtures/fake_worker"
4
+
5
+ def verify_callback_occured(file_path, times = 1)
6
+ expect(File).to exist(file_path)
7
+ expect(File.read(file_path).length).to eq(times)
8
+ end
9
+
10
+ def verify_callback_skipped(file_path)
11
+ expect(File).to_not exist(file_path)
12
+ end
13
+
14
+ def maybe_cleanup_file(file_path)
15
+ File.delete(file_path) if File.exist?(file_path)
16
+ end
17
+
18
+ RSpec.describe Sqeduler::Worker do
19
+ before do
20
+ Sqeduler::Service.config = Sqeduler::Config.new(
21
+ :redis_hash => REDIS_CONFIG,
22
+ :logger => Logger.new(STDOUT).tap { |l| l.level = Logger::DEBUG }
23
+ )
24
+ end
25
+
26
+ after do
27
+ maybe_cleanup_file(FakeWorker::JOB_RUN_PATH)
28
+ maybe_cleanup_file(FakeWorker::JOB_SUCCESS_PATH)
29
+ maybe_cleanup_file(FakeWorker::JOB_FAILURE_PATH)
30
+ maybe_cleanup_file(FakeWorker::JOB_LOCK_FAILURE_PATH)
31
+ maybe_cleanup_file(FakeWorker::JOB_BEFORE_START_PATH)
32
+ maybe_cleanup_file(FakeWorker::SCHEDULE_COLLISION_PATH)
33
+ end
34
+
35
+ describe "#perform" do
36
+ context "synchronized workers" do
37
+ before do
38
+ FakeWorker.synchronize :one_at_a_time,
39
+ :expiration => expiration,
40
+ :timeout => timeout
41
+ end
42
+
43
+ let(:expiration) { work_time * 4 }
44
+ let(:work_time) { 0.1 }
45
+
46
+ subject do
47
+ threads = []
48
+ threads << Thread.new { FakeWorker.new.perform(work_time) }
49
+ threads << Thread.new do
50
+ sleep wait_time
51
+ FakeWorker.new.perform(work_time)
52
+ end
53
+ threads.each(&:join)
54
+ end
55
+
56
+ context "overlapping schedule" do
57
+ let(:wait_time) { 0 }
58
+
59
+ context "timeout is less than work_time (too short)" do
60
+ let(:timeout) { work_time / 2 }
61
+
62
+ it "one worker should be blocked" do
63
+ subject
64
+ verify_callback_occured(FakeWorker::JOB_LOCK_FAILURE_PATH)
65
+ end
66
+
67
+ it "only one worker should run" do
68
+ subject
69
+ verify_callback_occured(FakeWorker::JOB_RUN_PATH)
70
+ end
71
+
72
+ it "one worker should succeed" do
73
+ subject
74
+ verify_callback_occured(FakeWorker::JOB_SUCCESS_PATH, 2)
75
+ end
76
+
77
+ it "no worker should fail" do
78
+ subject
79
+ verify_callback_skipped(FakeWorker::JOB_FAILURE_PATH)
80
+ end
81
+
82
+ it "all workers should have received before_start" do
83
+ subject
84
+ verify_callback_occured(FakeWorker::JOB_BEFORE_START_PATH, 2)
85
+ end
86
+
87
+ it "a schedule collision should not have occurred" do
88
+ subject
89
+ verify_callback_skipped(FakeWorker::SCHEDULE_COLLISION_PATH)
90
+ end
91
+ end
92
+
93
+ context "timeout is greater than work_time" do
94
+ let(:timeout) { work_time * 4 }
95
+
96
+ it "no worker should be blocked" do
97
+ subject
98
+ verify_callback_skipped(FakeWorker::JOB_LOCK_FAILURE_PATH)
99
+ end
100
+
101
+ it "both workers should succeed" do
102
+ subject
103
+ verify_callback_occured(FakeWorker::JOB_SUCCESS_PATH, 2)
104
+ end
105
+
106
+ it "no worker should fail" do
107
+ subject
108
+ verify_callback_skipped(FakeWorker::JOB_FAILURE_PATH)
109
+ end
110
+
111
+ it "all workers should have received before_start" do
112
+ subject
113
+ verify_callback_occured(FakeWorker::JOB_BEFORE_START_PATH, 2)
114
+ end
115
+
116
+ it "a schedule collision should not have occurred" do
117
+ subject
118
+ verify_callback_skipped(FakeWorker::SCHEDULE_COLLISION_PATH)
119
+ end
120
+
121
+ context "expiration too short" do
122
+ let(:expiration) { work_time / 2 }
123
+
124
+ it "no worker should be blocked" do
125
+ subject
126
+ verify_callback_skipped(FakeWorker::JOB_LOCK_FAILURE_PATH)
127
+ end
128
+
129
+ it "all workers should run" do
130
+ subject
131
+ verify_callback_occured(FakeWorker::JOB_RUN_PATH, 2)
132
+ end
133
+
134
+ it "all workers should have received before_start" do
135
+ subject
136
+ verify_callback_occured(FakeWorker::JOB_BEFORE_START_PATH, 2)
137
+ end
138
+
139
+ it "no worker should fail" do
140
+ subject
141
+ verify_callback_occured(FakeWorker::JOB_SUCCESS_PATH, 2)
142
+ verify_callback_skipped(FakeWorker::JOB_FAILURE_PATH)
143
+ end
144
+
145
+ it "a schedule collision should occur" do
146
+ subject
147
+ verify_callback_occured(FakeWorker::SCHEDULE_COLLISION_PATH, 2)
148
+ end
149
+ end
150
+ end
151
+ end
152
+
153
+ context "non-overlapping schedule" do
154
+ let(:wait_time) { work_time * 2 }
155
+
156
+ context "timeout is less than work_time (too short)" do
157
+ let(:timeout) { work_time }
158
+
159
+ it "no workers should be blocked" do
160
+ subject
161
+ verify_callback_skipped(FakeWorker::JOB_LOCK_FAILURE_PATH)
162
+ end
163
+
164
+ it "all workers should run" do
165
+ subject
166
+ verify_callback_occured(FakeWorker::JOB_RUN_PATH, 2)
167
+ end
168
+
169
+ it "no worker should fail" do
170
+ subject
171
+ verify_callback_occured(FakeWorker::JOB_SUCCESS_PATH, 2)
172
+ verify_callback_skipped(FakeWorker::JOB_FAILURE_PATH)
173
+ end
174
+
175
+ it "all workers should have received before_start" do
176
+ subject
177
+ verify_callback_occured(FakeWorker::JOB_BEFORE_START_PATH, 2)
178
+ end
179
+
180
+ it "a schedule collision should not have occurred" do
181
+ subject
182
+ verify_callback_skipped(FakeWorker::SCHEDULE_COLLISION_PATH)
183
+ end
184
+ end
185
+
186
+ context "timeout is greater than work_time" do
187
+ let(:timeout) { work_time * 2 }
188
+
189
+ it "no worker should be blocked" do
190
+ subject
191
+ verify_callback_skipped(FakeWorker::JOB_LOCK_FAILURE_PATH)
192
+ end
193
+
194
+ it "both workers should succeed" do
195
+ subject
196
+ verify_callback_occured(FakeWorker::JOB_SUCCESS_PATH, 2)
197
+ end
198
+
199
+ it "no worker should fail" do
200
+ subject
201
+ verify_callback_skipped(FakeWorker::JOB_FAILURE_PATH)
202
+ end
203
+
204
+ it "all workers should have received before_start" do
205
+ subject
206
+ verify_callback_occured(FakeWorker::JOB_BEFORE_START_PATH, 2)
207
+ end
208
+
209
+ context "expiration too short" do
210
+ let(:expiration) { work_time / 2 }
211
+
212
+ it "no worker should be blocked" do
213
+ subject
214
+ verify_callback_skipped(FakeWorker::JOB_LOCK_FAILURE_PATH)
215
+ end
216
+
217
+ it "all workers should run" do
218
+ subject
219
+ verify_callback_occured(FakeWorker::JOB_RUN_PATH, 2)
220
+ end
221
+
222
+ it "all workers should have received before_start" do
223
+ subject
224
+ verify_callback_occured(FakeWorker::JOB_BEFORE_START_PATH, 2)
225
+ end
226
+
227
+ it "no worker should fail" do
228
+ subject
229
+ verify_callback_occured(FakeWorker::JOB_SUCCESS_PATH, 2)
230
+ verify_callback_skipped(FakeWorker::JOB_FAILURE_PATH)
231
+ end
232
+
233
+ it "a schedule collision should occur" do
234
+ subject
235
+ verify_callback_occured(FakeWorker::SCHEDULE_COLLISION_PATH, 2)
236
+ end
237
+ end
238
+ end
239
+ end
240
+ end
241
+ end
242
+
243
+ describe ".disable" do
244
+ before do
245
+ FakeWorker.disable
246
+ end
247
+
248
+ it "should not run" do
249
+ FakeWorker.new.perform(0)
250
+ verify_callback_skipped(FakeWorker::JOB_RUN_PATH)
251
+ end
252
+
253
+ it "should be disabled?" do
254
+ expect(FakeWorker.disabled?).to be true
255
+ expect(FakeWorker.enabled?).to be false
256
+ end
257
+ end
258
+
259
+ describe ".enable" do
260
+ before do
261
+ FakeWorker.disable
262
+ FakeWorker.enable
263
+ end
264
+
265
+ it "should run" do
266
+ FakeWorker.new.perform(0)
267
+ verify_callback_occured(FakeWorker::JOB_RUN_PATH)
268
+ end
269
+
270
+ it "should be enabled?" do
271
+ expect(FakeWorker.disabled?).to be false
272
+ expect(FakeWorker.enabled?).to be true
273
+ end
274
+ end
275
+ end
data/sqeduler.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+ require File.expand_path("../lib/sqeduler/version", __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = "sqeduler"
6
+ gem.version = Sqeduler::VERSION
7
+ gem.summary = "Common Sidekiq infrastructure for multi-host applications."
8
+ gem.description = <<-DESC
9
+ Works with Sidekiq scheduler to provides a highly available scheduler that can be run on
10
+ multiple hosts. Also provides a convenient abstract class for Sidekiq workers.
11
+ DESC
12
+ gem.license = "Apache"
13
+ gem.authors = '["Jared Jenkins"]'
14
+ gem.email = "jaredjenkins@squareup.com"
15
+ gem.homepage = "https://rubygems.org/gems/sqeduler"
16
+
17
+ gem.files = `git ls-files`.split($RS)
18
+ gem.executables = gem.files.grep(/^bin\//).map { |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(/^(test|spec|features)\//)
20
+ gem.require_paths = ["lib"]
21
+
22
+ gem.add_runtime_dependency "sidekiq"
23
+ gem.add_runtime_dependency "sidekiq-scheduler"
24
+ gem.add_runtime_dependency "activesupport"
25
+
26
+ gem.add_development_dependency "bundler", "~> 1.7"
27
+ gem.add_development_dependency "pry", "~> 0"
28
+ gem.add_development_dependency "rake", "~> 10"
29
+ gem.add_development_dependency "rspec", "~> 3"
30
+ gem.add_development_dependency "rubocop", "~> 0.24"
31
+ gem.add_development_dependency "timecop", "~> 0"
32
+ gem.add_development_dependency "yard", "~> 0"
33
+ end
metadata ADDED
@@ -0,0 +1,233 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sqeduler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ platform: ruby
6
+ authors:
7
+ - '["Jared Jenkins"]'
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sidekiq
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sidekiq-scheduler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.24'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.24'
125
+ - !ruby/object:Gem::Dependency
126
+ name: timecop
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: yard
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: |2
154
+ Works with Sidekiq scheduler to provides a highly available scheduler that can be run on
155
+ multiple hosts. Also provides a convenient abstract class for Sidekiq workers.
156
+ email: jaredjenkins@squareup.com
157
+ executables: []
158
+ extensions: []
159
+ extra_rdoc_files: []
160
+ files:
161
+ - ".document"
162
+ - ".gitignore"
163
+ - ".rspec"
164
+ - ".rubocop.yml"
165
+ - ".travis.yml"
166
+ - ".yardopts"
167
+ - CHANGES.md
168
+ - CONTRIBUTING.md
169
+ - Gemfile
170
+ - LICENSE.txt
171
+ - README.md
172
+ - Rakefile
173
+ - lib/sqeduler.rb
174
+ - lib/sqeduler/config.rb
175
+ - lib/sqeduler/lock_value.rb
176
+ - lib/sqeduler/redis_lock.rb
177
+ - lib/sqeduler/redis_scripts.rb
178
+ - lib/sqeduler/service.rb
179
+ - lib/sqeduler/trigger_lock.rb
180
+ - lib/sqeduler/version.rb
181
+ - lib/sqeduler/worker/callbacks.rb
182
+ - lib/sqeduler/worker/everything.rb
183
+ - lib/sqeduler/worker/kill_switch.rb
184
+ - lib/sqeduler/worker/synchronization.rb
185
+ - spec/config_spec.rb
186
+ - spec/fixtures/env.rb
187
+ - spec/fixtures/fake_worker.rb
188
+ - spec/fixtures/schedule.yaml
189
+ - spec/integration_spec.rb
190
+ - spec/service_spec.rb
191
+ - spec/spec_helper.rb
192
+ - spec/sqeduler_spec.rb
193
+ - spec/trigger_lock_spec.rb
194
+ - spec/worker/synchronization_spec.rb
195
+ - spec/worker_spec.rb
196
+ - sqeduler.gemspec
197
+ homepage: https://rubygems.org/gems/sqeduler
198
+ licenses:
199
+ - Apache
200
+ metadata: {}
201
+ post_install_message:
202
+ rdoc_options: []
203
+ require_paths:
204
+ - lib
205
+ required_ruby_version: !ruby/object:Gem::Requirement
206
+ requirements:
207
+ - - ">="
208
+ - !ruby/object:Gem::Version
209
+ version: '0'
210
+ required_rubygems_version: !ruby/object:Gem::Requirement
211
+ requirements:
212
+ - - ">="
213
+ - !ruby/object:Gem::Version
214
+ version: '0'
215
+ requirements: []
216
+ rubyforge_project:
217
+ rubygems_version: 2.4.6
218
+ signing_key:
219
+ specification_version: 4
220
+ summary: Common Sidekiq infrastructure for multi-host applications.
221
+ test_files:
222
+ - spec/config_spec.rb
223
+ - spec/fixtures/env.rb
224
+ - spec/fixtures/fake_worker.rb
225
+ - spec/fixtures/schedule.yaml
226
+ - spec/integration_spec.rb
227
+ - spec/service_spec.rb
228
+ - spec/spec_helper.rb
229
+ - spec/sqeduler_spec.rb
230
+ - spec/trigger_lock_spec.rb
231
+ - spec/worker/synchronization_spec.rb
232
+ - spec/worker_spec.rb
233
+ has_rdoc: