resque-igo 1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/HISTORY.md +225 -0
  2. data/LICENSE +20 -0
  3. data/README.markdown +855 -0
  4. data/Rakefile +70 -0
  5. data/bin/resque +57 -0
  6. data/bin/resque-web +23 -0
  7. data/lib/resque.rb +380 -0
  8. data/lib/resque/errors.rb +10 -0
  9. data/lib/resque/failure.rb +66 -0
  10. data/lib/resque/failure/base.rb +61 -0
  11. data/lib/resque/failure/hoptoad.rb +132 -0
  12. data/lib/resque/failure/multiple.rb +50 -0
  13. data/lib/resque/failure/redis.rb +40 -0
  14. data/lib/resque/helpers.rb +71 -0
  15. data/lib/resque/job.rb +209 -0
  16. data/lib/resque/plugin.rb +51 -0
  17. data/lib/resque/server.rb +247 -0
  18. data/lib/resque/server/public/idle.png +0 -0
  19. data/lib/resque/server/public/jquery-1.3.2.min.js +19 -0
  20. data/lib/resque/server/public/jquery.relatize_date.js +95 -0
  21. data/lib/resque/server/public/poll.png +0 -0
  22. data/lib/resque/server/public/ranger.js +67 -0
  23. data/lib/resque/server/public/reset.css +48 -0
  24. data/lib/resque/server/public/style.css +86 -0
  25. data/lib/resque/server/public/working.png +0 -0
  26. data/lib/resque/server/test_helper.rb +19 -0
  27. data/lib/resque/server/views/error.erb +1 -0
  28. data/lib/resque/server/views/failed.erb +53 -0
  29. data/lib/resque/server/views/key_sets.erb +20 -0
  30. data/lib/resque/server/views/key_string.erb +11 -0
  31. data/lib/resque/server/views/layout.erb +42 -0
  32. data/lib/resque/server/views/next_more.erb +10 -0
  33. data/lib/resque/server/views/overview.erb +4 -0
  34. data/lib/resque/server/views/queues.erb +65 -0
  35. data/lib/resque/server/views/stats.erb +73 -0
  36. data/lib/resque/server/views/workers.erb +109 -0
  37. data/lib/resque/server/views/working.erb +68 -0
  38. data/lib/resque/stat.rb +54 -0
  39. data/lib/resque/tasks.rb +39 -0
  40. data/lib/resque/version.rb +3 -0
  41. data/lib/resque/worker.rb +478 -0
  42. data/tasks/resque.rake +2 -0
  43. data/test/job_hooks_test.rb +323 -0
  44. data/test/job_plugins_test.rb +230 -0
  45. data/test/plugin_test.rb +116 -0
  46. data/test/resque-web_test.rb +54 -0
  47. data/test/resque_test.rb +351 -0
  48. data/test/test_helper.rb +166 -0
  49. data/test/worker_test.rb +302 -0
  50. metadata +180 -0
data/tasks/resque.rake ADDED
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'resque/tasks'
@@ -0,0 +1,323 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ context "Resque::Job before_perform" do
4
+ include PerformJob
5
+
6
+ class ::BeforePerformJob
7
+ def self.before_perform_record_history(history)
8
+ history << :before_perform
9
+ end
10
+
11
+ def self.perform(history)
12
+ history << :perform
13
+ end
14
+ end
15
+
16
+ test "it runs before_perform before perform" do
17
+ result = perform_job(BeforePerformJob, history=[])
18
+ assert_equal true, result, "perform returned true"
19
+ assert_equal history, [:before_perform, :perform]
20
+ end
21
+
22
+ class ::BeforePerformJobFails
23
+ def self.before_perform_fail_job(history)
24
+ history << :before_perform
25
+ raise StandardError
26
+ end
27
+ def self.perform(history)
28
+ history << :perform
29
+ end
30
+ end
31
+
32
+ test "raises an error and does not perform if before_perform fails" do
33
+ history = []
34
+ assert_raises StandardError do
35
+ perform_job(BeforePerformJobFails, history)
36
+ end
37
+ assert_equal history, [:before_perform], "Only before_perform was run"
38
+ end
39
+
40
+ class ::BeforePerformJobAborts
41
+ def self.before_perform_abort(history)
42
+ history << :before_perform
43
+ raise Resque::Job::DontPerform
44
+ end
45
+ def self.perform(history)
46
+ history << :perform
47
+ end
48
+ end
49
+
50
+ test "does not perform if before_perform raises Resque::Job::DontPerform" do
51
+ result = perform_job(BeforePerformJobAborts, history=[])
52
+ assert_equal false, result, "perform returned false"
53
+ assert_equal history, [:before_perform], "Only before_perform was run"
54
+ end
55
+ end
56
+
57
+ context "Resque::Job after_perform" do
58
+ include PerformJob
59
+
60
+ class ::AfterPerformJob
61
+ def self.perform(history)
62
+ history << :perform
63
+ end
64
+ def self.after_perform_record_history(history)
65
+ history << :after_perform
66
+ end
67
+ end
68
+
69
+ test "it runs after_perform after perform" do
70
+ result = perform_job(AfterPerformJob, history=[])
71
+ assert_equal true, result, "perform returned true"
72
+ assert_equal history, [:perform, :after_perform]
73
+ end
74
+
75
+ class ::AfterPerformJobFails
76
+ def self.perform(history)
77
+ history << :perform
78
+ end
79
+ def self.after_perform_fail_job(history)
80
+ history << :after_perform
81
+ raise StandardError
82
+ end
83
+ end
84
+
85
+ test "raises an error but has already performed if after_perform fails" do
86
+ history = []
87
+ assert_raises StandardError do
88
+ perform_job(AfterPerformJobFails, history)
89
+ end
90
+ assert_equal history, [:perform, :after_perform], "Only after_perform was run"
91
+ end
92
+ end
93
+
94
+ context "Resque::Job around_perform" do
95
+ include PerformJob
96
+
97
+ class ::AroundPerformJob
98
+ def self.perform(history)
99
+ history << :perform
100
+ end
101
+ def self.around_perform_record_history(history)
102
+ history << :start_around_perform
103
+ yield
104
+ history << :finish_around_perform
105
+ end
106
+ end
107
+
108
+ test "it runs around_perform then yields in order to perform" do
109
+ result = perform_job(AroundPerformJob, history=[])
110
+ assert_equal true, result, "perform returned true"
111
+ assert_equal history, [:start_around_perform, :perform, :finish_around_perform]
112
+ end
113
+
114
+ class ::AroundPerformJobFailsBeforePerforming
115
+ def self.perform(history)
116
+ history << :perform
117
+ end
118
+ def self.around_perform_fail(history)
119
+ history << :start_around_perform
120
+ raise StandardError
121
+ yield
122
+ history << :finish_around_perform
123
+ end
124
+ end
125
+
126
+ test "raises an error and does not perform if around_perform fails before yielding" do
127
+ history = []
128
+ assert_raises StandardError do
129
+ perform_job(AroundPerformJobFailsBeforePerforming, history)
130
+ end
131
+ assert_equal history, [:start_around_perform], "Only part of around_perform was run"
132
+ end
133
+
134
+ class ::AroundPerformJobFailsWhilePerforming
135
+ def self.perform(history)
136
+ history << :perform
137
+ raise StandardError
138
+ end
139
+ def self.around_perform_fail_in_yield(history)
140
+ history << :start_around_perform
141
+ begin
142
+ yield
143
+ ensure
144
+ history << :ensure_around_perform
145
+ end
146
+ history << :finish_around_perform
147
+ end
148
+ end
149
+
150
+ test "raises an error but may handle exceptions if perform fails" do
151
+ history = []
152
+ assert_raises StandardError do
153
+ perform_job(AroundPerformJobFailsWhilePerforming, history)
154
+ end
155
+ assert_equal history, [:start_around_perform, :perform, :ensure_around_perform], "Only part of around_perform was run"
156
+ end
157
+
158
+ class ::AroundPerformJobDoesNotHaveToYield
159
+ def self.perform(history)
160
+ history << :perform
161
+ end
162
+ def self.around_perform_dont_yield(history)
163
+ history << :start_around_perform
164
+ history << :finish_around_perform
165
+ end
166
+ end
167
+
168
+ test "around_perform is not required to yield" do
169
+ history = []
170
+ result = perform_job(AroundPerformJobDoesNotHaveToYield, history)
171
+ assert_equal false, result, "perform returns false"
172
+ assert_equal history, [:start_around_perform, :finish_around_perform], "perform was not run"
173
+ end
174
+ end
175
+
176
+ context "Resque::Job on_failure" do
177
+ include PerformJob
178
+
179
+ class ::FailureJobThatDoesNotFail
180
+ def self.perform(history)
181
+ history << :perform
182
+ end
183
+ def self.on_failure_record_failure(exception, history)
184
+ history << exception.message
185
+ end
186
+ end
187
+
188
+ test "it does not call on_failure if no failures occur" do
189
+ result = perform_job(FailureJobThatDoesNotFail, history=[])
190
+ assert_equal true, result, "perform returned true"
191
+ assert_equal history, [:perform]
192
+ end
193
+
194
+ class ::FailureJobThatFails
195
+ def self.perform(history)
196
+ history << :perform
197
+ raise StandardError, "oh no"
198
+ end
199
+ def self.on_failure_record_failure(exception, history)
200
+ history << exception.message
201
+ end
202
+ end
203
+
204
+ test "it calls on_failure with the exception and then re-raises the exception" do
205
+ history = []
206
+ assert_raises StandardError do
207
+ perform_job(FailureJobThatFails, history)
208
+ end
209
+ assert_equal history, [:perform, "oh no"]
210
+ end
211
+
212
+ class ::FailureJobThatFailsBadly
213
+ def self.perform(history)
214
+ history << :perform
215
+ raise SyntaxError, "oh no"
216
+ end
217
+ def self.on_failure_record_failure(exception, history)
218
+ history << exception.message
219
+ end
220
+ end
221
+
222
+ test "it calls on_failure even with bad exceptions" do
223
+ history = []
224
+ assert_raises SyntaxError do
225
+ perform_job(FailureJobThatFailsBadly, history)
226
+ end
227
+ assert_equal history, [:perform, "oh no"]
228
+ end
229
+ end
230
+
231
+ context "Resque::Job after_enqueue" do
232
+ include PerformJob
233
+
234
+ class ::AfterEnqueueJob
235
+ def self.after_enqueue_record_history(history)
236
+ history << :after_enqueue
237
+ end
238
+
239
+ def self.perform(history)
240
+ end
241
+ end
242
+
243
+ test "the after enqueue hook should run" do
244
+ history = []
245
+ @worker = Resque::Worker.new(:jobs)
246
+ Resque::Job.create(:jobs, AfterEnqueueJob, history)
247
+ @worker.work(0)
248
+ assert_equal history, [:after_enqueue], "after_enqueue was not run"
249
+ end
250
+ end
251
+
252
+ context "Resque::Job all hooks" do
253
+ include PerformJob
254
+
255
+ class ::VeryHookyJob
256
+ def self.before_perform_record_history(history)
257
+ history << :before_perform
258
+ end
259
+ def self.around_perform_record_history(history)
260
+ history << :start_around_perform
261
+ yield
262
+ history << :finish_around_perform
263
+ end
264
+ def self.perform(history)
265
+ history << :perform
266
+ end
267
+ def self.after_perform_record_history(history)
268
+ history << :after_perform
269
+ end
270
+ def self.on_failure_record_history(exception, history)
271
+ history << exception.message
272
+ end
273
+ end
274
+
275
+ test "the complete hook order" do
276
+ result = perform_job(VeryHookyJob, history=[])
277
+ assert_equal true, result, "perform returned true"
278
+ assert_equal history, [
279
+ :before_perform,
280
+ :start_around_perform,
281
+ :perform,
282
+ :finish_around_perform,
283
+ :after_perform
284
+ ]
285
+ end
286
+
287
+ class ::VeryHookyJobThatFails
288
+ def self.before_perform_record_history(history)
289
+ history << :before_perform
290
+ end
291
+ def self.around_perform_record_history(history)
292
+ history << :start_around_perform
293
+ yield
294
+ history << :finish_around_perform
295
+ end
296
+ def self.perform(history)
297
+ history << :perform
298
+ end
299
+ def self.after_perform_record_history(history)
300
+ history << :after_perform
301
+ raise StandardError, "oh no"
302
+ end
303
+ def self.on_failure_record_history(exception, history)
304
+ history << exception.message
305
+ end
306
+ end
307
+
308
+ test "the complete hook order with a failure at the last minute" do
309
+ history = []
310
+ assert_raises StandardError do
311
+ perform_job(VeryHookyJobThatFails, history)
312
+ end
313
+ assert_equal history, [
314
+ :before_perform,
315
+ :start_around_perform,
316
+ :perform,
317
+ :finish_around_perform,
318
+ :after_perform,
319
+ "oh no"
320
+ ]
321
+ end
322
+ end
323
+
@@ -0,0 +1,230 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ context "Multiple plugins with multiple hooks" do
4
+ include PerformJob
5
+
6
+ module Plugin1
7
+ def before_perform_record_history1(history)
8
+ history << :before1
9
+ end
10
+ def after_perform_record_history1(history)
11
+ history << :after1
12
+ end
13
+ end
14
+
15
+ module Plugin2
16
+ def before_perform_record_history2(history)
17
+ history << :before2
18
+ end
19
+ def after_perform_record_history2(history)
20
+ history << :after2
21
+ end
22
+ end
23
+
24
+ class ::ManyBeforesJob
25
+ extend Plugin1
26
+ extend Plugin2
27
+ def self.perform(history)
28
+ history << :perform
29
+ end
30
+ end
31
+
32
+ test "hooks of each type are executed in alphabetical order" do
33
+ result = perform_job(ManyBeforesJob, history=[])
34
+ assert_equal true, result, "perform returned true"
35
+ assert_equal [:before1, :before2, :perform, :after1, :after2], history
36
+ end
37
+ end
38
+
39
+ context "Resque::Plugin ordering before_perform" do
40
+ include PerformJob
41
+
42
+ module BeforePerformPlugin
43
+ def before_perform1(history)
44
+ history << :before_perform1
45
+ end
46
+ end
47
+
48
+ class ::BeforePerformJob
49
+ extend BeforePerformPlugin
50
+ def self.perform(history)
51
+ history << :perform
52
+ end
53
+ def self.before_perform(history)
54
+ history << :before_perform
55
+ end
56
+ end
57
+
58
+ test "before_perform hooks are executed in order" do
59
+ result = perform_job(BeforePerformJob, history=[])
60
+ assert_equal true, result, "perform returned true"
61
+ assert_equal [:before_perform, :before_perform1, :perform], history
62
+ end
63
+ end
64
+
65
+ context "Resque::Plugin ordering after_perform" do
66
+ include PerformJob
67
+
68
+ module AfterPerformPlugin
69
+ def after_perform_record_history(history)
70
+ history << :after_perform1
71
+ end
72
+ end
73
+
74
+ class ::AfterPerformJob
75
+ extend AfterPerformPlugin
76
+ def self.perform(history)
77
+ history << :perform
78
+ end
79
+ def self.after_perform(history)
80
+ history << :after_perform
81
+ end
82
+ end
83
+
84
+ test "after_perform hooks are executed in order" do
85
+ result = perform_job(AfterPerformJob, history=[])
86
+ assert_equal true, result, "perform returned true"
87
+ assert_equal [:perform, :after_perform, :after_perform1], history
88
+ end
89
+ end
90
+
91
+ context "Resque::Plugin ordering around_perform" do
92
+ include PerformJob
93
+
94
+ module AroundPerformPlugin1
95
+ def around_perform1(history)
96
+ history << :around_perform_plugin1
97
+ yield
98
+ end
99
+ end
100
+
101
+ class ::AroundPerformJustPerformsJob
102
+ extend AroundPerformPlugin1
103
+ def self.perform(history)
104
+ history << :perform
105
+ end
106
+ end
107
+
108
+ test "around_perform hooks are executed before the job" do
109
+ result = perform_job(AroundPerformJustPerformsJob, history=[])
110
+ assert_equal true, result, "perform returned true"
111
+ assert_equal [:around_perform_plugin1, :perform], history
112
+ end
113
+
114
+ class ::AroundPerformJob
115
+ extend AroundPerformPlugin1
116
+ def self.perform(history)
117
+ history << :perform
118
+ end
119
+ def self.around_perform(history)
120
+ history << :around_perform
121
+ yield
122
+ end
123
+ end
124
+
125
+ test "around_perform hooks are executed in order" do
126
+ result = perform_job(AroundPerformJob, history=[])
127
+ assert_equal true, result, "perform returned true"
128
+ assert_equal [:around_perform, :around_perform_plugin1, :perform], history
129
+ end
130
+
131
+ module AroundPerformPlugin2
132
+ def around_perform2(history)
133
+ history << :around_perform_plugin2
134
+ yield
135
+ end
136
+ end
137
+
138
+ class ::AroundPerformJob2
139
+ extend AroundPerformPlugin1
140
+ extend AroundPerformPlugin2
141
+ def self.perform(history)
142
+ history << :perform
143
+ end
144
+ def self.around_perform(history)
145
+ history << :around_perform
146
+ yield
147
+ end
148
+ end
149
+
150
+ test "many around_perform are executed in order" do
151
+ result = perform_job(AroundPerformJob2, history=[])
152
+ assert_equal true, result, "perform returned true"
153
+ assert_equal [:around_perform, :around_perform_plugin1, :around_perform_plugin2, :perform], history
154
+ end
155
+
156
+ module AroundPerformDoesNotYield
157
+ def around_perform0(history)
158
+ history << :around_perform0
159
+ end
160
+ end
161
+
162
+ class ::AroundPerformJob3
163
+ extend AroundPerformPlugin1
164
+ extend AroundPerformPlugin2
165
+ extend AroundPerformDoesNotYield
166
+ def self.perform(history)
167
+ history << :perform
168
+ end
169
+ def self.around_perform(history)
170
+ history << :around_perform
171
+ yield
172
+ end
173
+ end
174
+
175
+ test "the job is aborted if an around_perform hook does not yield" do
176
+ result = perform_job(AroundPerformJob3, history=[])
177
+ assert_equal false, result, "perform returned false"
178
+ assert_equal [:around_perform, :around_perform0], history
179
+ end
180
+
181
+ module AroundPerformGetsJobResult
182
+ @@result = nil
183
+ def last_job_result
184
+ @@result
185
+ end
186
+
187
+ def around_perform_gets_job_result(*args)
188
+ @@result = yield
189
+ end
190
+ end
191
+
192
+ class ::AroundPerformJobWithReturnValue < GoodJob
193
+ extend AroundPerformGetsJobResult
194
+ end
195
+
196
+ test "the job is aborted if an around_perform hook does not yield" do
197
+ result = perform_job(AroundPerformJobWithReturnValue, 'Bob')
198
+ assert_equal true, result, "perform returned true"
199
+ assert_equal 'Good job, Bob', AroundPerformJobWithReturnValue.last_job_result
200
+ end
201
+ end
202
+
203
+ context "Resque::Plugin ordering on_failure" do
204
+ include PerformJob
205
+
206
+ module OnFailurePlugin
207
+ def on_failure1(exception, history)
208
+ history << "#{exception.message} plugin"
209
+ end
210
+ end
211
+
212
+ class ::FailureJob
213
+ extend OnFailurePlugin
214
+ def self.perform(history)
215
+ history << :perform
216
+ raise StandardError, "oh no"
217
+ end
218
+ def self.on_failure(exception, history)
219
+ history << exception.message
220
+ end
221
+ end
222
+
223
+ test "on_failure hooks are executed in order" do
224
+ history = []
225
+ assert_raises StandardError do
226
+ perform_job(FailureJob, history)
227
+ end
228
+ assert_equal [:perform, "oh no", "oh no plugin"], history
229
+ end
230
+ end