multi_worker 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +4 -0
  4. data/.travis.yml +10 -0
  5. data/Gemfile +6 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +110 -0
  8. data/Rakefile +22 -0
  9. data/lib/multi_worker/adapters/backburner.rb +32 -0
  10. data/lib/multi_worker/adapters/delayed_job.rb +30 -0
  11. data/lib/multi_worker/adapters/inline.rb +21 -0
  12. data/lib/multi_worker/adapters/qu.rb +30 -0
  13. data/lib/multi_worker/adapters/que.rb +47 -0
  14. data/lib/multi_worker/adapters/queue_classic.rb +29 -0
  15. data/lib/multi_worker/adapters/resque.rb +59 -0
  16. data/lib/multi_worker/adapters/sidekiq.rb +56 -0
  17. data/lib/multi_worker/adapters/sneakers.rb +32 -0
  18. data/lib/multi_worker/adapters/sucker_punch.rb +25 -0
  19. data/lib/multi_worker/adapters/threaded_in_memory_queue.rb +27 -0
  20. data/lib/multi_worker/adapters/torquebox_backgroundable.rb +25 -0
  21. data/lib/multi_worker/interface.rb +24 -0
  22. data/lib/multi_worker/tasks.rb +13 -0
  23. data/lib/multi_worker/version.rb +3 -0
  24. data/lib/multi_worker.rb +79 -0
  25. data/multi_worker.gemspec +56 -0
  26. data/spec/adapters/backburner_spec.rb +32 -0
  27. data/spec/adapters/delayed_job_spec.rb +32 -0
  28. data/spec/adapters/inline_spec.rb +18 -0
  29. data/spec/adapters/qu_spec.rb +29 -0
  30. data/spec/adapters/que_spec.rb +44 -0
  31. data/spec/adapters/queue_classic_spec.rb +41 -0
  32. data/spec/adapters/resque_spec.rb +74 -0
  33. data/spec/adapters/sidekiq_spec.rb +91 -0
  34. data/spec/adapters/sneakers_spec.rb +36 -0
  35. data/spec/adapters/sucker_punch_spec.rb +36 -0
  36. data/spec/adapters/threaded_in_memory_queue_spec.rb +25 -0
  37. data/spec/adapters/torquebox_backgroundable_spec.rb +31 -0
  38. data/spec/configuration_spec.rb +59 -0
  39. data/spec/delayed/backend/test.rb +113 -0
  40. data/spec/delayed/serialization/test.rb +0 -0
  41. data/spec/shared/worker_spec.rb +18 -0
  42. data/spec/spec_helper.rb +22 -0
  43. data/spec/test_workers.rb +7 -0
  44. metadata +427 -0
@@ -0,0 +1,113 @@
1
+ require 'ostruct'
2
+
3
+ # An in-memory backend suitable only for testing. Tries to behave as if it were an ORM.
4
+ module Delayed
5
+ module Backend
6
+ module Test
7
+ class Job
8
+ attr_accessor :id
9
+ attr_accessor :priority
10
+ attr_accessor :attempts
11
+ attr_accessor :handler
12
+ attr_accessor :last_error
13
+ attr_accessor :run_at
14
+ attr_accessor :locked_at
15
+ attr_accessor :locked_by
16
+ attr_accessor :failed_at
17
+ attr_accessor :queue
18
+
19
+ include Delayed::Backend::Base
20
+
21
+ cattr_accessor :id
22
+ self.id = 0
23
+
24
+ def initialize(hash = {})
25
+ self.attempts = 0
26
+ self.priority = 0
27
+ self.id = (self.class.id += 1)
28
+ hash.each{|k,v| send(:"#{k}=", v)}
29
+ end
30
+
31
+ @jobs = []
32
+ def self.all
33
+ @jobs
34
+ end
35
+
36
+ def self.count
37
+ all.size
38
+ end
39
+
40
+ def self.delete_all
41
+ all.clear
42
+ end
43
+
44
+ def self.create(attrs = {})
45
+ new(attrs).tap do |o|
46
+ o.save
47
+ end
48
+ end
49
+
50
+ def self.create!(*args); create(*args); end
51
+
52
+ def self.clear_locks!(worker_name)
53
+ all.select{|j| j.locked_by == worker_name}.each {|j| j.locked_by = nil; j.locked_at = nil}
54
+ end
55
+
56
+ # Find a few candidate jobs to run (in case some immediately get locked by others).
57
+ def self.find_available(worker_name, limit = 5, max_run_time = Worker.max_run_time)
58
+ jobs = all.select do |j|
59
+ j.run_at <= db_time_now &&
60
+ (j.locked_at.nil? || j.locked_at < db_time_now - max_run_time || j.locked_by == worker_name) &&
61
+ !j.failed?
62
+ end
63
+
64
+ jobs = jobs.select{|j| Worker.queues.include?(j.queue)} if Worker.queues.any?
65
+ jobs = jobs.select{|j| j.priority >= Worker.min_priority} if Worker.min_priority
66
+ jobs = jobs.select{|j| j.priority <= Worker.max_priority} if Worker.max_priority
67
+ jobs.sort_by{|j| [j.priority, j.run_at]}[0..limit-1]
68
+ end
69
+
70
+ # Lock this job for this worker.
71
+ # Returns true if we have the lock, false otherwise.
72
+ def lock_exclusively!(max_run_time, worker)
73
+ now = self.class.db_time_now
74
+ if locked_by != worker
75
+ # We don't own this job so we will update the locked_by name and the locked_at
76
+ self.locked_at = now
77
+ self.locked_by = worker
78
+ end
79
+
80
+ return true
81
+ end
82
+
83
+ def self.db_time_now
84
+ Time.current
85
+ end
86
+
87
+ def update_attributes(attrs = {})
88
+ attrs.each{|k,v| send(:"#{k}=", v)}
89
+ save
90
+ end
91
+
92
+ def destroy
93
+ self.class.all.delete(self)
94
+ end
95
+
96
+ def save
97
+ self.run_at ||= Time.current
98
+
99
+ self.class.all << self unless self.class.all.include?(self)
100
+ true
101
+ end
102
+
103
+ def save!; save; end
104
+
105
+ def reload
106
+ reset
107
+ self
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
113
+
File without changes
@@ -0,0 +1,18 @@
1
+ shared_examples_for "a worker" do
2
+ context "class" do
3
+ subject { described_class }
4
+
5
+ it { should respond_to(:perform) }
6
+ it { should respond_to(:perform_async) }
7
+
8
+ it "delegates ::perform to #perform" do
9
+ TestWorker.any_instance.should_receive(:perform).once.with("foo")
10
+ TestWorker.perform("foo")
11
+ end
12
+ end
13
+
14
+ context "instance" do
15
+ it { should respond_to(:perform) }
16
+ it { should respond_to(:perform_async) }
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ #require 'bundler/setup'
2
+ require 'multi_worker'
3
+ #require 'pry'
4
+ $LOAD_PATH << File.expand_path('.')
5
+
6
+ def jruby?
7
+ defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
8
+ end
9
+
10
+ RSpec.configure do |config|
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+
15
+ # Run specs in random order to surface order dependencies. If you find an
16
+ # order dependency and want to debug it, you can fix the order by providing
17
+ # the seed, which is printed after each run.
18
+ # --seed 1234
19
+ config.order = 'random'
20
+ end
21
+
22
+ require 'spec/shared/worker_spec'
@@ -0,0 +1,7 @@
1
+ class TestWorker
2
+ worker
3
+
4
+ def perform(arg)
5
+ arg
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,427 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multi_worker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David Butler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: resque
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: resque-retry
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: resque-status
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: resque-loner
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: resque-lock-timeout
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: resque-delay
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: sidekiq
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
+ - !ruby/object:Gem::Dependency
154
+ name: sidekiq-lock
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - '>='
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: sidekiq_status
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - '>='
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: sidekiq-unique-jobs
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - '>='
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - '>='
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: sidekiq-delay
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - '>='
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - '>='
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ - !ruby/object:Gem::Dependency
210
+ name: delayed_job
211
+ requirement: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - '>='
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ type: :development
217
+ prerelease: false
218
+ version_requirements: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - '>='
221
+ - !ruby/object:Gem::Version
222
+ version: '0'
223
+ - !ruby/object:Gem::Dependency
224
+ name: delayed_job_active_record
225
+ requirement: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - '>='
228
+ - !ruby/object:Gem::Version
229
+ version: '0'
230
+ type: :development
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - '>='
235
+ - !ruby/object:Gem::Version
236
+ version: '0'
237
+ - !ruby/object:Gem::Dependency
238
+ name: qu
239
+ requirement: !ruby/object:Gem::Requirement
240
+ requirements:
241
+ - - '='
242
+ - !ruby/object:Gem::Version
243
+ version: 0.2.0
244
+ type: :development
245
+ prerelease: false
246
+ version_requirements: !ruby/object:Gem::Requirement
247
+ requirements:
248
+ - - '='
249
+ - !ruby/object:Gem::Version
250
+ version: 0.2.0
251
+ - !ruby/object:Gem::Dependency
252
+ name: queue_classic
253
+ requirement: !ruby/object:Gem::Requirement
254
+ requirements:
255
+ - - '>='
256
+ - !ruby/object:Gem::Version
257
+ version: '0'
258
+ type: :development
259
+ prerelease: false
260
+ version_requirements: !ruby/object:Gem::Requirement
261
+ requirements:
262
+ - - '>='
263
+ - !ruby/object:Gem::Version
264
+ version: '0'
265
+ - !ruby/object:Gem::Dependency
266
+ name: que
267
+ requirement: !ruby/object:Gem::Requirement
268
+ requirements:
269
+ - - '>='
270
+ - !ruby/object:Gem::Version
271
+ version: '0'
272
+ type: :development
273
+ prerelease: false
274
+ version_requirements: !ruby/object:Gem::Requirement
275
+ requirements:
276
+ - - '>='
277
+ - !ruby/object:Gem::Version
278
+ version: '0'
279
+ - !ruby/object:Gem::Dependency
280
+ name: sneakers
281
+ requirement: !ruby/object:Gem::Requirement
282
+ requirements:
283
+ - - '>='
284
+ - !ruby/object:Gem::Version
285
+ version: '0'
286
+ type: :development
287
+ prerelease: false
288
+ version_requirements: !ruby/object:Gem::Requirement
289
+ requirements:
290
+ - - '>='
291
+ - !ruby/object:Gem::Version
292
+ version: '0'
293
+ - !ruby/object:Gem::Dependency
294
+ name: threaded_in_memory_queue
295
+ requirement: !ruby/object:Gem::Requirement
296
+ requirements:
297
+ - - '>='
298
+ - !ruby/object:Gem::Version
299
+ version: '0'
300
+ type: :development
301
+ prerelease: false
302
+ version_requirements: !ruby/object:Gem::Requirement
303
+ requirements:
304
+ - - '>='
305
+ - !ruby/object:Gem::Version
306
+ version: '0'
307
+ - !ruby/object:Gem::Dependency
308
+ name: sucker_punch
309
+ requirement: !ruby/object:Gem::Requirement
310
+ requirements:
311
+ - - '>='
312
+ - !ruby/object:Gem::Version
313
+ version: '0'
314
+ type: :development
315
+ prerelease: false
316
+ version_requirements: !ruby/object:Gem::Requirement
317
+ requirements:
318
+ - - '>='
319
+ - !ruby/object:Gem::Version
320
+ version: '0'
321
+ - !ruby/object:Gem::Dependency
322
+ name: backburner
323
+ requirement: !ruby/object:Gem::Requirement
324
+ requirements:
325
+ - - '>='
326
+ - !ruby/object:Gem::Version
327
+ version: '0'
328
+ type: :development
329
+ prerelease: false
330
+ version_requirements: !ruby/object:Gem::Requirement
331
+ requirements:
332
+ - - '>='
333
+ - !ruby/object:Gem::Version
334
+ version: '0'
335
+ description: Provides a common interface to Ruby worker/queue libraries.
336
+ email:
337
+ - dwbutler@ucla.edu
338
+ executables: []
339
+ extensions: []
340
+ extra_rdoc_files: []
341
+ files:
342
+ - .gitignore
343
+ - .rspec
344
+ - .travis.yml
345
+ - Gemfile
346
+ - LICENSE.txt
347
+ - README.md
348
+ - Rakefile
349
+ - lib/multi_worker.rb
350
+ - lib/multi_worker/adapters/backburner.rb
351
+ - lib/multi_worker/adapters/delayed_job.rb
352
+ - lib/multi_worker/adapters/inline.rb
353
+ - lib/multi_worker/adapters/qu.rb
354
+ - lib/multi_worker/adapters/que.rb
355
+ - lib/multi_worker/adapters/queue_classic.rb
356
+ - lib/multi_worker/adapters/resque.rb
357
+ - lib/multi_worker/adapters/sidekiq.rb
358
+ - lib/multi_worker/adapters/sneakers.rb
359
+ - lib/multi_worker/adapters/sucker_punch.rb
360
+ - lib/multi_worker/adapters/threaded_in_memory_queue.rb
361
+ - lib/multi_worker/adapters/torquebox_backgroundable.rb
362
+ - lib/multi_worker/interface.rb
363
+ - lib/multi_worker/tasks.rb
364
+ - lib/multi_worker/version.rb
365
+ - multi_worker.gemspec
366
+ - spec/adapters/backburner_spec.rb
367
+ - spec/adapters/delayed_job_spec.rb
368
+ - spec/adapters/inline_spec.rb
369
+ - spec/adapters/qu_spec.rb
370
+ - spec/adapters/que_spec.rb
371
+ - spec/adapters/queue_classic_spec.rb
372
+ - spec/adapters/resque_spec.rb
373
+ - spec/adapters/sidekiq_spec.rb
374
+ - spec/adapters/sneakers_spec.rb
375
+ - spec/adapters/sucker_punch_spec.rb
376
+ - spec/adapters/threaded_in_memory_queue_spec.rb
377
+ - spec/adapters/torquebox_backgroundable_spec.rb
378
+ - spec/configuration_spec.rb
379
+ - spec/delayed/backend/test.rb
380
+ - spec/delayed/serialization/test.rb
381
+ - spec/shared/worker_spec.rb
382
+ - spec/spec_helper.rb
383
+ - spec/test_workers.rb
384
+ homepage: https://github.com/dwbutler/multi_worker
385
+ licenses:
386
+ - MIT
387
+ metadata: {}
388
+ post_install_message:
389
+ rdoc_options: []
390
+ require_paths:
391
+ - lib
392
+ required_ruby_version: !ruby/object:Gem::Requirement
393
+ requirements:
394
+ - - '>='
395
+ - !ruby/object:Gem::Version
396
+ version: '0'
397
+ required_rubygems_version: !ruby/object:Gem::Requirement
398
+ requirements:
399
+ - - '>='
400
+ - !ruby/object:Gem::Version
401
+ version: '0'
402
+ requirements: []
403
+ rubyforge_project:
404
+ rubygems_version: 2.1.11
405
+ signing_key:
406
+ specification_version: 4
407
+ summary: Provides a common interface to Ruby worker/queue libraries.
408
+ test_files:
409
+ - spec/adapters/backburner_spec.rb
410
+ - spec/adapters/delayed_job_spec.rb
411
+ - spec/adapters/inline_spec.rb
412
+ - spec/adapters/qu_spec.rb
413
+ - spec/adapters/que_spec.rb
414
+ - spec/adapters/queue_classic_spec.rb
415
+ - spec/adapters/resque_spec.rb
416
+ - spec/adapters/sidekiq_spec.rb
417
+ - spec/adapters/sneakers_spec.rb
418
+ - spec/adapters/sucker_punch_spec.rb
419
+ - spec/adapters/threaded_in_memory_queue_spec.rb
420
+ - spec/adapters/torquebox_backgroundable_spec.rb
421
+ - spec/configuration_spec.rb
422
+ - spec/delayed/backend/test.rb
423
+ - spec/delayed/serialization/test.rb
424
+ - spec/shared/worker_spec.rb
425
+ - spec/spec_helper.rb
426
+ - spec/test_workers.rb
427
+ has_rdoc: