emipair-delayed_job 2.0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. data/.gitignore +2 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.textile +213 -0
  4. data/Rakefile +33 -0
  5. data/VERSION +1 -0
  6. data/benchmarks.rb +33 -0
  7. data/contrib/delayed_job.monitrc +14 -0
  8. data/contrib/delayed_job_multiple.monitrc +23 -0
  9. data/emipair-delayed_job.gemspec +25 -0
  10. data/generators/delayed_job/delayed_job_generator.rb +22 -0
  11. data/generators/delayed_job/templates/migration.rb +21 -0
  12. data/generators/delayed_job/templates/script +5 -0
  13. data/init.rb +1 -0
  14. data/lib/delayed/backend/active_record.rb +90 -0
  15. data/lib/delayed/backend/base.rb +111 -0
  16. data/lib/delayed/backend/data_mapper.rb +147 -0
  17. data/lib/delayed/backend/mongo_mapper.rb +110 -0
  18. data/lib/delayed/command.rb +109 -0
  19. data/lib/delayed/message_sending.rb +22 -0
  20. data/lib/delayed/performable_method.rb +62 -0
  21. data/lib/delayed/railtie.rb +10 -0
  22. data/lib/delayed/recipes.rb +31 -0
  23. data/lib/delayed/tasks.rb +15 -0
  24. data/lib/delayed/worker.rb +214 -0
  25. data/lib/delayed_job.rb +15 -0
  26. data/lib/passive_support.rb +4 -0
  27. data/lib/passive_support/basic_object.rb +16 -0
  28. data/lib/passive_support/core_ext.rb +8 -0
  29. data/lib/passive_support/core_ext/class.rb +1 -0
  30. data/lib/passive_support/core_ext/class/attribute_accessors.rb +57 -0
  31. data/lib/passive_support/core_ext/date.rb +10 -0
  32. data/lib/passive_support/core_ext/date/behavior.rb +42 -0
  33. data/lib/passive_support/core_ext/date/calculations.rb +241 -0
  34. data/lib/passive_support/core_ext/date/conversions.rb +107 -0
  35. data/lib/passive_support/core_ext/date_time.rb +12 -0
  36. data/lib/passive_support/core_ext/date_time/calculations.rb +126 -0
  37. data/lib/passive_support/core_ext/date_time/conversions.rb +107 -0
  38. data/lib/passive_support/core_ext/enumerable.rb +120 -0
  39. data/lib/passive_support/core_ext/kernel.rb +5 -0
  40. data/lib/passive_support/core_ext/kernel/agnostics.rb +11 -0
  41. data/lib/passive_support/core_ext/kernel/daemonizing.rb +7 -0
  42. data/lib/passive_support/core_ext/kernel/debugger.rb +16 -0
  43. data/lib/passive_support/core_ext/kernel/reporting.rb +59 -0
  44. data/lib/passive_support/core_ext/kernel/requires.rb +24 -0
  45. data/lib/passive_support/core_ext/module.rb +20 -0
  46. data/lib/passive_support/core_ext/module/aliasing.rb +74 -0
  47. data/lib/passive_support/core_ext/module/attr_accessor_with_default.rb +31 -0
  48. data/lib/passive_support/core_ext/module/attr_internal.rb +32 -0
  49. data/lib/passive_support/core_ext/module/delegation.rb +135 -0
  50. data/lib/passive_support/core_ext/module/inclusion.rb +30 -0
  51. data/lib/passive_support/core_ext/module/introspection.rb +90 -0
  52. data/lib/passive_support/core_ext/module/loading.rb +23 -0
  53. data/lib/passive_support/core_ext/module/model_naming.rb +25 -0
  54. data/lib/passive_support/core_ext/module/synchronization.rb +39 -0
  55. data/lib/passive_support/core_ext/numeric.rb +9 -0
  56. data/lib/passive_support/core_ext/numeric/bytes.rb +50 -0
  57. data/lib/passive_support/core_ext/numeric/conversions.rb +19 -0
  58. data/lib/passive_support/core_ext/numeric/time.rb +81 -0
  59. data/lib/passive_support/core_ext/object.rb +6 -0
  60. data/lib/passive_support/core_ext/object/blank.rb +76 -0
  61. data/lib/passive_support/core_ext/object/conversions.rb +15 -0
  62. data/lib/passive_support/core_ext/object/extending.rb +80 -0
  63. data/lib/passive_support/core_ext/object/instance_variables.rb +74 -0
  64. data/lib/passive_support/core_ext/object/misc.rb +90 -0
  65. data/lib/passive_support/core_ext/object/singleton_class.rb +13 -0
  66. data/lib/passive_support/core_ext/string.rb +1 -0
  67. data/lib/passive_support/core_ext/string/constantize.rb +7 -0
  68. data/lib/passive_support/core_ext/time.rb +46 -0
  69. data/lib/passive_support/core_ext/time/behavior.rb +13 -0
  70. data/lib/passive_support/core_ext/time/calculations.rb +313 -0
  71. data/lib/passive_support/core_ext/time/conversions.rb +90 -0
  72. data/lib/passive_support/core_ext/time/zones.rb +86 -0
  73. data/lib/passive_support/duration.rb +100 -0
  74. data/lib/passive_support/ordered_hash.rb +158 -0
  75. data/rails/init.rb +5 -0
  76. data/recipes/delayed_job.rb +1 -0
  77. data/spec/backend/active_record_job_spec.rb +46 -0
  78. data/spec/backend/data_mapper_job_spec.rb +16 -0
  79. data/spec/backend/mongo_mapper_job_spec.rb +94 -0
  80. data/spec/backend/shared_backend_spec.rb +268 -0
  81. data/spec/delayed_method_spec.rb +58 -0
  82. data/spec/performable_method_spec.rb +42 -0
  83. data/spec/sample_jobs.rb +25 -0
  84. data/spec/setup/active_record.rb +33 -0
  85. data/spec/setup/data_mapper.rb +24 -0
  86. data/spec/setup/mongo_mapper.rb +17 -0
  87. data/spec/spec_helper.rb +19 -0
  88. data/spec/story_spec.rb +17 -0
  89. data/spec/worker_spec.rb +225 -0
  90. data/tasks/jobs.rake +1 -0
  91. metadata +323 -0
@@ -0,0 +1,268 @@
1
+ shared_examples_for 'a backend' do
2
+ def create_job(opts = {})
3
+ @backend.create(opts.merge(:payload_object => SimpleJob.new))
4
+ end
5
+
6
+ before do
7
+ Delayed::Worker.max_priority = nil
8
+ Delayed::Worker.min_priority = nil
9
+ SimpleJob.runs = 0
10
+ end
11
+
12
+ it "should set run_at automatically if not set" do
13
+ @backend.create(:payload_object => ErrorJob.new ).run_at.should_not be_nil
14
+ end
15
+
16
+ it "should not set run_at automatically if already set" do
17
+ later = @backend.db_time_now + 5.minutes
18
+ @backend.create(:payload_object => ErrorJob.new, :run_at => later).run_at.should be_close(later, 1)
19
+ end
20
+
21
+ it "should raise ArgumentError when handler doesn't respond_to :perform" do
22
+ lambda { @backend.enqueue(Object.new) }.should raise_error(ArgumentError)
23
+ end
24
+
25
+ it "should increase count after enqueuing items" do
26
+ @backend.enqueue SimpleJob.new
27
+ @backend.count.should == 1
28
+ end
29
+
30
+ it "should be able to set priority when enqueuing items" do
31
+ @job = @backend.enqueue SimpleJob.new, 5
32
+ @job.priority.should == 5
33
+ end
34
+
35
+ it "should be able to set run_at when enqueuing items" do
36
+ later = @backend.db_time_now + 5.minutes
37
+ @job = @backend.enqueue SimpleJob.new, 5, later
38
+ @job.run_at.should be_close(later, 1)
39
+ end
40
+
41
+ it "should work with jobs in modules" do
42
+ M::ModuleJob.runs = 0
43
+ job = @backend.enqueue M::ModuleJob.new
44
+ lambda { job.invoke_job }.should change { M::ModuleJob.runs }.from(0).to(1)
45
+ end
46
+
47
+ it "should raise an DeserializationError when the job class is totally unknown" do
48
+ job = @backend.new :handler => "--- !ruby/object:JobThatDoesNotExist {}"
49
+ lambda { job.payload_object.perform }.should raise_error(Delayed::Backend::DeserializationError)
50
+ end
51
+
52
+ it "should try to load the class when it is unknown at the time of the deserialization" do
53
+ job = @backend.new :handler => "--- !ruby/object:JobThatDoesNotExist {}"
54
+ job.should_receive(:attempt_to_load).with('JobThatDoesNotExist').and_return(true)
55
+ lambda { job.payload_object.perform }.should raise_error(Delayed::Backend::DeserializationError)
56
+ end
57
+
58
+ it "should try include the namespace when loading unknown objects" do
59
+ job = @backend.new :handler => "--- !ruby/object:Delayed::JobThatDoesNotExist {}"
60
+ job.should_receive(:attempt_to_load).with('Delayed::JobThatDoesNotExist').and_return(true)
61
+ lambda { job.payload_object.perform }.should raise_error(Delayed::Backend::DeserializationError)
62
+ end
63
+
64
+ it "should also try to load structs when they are unknown (raises TypeError)" do
65
+ job = @backend.new :handler => "--- !ruby/struct:JobThatDoesNotExist {}"
66
+ job.should_receive(:attempt_to_load).with('JobThatDoesNotExist').and_return(true)
67
+ lambda { job.payload_object.perform }.should raise_error(Delayed::Backend::DeserializationError)
68
+ end
69
+
70
+ it "should try include the namespace when loading unknown structs" do
71
+ job = @backend.new :handler => "--- !ruby/struct:Delayed::JobThatDoesNotExist {}"
72
+ job.should_receive(:attempt_to_load).with('Delayed::JobThatDoesNotExist').and_return(true)
73
+ lambda { job.payload_object.perform }.should raise_error(Delayed::Backend::DeserializationError)
74
+ end
75
+
76
+ describe "find_available" do
77
+ it "should not find failed jobs" do
78
+ @job = create_job :attempts => 50, :failed_at => @backend.db_time_now
79
+ @backend.find_available('worker', 5, 1.second).should_not include(@job)
80
+ end
81
+
82
+ it "should not find jobs scheduled for the future" do
83
+ @job = create_job :run_at => (@backend.db_time_now + 1.minute)
84
+ @backend.find_available('worker', 5, 4.hours).should_not include(@job)
85
+ end
86
+
87
+ it "should not find jobs locked by another worker" do
88
+ @job = create_job(:locked_by => 'other_worker', :locked_at => @backend.db_time_now - 1.minute)
89
+ @backend.find_available('worker', 5, 4.hours).should_not include(@job)
90
+ end
91
+
92
+ it "should find open jobs" do
93
+ @job = create_job
94
+ @backend.find_available('worker', 5, 4.hours).should include(@job)
95
+ end
96
+
97
+ it "should find expired jobs" do
98
+ @job = create_job(:locked_by => 'worker', :locked_at => @backend.db_time_now - 2.minutes)
99
+ @backend.find_available('worker', 5, 1.minute).should include(@job)
100
+ end
101
+
102
+ it "should find own jobs" do
103
+ @job = create_job(:locked_by => 'worker', :locked_at => (@backend.db_time_now - 1.minutes))
104
+ @backend.find_available('worker', 5, 4.hours).should include(@job)
105
+ end
106
+
107
+ it "should find only the right amount of jobs" do
108
+ 10.times { create_job }
109
+ @backend.find_available('worker', 7, 4.hours).should have(7).jobs
110
+ end
111
+ end
112
+
113
+ context "when another worker is already performing an task, it" do
114
+
115
+ before :each do
116
+ @job = @backend.create :payload_object => SimpleJob.new, :locked_by => 'worker1', :locked_at => @backend.db_time_now - 5.minutes
117
+ end
118
+
119
+ it "should not allow a second worker to get exclusive access" do
120
+ @job.lock_exclusively!(4.hours, 'worker2').should == false
121
+ end
122
+
123
+ it "should allow a second worker to get exclusive access if the timeout has passed" do
124
+ @job.lock_exclusively!(1.minute, 'worker2').should == true
125
+ end
126
+
127
+ it "should be able to get access to the task if it was started more then max_age ago" do
128
+ @job.locked_at = 5.hours.ago
129
+ @job.save
130
+
131
+ @job.lock_exclusively! 4.hours, 'worker2'
132
+ @job.reload
133
+ @job.locked_by.should == 'worker2'
134
+ @job.locked_at.should > 1.minute.ago
135
+ end
136
+
137
+ it "should not be found by another worker" do
138
+ @backend.find_available('worker2', 1, 6.minutes).length.should == 0
139
+ end
140
+
141
+ it "should be found by another worker if the time has expired" do
142
+ @backend.find_available('worker2', 1, 4.minutes).length.should == 1
143
+ end
144
+
145
+ it "should be able to get exclusive access again when the worker name is the same" do
146
+ @job.lock_exclusively!(5.minutes, 'worker1').should be_true
147
+ @job.lock_exclusively!(5.minutes, 'worker1').should be_true
148
+ @job.lock_exclusively!(5.minutes, 'worker1').should be_true
149
+ end
150
+ end
151
+
152
+ context "when another worker has worked on a task since the job was found to be available, it" do
153
+
154
+ before :each do
155
+ @job = @backend.create :payload_object => SimpleJob.new
156
+ @job_copy_for_worker_2 = @backend.find(@job.id)
157
+ end
158
+
159
+ it "should not allow a second worker to get exclusive access if already successfully processed by worker1" do
160
+ @job.destroy
161
+ @job_copy_for_worker_2.lock_exclusively!(4.hours, 'worker2').should == false
162
+ end
163
+
164
+ it "should not allow a second worker to get exclusive access if failed to be processed by worker1 and run_at time is now in future (due to backing off behaviour)" do
165
+ @job.update_attributes(:attempts => 1, :run_at => 1.day.from_now)
166
+ @job_copy_for_worker_2.lock_exclusively!(4.hours, 'worker2').should == false
167
+ end
168
+ end
169
+
170
+ context "#name" do
171
+ it "should be the class name of the job that was enqueued" do
172
+ @backend.create(:payload_object => ErrorJob.new ).name.should == 'ErrorJob'
173
+ end
174
+
175
+ it "should be the method that will be called if its a performable method object" do
176
+ @job = Story.send_later(:create)
177
+ @job.name.should == "Story.create"
178
+ end
179
+
180
+ it "should be the instance method that will be called if its a performable method object" do
181
+ story = Story.create(:text => "...")
182
+ story.should be_valid
183
+ story.respond_to?(:dump_for_delayed_job).should be_true
184
+ @job = story.send_later(:save)
185
+ @job.name.should == 'Story#save'
186
+ end
187
+ end
188
+
189
+ context "worker prioritization" do
190
+ before(:each) do
191
+ Delayed::Worker.max_priority = nil
192
+ Delayed::Worker.min_priority = nil
193
+ end
194
+
195
+ it "should fetch jobs ordered by priority" do
196
+ 10.times { @backend.enqueue SimpleJob.new, rand(10) }
197
+ jobs = @backend.find_available('worker', 10)
198
+ jobs.size.should == 10
199
+ jobs.each_cons(2) do |a, b|
200
+ a.priority.should <= b.priority
201
+ end
202
+ end
203
+
204
+ it "should only find jobs greater than or equal to min priority" do
205
+ min = 5
206
+ Delayed::Worker.min_priority = min
207
+ 10.times {|i| @backend.enqueue SimpleJob.new, i }
208
+ jobs = @backend.find_available('worker', 10)
209
+ jobs.each {|job| job.priority.should >= min}
210
+ end
211
+
212
+ it "should only find jobs less than or equal to max priority" do
213
+ max = 5
214
+ Delayed::Worker.max_priority = max
215
+ 10.times {|i| @backend.enqueue SimpleJob.new, i }
216
+ jobs = @backend.find_available('worker', 10)
217
+ jobs.each {|job| job.priority.should <= max}
218
+ end
219
+ end
220
+
221
+ context "clear_locks!" do
222
+ before do
223
+ @job = create_job(:locked_by => 'worker', :locked_at => @backend.db_time_now)
224
+ end
225
+
226
+ it "should clear locks for the given worker" do
227
+ @backend.clear_locks!('worker')
228
+ @backend.find_available('worker2', 5, 1.minute).should include(@job)
229
+ end
230
+
231
+ it "should not clear locks for other workers" do
232
+ @backend.clear_locks!('worker1')
233
+ @backend.find_available('worker1', 5, 1.minute).should_not include(@job)
234
+ end
235
+ end
236
+
237
+ context "unlock" do
238
+ before do
239
+ @job = create_job(:locked_by => 'worker', :locked_at => @backend.db_time_now)
240
+ end
241
+
242
+ it "should clear locks" do
243
+ @job.unlock
244
+ @job.locked_by.should be_nil
245
+ @job.locked_at.should be_nil
246
+ end
247
+ end
248
+
249
+ context "large handler" do
250
+ @@text = %{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus eu vehicula augue. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Quisque odio lectus, volutpat sed dictum rutrum, interdum aliquam neque. Vivamus quis velit nisi, quis dictum purus. Duis magna nisi, faucibus nec molestie vitae, dictum eget odio. Nunc nulla mauris, vestibulum at dapibus nec, dapibus et lectus. Nullam sapien lacus, consectetur eget mattis in, rhoncus sed ipsum. Nullam nec nibh nisl. Integer ut erat in arcu feugiat semper. Nulla gravida sapien quam. Vestibulum pharetra elementum posuere. Fusce mattis justo auctor nibh facilisis vitae consectetur nibh vehicula.
251
+
252
+ Ut at pharetra justo. Donec dictum ornare tortor in feugiat. Sed ac purus sem. Aenean dignissim, erat vel bibendum mollis, elit neque mollis mauris, vitae pretium diam enim non leo. Aliquam aliquet, odio id iaculis varius, metus nibh fermentum sapien, a euismod turpis lectus sit amet turpis. Morbi sapien est, scelerisque in placerat in, varius nec mauris. Aliquam erat volutpat. Quisque suscipit tincidunt libero, sed tincidunt libero iaculis et. Vivamus sed faucibus elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus dignissim sem sed tortor semper et lacinia leo viverra. Nulla nec quam at arcu ullamcorper imperdiet vitae in ligula. Quisque placerat vulputate orci sit amet tempor. Duis sed quam nulla. Cras quis mi nibh, at euismod velit. Etiam nec nunc libero, sed condimentum diam.
253
+
254
+ Duis nec mauris in est suscipit viverra a in nibh. Suspendisse nec nulla tortor. Etiam et nulla tellus. Nam feugiat adipiscing commodo. Curabitur scelerisque varius lacus non hendrerit. Vivamus nec enim non turpis auctor tempus sit amet in nisi. Sed ligula nulla, condimentum sed tempor vel, imperdiet id mauris. Quisque mollis ante eu magna tempus porttitor. Integer est libero, consectetur sed tristique a, scelerisque id risus. Donec lacinia justo eget diam fringilla vitae egestas dolor feugiat. Vivamus massa ante, mattis et hendrerit nec, dictum vitae nulla. Pellentesque at nisl et odio suscipit ullamcorper cursus quis enim. Ut nec tellus molestie erat dignissim mollis. Curabitur quis ipsum sapien, sed tincidunt massa. Vestibulum volutpat pretium fringilla.
255
+
256
+ Integer at lorem sit amet nibh suscipit euismod et ut ante. Maecenas feugiat hendrerit dolor, eget egestas velit consequat eget. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Suspendisse ut nunc odio. Vivamus semper, sem vitae sollicitudin auctor, leo mi vulputate augue, eget venenatis libero nunc ut dolor. Phasellus vulputate, metus et dapibus tempus, tellus arcu ullamcorper leo, porttitor dictum lectus turpis blandit sapien. Pellentesque et accumsan justo. Maecenas elit nisi, tincidunt eget consequat a, laoreet et magna. Pellentesque venenatis felis ut massa ultrices bibendum. Duis vulputate tempor leo at bibendum. Curabitur aliquet, turpis sit amet porta porttitor, nibh mi vehicula dolor, suscipit aliquet mi augue quis magna. Praesent tellus turpis, malesuada at ultricies id, feugiat a urna. Curabitur sed mi magna.
257
+
258
+ Quisque adipiscing dignissim mollis. Aenean blandit, diam porttitor bibendum bibendum, leo neque tempus risus, in rutrum dolor elit a lorem. Aenean sollicitudin scelerisque ullamcorper. Nunc tristique ultricies nunc et imperdiet. Duis vitae egestas mauris. Suspendisse odio nisi, accumsan vel volutpat nec, aliquam vitae odio. Praesent elementum fermentum suscipit. Quisque quis tellus eu tellus bibendum luctus a quis nunc. Praesent dictum velit sed lacus dapibus ut ultricies mauris facilisis. Vivamus bibendum, ipsum sit amet facilisis consequat, leo lectus aliquam augue, eu consectetur magna nunc gravida sapien. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Duis tempor nisl ac odio molestie ut tincidunt purus varius. Nunc quis lorem nibh, vestibulum cursus lorem. Nunc sit amet est ut magna suscipit tempor vitae a augue.}
259
+
260
+ before do
261
+ @job = @backend.enqueue Delayed::PerformableMethod.new(@@text, :length, {})
262
+ end
263
+
264
+ it "should have an id" do
265
+ @job.id.should_not be_nil
266
+ end
267
+ end
268
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'random ruby objects' do
4
+ before { Delayed::Job.delete_all }
5
+
6
+ it "should respond_to :send_later method" do
7
+ Object.new.respond_to?(:send_later).should be_true
8
+ end
9
+
10
+ it "should raise a ArgumentError if send_later is called but the target method doesn't exist" do
11
+ lambda { Object.new.send_later(:method_that_deos_not_exist) }.should raise_error(NoMethodError)
12
+ end
13
+
14
+ it "should add a new entry to the job table when send_later is called on it" do
15
+ lambda { Object.new.send_later(:to_s) }.should change { Delayed::Job.count }.by(1)
16
+ end
17
+
18
+ it "should add a new entry to the job table when send_later is called on the class" do
19
+ lambda { Object.send_later(:to_s) }.should change { Delayed::Job.count }.by(1)
20
+ end
21
+
22
+ it "should call send later on methods which are wrapped with handle_asynchronously" do
23
+ story = Story.create :text => 'Once upon...'
24
+ Delayed::Job.count.should == 0
25
+
26
+ story.whatever(1, 5)
27
+
28
+ Delayed::Job.count.should == 1
29
+ job = Delayed::Job.first
30
+ job.payload_object.class.should == Delayed::PerformableMethod
31
+ job.payload_object.method.should == :whatever_without_send_later
32
+ job.payload_object.args.should == [1, 5]
33
+ job.payload_object.perform.should == 'Once upon...'
34
+ end
35
+
36
+ context "send_at" do
37
+ it "should queue a new job" do
38
+ lambda do
39
+ "string".send_at(1.hour.from_now, :length)
40
+ end.should change { Delayed::Job.count }.by(1)
41
+ end
42
+
43
+ it "should schedule the job in the future" do
44
+ time = 1.hour.from_now.utc.to_time
45
+ job = "string".send_at(time, :length)
46
+ job.run_at.to_i.should == time.to_i
47
+ end
48
+
49
+ it "should store payload as PerformableMethod" do
50
+ job = "string".send_at(1.hour.from_now, :count, 'r')
51
+ job.payload_object.class.should == Delayed::PerformableMethod
52
+ job.payload_object.method.should == :count
53
+ job.payload_object.args.should == ['r']
54
+ job.payload_object.perform.should == 1
55
+ end
56
+ end
57
+
58
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ class StoryReader
4
+ def read(story)
5
+ "Epilog: #{story.tell}"
6
+ end
7
+ end
8
+
9
+ describe Delayed::PerformableMethod do
10
+
11
+ it "should ignore ActiveRecord::RecordNotFound errors because they are permanent" do
12
+ story = Story.create :text => 'Once upon...'
13
+ p = Delayed::PerformableMethod.new(story, :tell, [])
14
+ story.destroy
15
+ lambda { p.perform }.should_not raise_error
16
+ end
17
+
18
+ it "should store the object as string if its an active record" do
19
+ story = Story.create :text => 'Once upon...'
20
+ p = Delayed::PerformableMethod.new(story, :tell, [])
21
+ p.class.should == Delayed::PerformableMethod
22
+ p.object.should == "LOAD;Story;#{story.id}"
23
+ p.method.should == :tell
24
+ p.args.should == []
25
+ p.perform.should == 'Once upon...'
26
+ end
27
+
28
+ it "should allow class methods to be called on ActiveRecord models" do
29
+ p = Delayed::PerformableMethod.new(Story, :count, [])
30
+ lambda { p.send(:load, p.object) }.should_not raise_error
31
+ end
32
+
33
+ it "should store arguments as string if they are active record objects" do
34
+ story = Story.create :text => 'Once upon...'
35
+ reader = StoryReader.new
36
+ p = Delayed::PerformableMethod.new(reader, :read, [story])
37
+ p.class.should == Delayed::PerformableMethod
38
+ p.method.should == :read
39
+ p.args.should == ["LOAD;Story;#{story.id}"]
40
+ p.perform.should == 'Epilog: Once upon...'
41
+ end
42
+ end
@@ -0,0 +1,25 @@
1
+ class SimpleJob
2
+ cattr_accessor :runs; self.runs = 0
3
+ def perform; @@runs += 1; end
4
+ end
5
+
6
+ class ErrorJob
7
+ cattr_accessor :runs; self.runs = 0
8
+ def perform; raise 'did not work'; end
9
+ end
10
+
11
+ class LongRunningJob
12
+ def perform; sleep 250; end
13
+ end
14
+
15
+ class OnPermanentFailureJob < SimpleJob
16
+ def on_permanent_failure
17
+ end
18
+ end
19
+
20
+ module M
21
+ class ModuleJob
22
+ cattr_accessor :runs; self.runs = 0
23
+ def perform; @@runs += 1; end
24
+ end
25
+ end
@@ -0,0 +1,33 @@
1
+ require 'active_record'
2
+
3
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
4
+ ActiveRecord::Base.logger = Delayed::Worker.logger
5
+ ActiveRecord::Migration.verbose = false
6
+
7
+ ActiveRecord::Schema.define do
8
+ create_table :delayed_jobs, :force => true do |table|
9
+ table.integer :priority, :default => 0
10
+ table.integer :attempts, :default => 0
11
+ table.text :handler
12
+ table.text :last_error
13
+ table.datetime :run_at
14
+ table.datetime :locked_at
15
+ table.datetime :failed_at
16
+ table.string :locked_by
17
+ table.timestamps
18
+ end
19
+
20
+ add_index :delayed_jobs, [:priority, :run_at], :name => 'delayed_jobs_priority'
21
+
22
+ create_table :stories, :force => true do |table|
23
+ table.string :text
24
+ end
25
+ end
26
+
27
+ # Purely useful for test cases...
28
+ class Story < ActiveRecord::Base
29
+ def tell; text; end
30
+ def whatever(n, _); tell*n; end
31
+
32
+ handle_asynchronously :whatever
33
+ end
@@ -0,0 +1,24 @@
1
+ require 'dm-core'
2
+ require 'dm-validations'
3
+
4
+ require 'delayed/backend/data_mapper'
5
+
6
+ class Story
7
+ include DataMapper::Resource
8
+ property :id, Serial, :key => true
9
+ property :text, Text
10
+
11
+ def tell
12
+ self.text
13
+ end
14
+
15
+ def whatever(n, _)
16
+ tell*n
17
+ end
18
+
19
+ handle_asynchronously :whatever
20
+ end
21
+
22
+ DataMapper.logger = Delayed::Worker.logger
23
+ DataMapper.setup(:default, 'sqlite3::memory:')
24
+ DataMapper.auto_migrate!