blue_light_special 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/README.rdoc +28 -0
  2. data/VERSION +1 -1
  3. data/app/controllers/blue_light_special/impersonations_controller.rb +6 -3
  4. data/test/rails_root/app/controllers/admin/admin_controller.rb +14 -0
  5. data/test/rails_root/app/controllers/admin/users_controller.rb +52 -0
  6. data/test/rails_root/config/initializers/blue_light_special.rb +20 -3
  7. data/test/rails_root/db/migrate/{20100305173127_blue_light_special_create_users.rb → 20100727214301_blue_light_special_create_users.rb} +4 -1
  8. data/test/rails_root/db/migrate/{20100305173129_create_delayed_jobs.rb → 20100727214302_create_delayed_jobs.rb} +0 -0
  9. data/test/rails_root/test/factories/user.rb +10 -0
  10. data/test/rails_root/test/integration/admin/users_test.rb +201 -0
  11. data/test/rails_root/test/integration/edit_profile_test.rb +35 -0
  12. data/test/rails_root/test/integration/facebook_test.rb +48 -36
  13. data/test/rails_root/test/integration/impersonation_test.rb +5 -4
  14. data/test/rails_root/test/integration/password_reset_test.rb +5 -4
  15. data/test/rails_root/test/integration/sign_in_test.rb +0 -6
  16. data/test/rails_root/test/integration/sign_up_test.rb +3 -40
  17. data/test/rails_root/vendor/gems/delayed_job-1.8.4/generators/delayed_job/delayed_job_generator.rb +22 -0
  18. data/test/rails_root/vendor/gems/delayed_job-1.8.4/generators/delayed_job/templates/migration.rb +20 -0
  19. data/test/rails_root/vendor/gems/delayed_job-1.8.4/init.rb +1 -0
  20. data/test/rails_root/vendor/gems/delayed_job-1.8.4/lib/delayed/command.rb +76 -0
  21. data/test/rails_root/vendor/gems/delayed_job-1.8.4/lib/delayed/job.rb +270 -0
  22. data/test/rails_root/vendor/gems/delayed_job-1.8.4/lib/delayed/message_sending.rb +22 -0
  23. data/test/rails_root/vendor/gems/delayed_job-1.8.4/lib/delayed/performable_method.rb +55 -0
  24. data/test/rails_root/vendor/gems/delayed_job-1.8.4/lib/delayed/recipes.rb +27 -0
  25. data/test/rails_root/vendor/gems/delayed_job-1.8.4/lib/delayed/tasks.rb +15 -0
  26. data/test/rails_root/vendor/gems/delayed_job-1.8.4/lib/delayed/worker.rb +54 -0
  27. data/test/rails_root/vendor/gems/delayed_job-1.8.4/lib/delayed_job.rb +13 -0
  28. data/test/rails_root/vendor/gems/delayed_job-1.8.4/recipes/delayed_job.rb +1 -0
  29. data/test/rails_root/vendor/gems/delayed_job-1.8.4/spec/database.rb +42 -0
  30. data/test/rails_root/vendor/gems/delayed_job-1.8.4/spec/delayed_method_spec.rb +150 -0
  31. data/test/rails_root/vendor/gems/delayed_job-1.8.4/spec/job_spec.rb +406 -0
  32. data/test/rails_root/vendor/gems/delayed_job-1.8.4/spec/story_spec.rb +17 -0
  33. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.4/generators/formtastic_stylesheets/formtastic_stylesheets_generator.rb +21 -0
  34. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.4/lib/formtastic.rb +1312 -0
  35. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.4/lib/justin_french/formtastic.rb +10 -0
  36. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.4/rails/init.rb +3 -0
  37. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.4/spec/formtastic_spec.rb +3079 -0
  38. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.4/spec/test_helper.rb +14 -0
  39. data/test/rails_root/vendor/gems/mini_fb-0.2.2/lib/mini_fb.rb +284 -0
  40. data/test/rails_root/vendor/gems/mini_fb-0.2.2/test/test_mini_fb.rb +29 -0
  41. metadata +111 -6
@@ -0,0 +1,22 @@
1
+ module Delayed
2
+ module MessageSending
3
+ def send_later(method, *args)
4
+ Delayed::Job.enqueue Delayed::PerformableMethod.new(self, method.to_sym, args)
5
+ end
6
+
7
+ def send_at(time, method, *args)
8
+ Delayed::Job.enqueue(Delayed::PerformableMethod.new(self, method.to_sym, args), 0, time)
9
+ end
10
+
11
+ module ClassMethods
12
+ def handle_asynchronously(method)
13
+ aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
14
+ with_method, without_method = "#{aliased_method}_with_send_later#{punctuation}", "#{aliased_method}_without_send_later#{punctuation}"
15
+ define_method(with_method) do |*args|
16
+ send_later(without_method, *args)
17
+ end
18
+ alias_method_chain method, :send_later
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,55 @@
1
+ module Delayed
2
+ class PerformableMethod < Struct.new(:object, :method, :args)
3
+ CLASS_STRING_FORMAT = /^CLASS\:([A-Z][\w\:]+)$/
4
+ AR_STRING_FORMAT = /^AR\:([A-Z][\w\:]+)\:(\d+)$/
5
+
6
+ def initialize(object, method, args)
7
+ raise NoMethodError, "undefined method `#{method}' for #{self.inspect}" unless object.respond_to?(method)
8
+
9
+ self.object = dump(object)
10
+ self.args = args.map { |a| dump(a) }
11
+ self.method = method.to_sym
12
+ end
13
+
14
+ def display_name
15
+ case self.object
16
+ when CLASS_STRING_FORMAT then "#{$1}.#{method}"
17
+ when AR_STRING_FORMAT then "#{$1}##{method}"
18
+ else "Unknown##{method}"
19
+ end
20
+ end
21
+
22
+ def perform
23
+ load(object).send(method, *args.map{|a| load(a)})
24
+ rescue ActiveRecord::RecordNotFound
25
+ # We cannot do anything about objects which were deleted in the meantime
26
+ true
27
+ end
28
+
29
+ private
30
+
31
+ def load(arg)
32
+ case arg
33
+ when CLASS_STRING_FORMAT then $1.constantize
34
+ when AR_STRING_FORMAT then $1.constantize.find($2)
35
+ else arg
36
+ end
37
+ end
38
+
39
+ def dump(arg)
40
+ case arg
41
+ when Class then class_to_string(arg)
42
+ when ActiveRecord::Base then ar_to_string(arg)
43
+ else arg
44
+ end
45
+ end
46
+
47
+ def ar_to_string(obj)
48
+ "AR:#{obj.class}:#{obj.id}"
49
+ end
50
+
51
+ def class_to_string(obj)
52
+ "CLASS:#{obj.name}"
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,27 @@
1
+ # Capistrano Recipes for managing delayed_job
2
+ #
3
+ # Add these callbacks to have the delayed_job process restart when the server
4
+ # is restarted:
5
+ #
6
+ # after "deploy:stop", "delayed_job:stop"
7
+ # after "deploy:start", "delayed_job:start"
8
+ # after "deploy:restart", "delayed_job:restart"
9
+
10
+ Capistrano::Configuration.instance.load do
11
+ namespace :delayed_job do
12
+ desc "Stop the delayed_job process"
13
+ task :stop, :roles => :app do
14
+ run "cd #{current_path}; RAILS_ENV=#{rails_env} script/delayed_job stop"
15
+ end
16
+
17
+ desc "Start the delayed_job process"
18
+ task :start, :roles => :app do
19
+ run "cd #{current_path}; RAILS_ENV=#{rails_env} script/delayed_job start"
20
+ end
21
+
22
+ desc "Restart the delayed_job process"
23
+ task :restart, :roles => :app do
24
+ run "cd #{current_path}; RAILS_ENV=#{rails_env} script/delayed_job restart"
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ # Re-definitions are appended to existing tasks
2
+ task :environment
3
+ task :merb_env
4
+
5
+ namespace :jobs do
6
+ desc "Clear the delayed_job queue."
7
+ task :clear => [:merb_env, :environment] do
8
+ Delayed::Job.delete_all
9
+ end
10
+
11
+ desc "Start a delayed_job worker."
12
+ task :work => [:merb_env, :environment] do
13
+ Delayed::Worker.new(:min_priority => ENV['MIN_PRIORITY'], :max_priority => ENV['MAX_PRIORITY']).start
14
+ end
15
+ end
@@ -0,0 +1,54 @@
1
+ module Delayed
2
+ class Worker
3
+ SLEEP = 5
4
+
5
+ cattr_accessor :logger
6
+ self.logger = if defined?(Merb::Logger)
7
+ Merb.logger
8
+ elsif defined?(RAILS_DEFAULT_LOGGER)
9
+ RAILS_DEFAULT_LOGGER
10
+ end
11
+
12
+ def initialize(options={})
13
+ @quiet = options[:quiet]
14
+ Delayed::Job.min_priority = options[:min_priority] if options.has_key?(:min_priority)
15
+ Delayed::Job.max_priority = options[:max_priority] if options.has_key?(:max_priority)
16
+ end
17
+
18
+ def start
19
+ say "*** Starting job worker #{Delayed::Job.worker_name}"
20
+
21
+ trap('TERM') { say 'Exiting...'; $exit = true }
22
+ trap('INT') { say 'Exiting...'; $exit = true }
23
+
24
+ loop do
25
+ result = nil
26
+
27
+ realtime = Benchmark.realtime do
28
+ result = Delayed::Job.work_off
29
+ end
30
+
31
+ count = result.sum
32
+
33
+ break if $exit
34
+
35
+ if count.zero?
36
+ sleep(SLEEP)
37
+ else
38
+ say "#{count} jobs processed at %.4f j/s, %d failed ..." % [count / realtime, result.last]
39
+ end
40
+
41
+ break if $exit
42
+ end
43
+
44
+ ensure
45
+ Delayed::Job.clear_locks!
46
+ end
47
+
48
+ def say(text)
49
+ puts text unless @quiet
50
+ logger.info text if logger
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,13 @@
1
+ autoload :ActiveRecord, 'activerecord'
2
+
3
+ require File.dirname(__FILE__) + '/delayed/message_sending'
4
+ require File.dirname(__FILE__) + '/delayed/performable_method'
5
+ require File.dirname(__FILE__) + '/delayed/job'
6
+ require File.dirname(__FILE__) + '/delayed/worker'
7
+
8
+ Object.send(:include, Delayed::MessageSending)
9
+ Module.send(:include, Delayed::MessageSending::ClassMethods)
10
+
11
+ if defined?(Merb::Plugins)
12
+ Merb::Plugins.add_rakefiles File.dirname(__FILE__) / 'delayed' / 'tasks'
13
+ end
@@ -0,0 +1 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'delayed', 'recipes'))
@@ -0,0 +1,42 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+ $:.unshift(File.dirname(__FILE__) + '/../../rspec/lib')
3
+
4
+ require 'rubygems'
5
+ require 'active_record'
6
+ gem 'sqlite3-ruby'
7
+
8
+ require File.dirname(__FILE__) + '/../init'
9
+ require 'spec'
10
+
11
+ ActiveRecord::Base.logger = Logger.new('/tmp/dj.log')
12
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => '/tmp/jobs.sqlite')
13
+ ActiveRecord::Migration.verbose = false
14
+
15
+ ActiveRecord::Schema.define do
16
+
17
+ create_table :delayed_jobs, :force => true do |table|
18
+ table.integer :priority, :default => 0
19
+ table.integer :attempts, :default => 0
20
+ table.text :handler
21
+ table.string :last_error
22
+ table.datetime :run_at
23
+ table.datetime :locked_at
24
+ table.string :locked_by
25
+ table.datetime :failed_at
26
+ table.timestamps
27
+ end
28
+
29
+ create_table :stories, :force => true do |table|
30
+ table.string :text
31
+ end
32
+
33
+ end
34
+
35
+
36
+ # Purely useful for test cases...
37
+ class Story < ActiveRecord::Base
38
+ def tell; text; end
39
+ def whatever(n, _); tell*n; end
40
+
41
+ handle_asynchronously :whatever
42
+ end
@@ -0,0 +1,150 @@
1
+ require File.dirname(__FILE__) + '/database'
2
+
3
+ class SimpleJob
4
+ cattr_accessor :runs; self.runs = 0
5
+ def perform; @@runs += 1; end
6
+ end
7
+
8
+ class RandomRubyObject
9
+ def say_hello
10
+ 'hello'
11
+ end
12
+ end
13
+
14
+ class ErrorObject
15
+
16
+ def throw
17
+ raise ActiveRecord::RecordNotFound, '...'
18
+ false
19
+ end
20
+
21
+ end
22
+
23
+ class StoryReader
24
+
25
+ def read(story)
26
+ "Epilog: #{story.tell}"
27
+ end
28
+
29
+ end
30
+
31
+ class StoryReader
32
+
33
+ def read(story)
34
+ "Epilog: #{story.tell}"
35
+ end
36
+
37
+ end
38
+
39
+ describe 'random ruby objects' do
40
+ before { Delayed::Job.delete_all }
41
+
42
+ it "should respond_to :send_later method" do
43
+
44
+ RandomRubyObject.new.respond_to?(:send_later)
45
+
46
+ end
47
+
48
+ it "should raise a ArgumentError if send_later is called but the target method doesn't exist" do
49
+ lambda { RandomRubyObject.new.send_later(:method_that_deos_not_exist) }.should raise_error(NoMethodError)
50
+ end
51
+
52
+ it "should add a new entry to the job table when send_later is called on it" do
53
+ Delayed::Job.count.should == 0
54
+
55
+ RandomRubyObject.new.send_later(:to_s)
56
+
57
+ Delayed::Job.count.should == 1
58
+ end
59
+
60
+ it "should add a new entry to the job table when send_later is called on the class" do
61
+ Delayed::Job.count.should == 0
62
+
63
+ RandomRubyObject.send_later(:to_s)
64
+
65
+ Delayed::Job.count.should == 1
66
+ end
67
+
68
+ it "should run get the original method executed when the job is performed" do
69
+
70
+ RandomRubyObject.new.send_later(:say_hello)
71
+
72
+ Delayed::Job.count.should == 1
73
+ end
74
+
75
+ it "should ignore ActiveRecord::RecordNotFound errors because they are permanent" do
76
+
77
+ ErrorObject.new.send_later(:throw)
78
+
79
+ Delayed::Job.count.should == 1
80
+
81
+ Delayed::Job.reserve_and_run_one_job
82
+
83
+ Delayed::Job.count.should == 0
84
+
85
+ end
86
+
87
+ it "should store the object as string if its an active record" do
88
+ story = Story.create :text => 'Once upon...'
89
+ story.send_later(:tell)
90
+
91
+ job = Delayed::Job.find(:first)
92
+ job.payload_object.class.should == Delayed::PerformableMethod
93
+ job.payload_object.object.should == "AR:Story:#{story.id}"
94
+ job.payload_object.method.should == :tell
95
+ job.payload_object.args.should == []
96
+ job.payload_object.perform.should == 'Once upon...'
97
+ end
98
+
99
+ it "should store arguments as string if they an active record" do
100
+
101
+ story = Story.create :text => 'Once upon...'
102
+
103
+ reader = StoryReader.new
104
+ reader.send_later(:read, story)
105
+
106
+ job = Delayed::Job.find(:first)
107
+ job.payload_object.class.should == Delayed::PerformableMethod
108
+ job.payload_object.method.should == :read
109
+ job.payload_object.args.should == ["AR:Story:#{story.id}"]
110
+ job.payload_object.perform.should == 'Epilog: Once upon...'
111
+ end
112
+
113
+ it "should call send later on methods which are wrapped with handle_asynchronously" do
114
+ story = Story.create :text => 'Once upon...'
115
+
116
+ Delayed::Job.count.should == 0
117
+
118
+ story.whatever(1, 5)
119
+
120
+ Delayed::Job.count.should == 1
121
+ job = Delayed::Job.find(:first)
122
+ job.payload_object.class.should == Delayed::PerformableMethod
123
+ job.payload_object.method.should == :whatever_without_send_later
124
+ job.payload_object.args.should == [1, 5]
125
+ job.payload_object.perform.should == 'Once upon...'
126
+ end
127
+
128
+ context "send_at" do
129
+ it "should queue a new job" do
130
+ lambda do
131
+ "string".send_at(1.hour.from_now, :length)
132
+ end.should change { Delayed::Job.count }.by(1)
133
+ end
134
+
135
+ it "should schedule the job in the future" do
136
+ time = 1.hour.from_now
137
+ job = "string".send_at(time, :length)
138
+ job.run_at.should == time
139
+ end
140
+
141
+ it "should store payload as PerformableMethod" do
142
+ job = "string".send_at(1.hour.from_now, :count, 'r')
143
+ job.payload_object.class.should == Delayed::PerformableMethod
144
+ job.payload_object.method.should == :count
145
+ job.payload_object.args.should == ['r']
146
+ job.payload_object.perform.should == 1
147
+ end
148
+ end
149
+
150
+ end
@@ -0,0 +1,406 @@
1
+ require File.dirname(__FILE__) + '/database'
2
+
3
+ class SimpleJob
4
+ cattr_accessor :runs; self.runs = 0
5
+ def perform; @@runs += 1; end
6
+ end
7
+
8
+ class ErrorJob
9
+ cattr_accessor :runs; self.runs = 0
10
+ def perform; raise 'did not work'; end
11
+ end
12
+
13
+ class LongRunningJob
14
+ def perform; sleep 250; end
15
+ end
16
+
17
+ module M
18
+ class ModuleJob
19
+ cattr_accessor :runs; self.runs = 0
20
+ def perform; @@runs += 1; end
21
+ end
22
+
23
+ end
24
+
25
+ describe Delayed::Job do
26
+ before do
27
+ Delayed::Job.max_priority = nil
28
+ Delayed::Job.min_priority = nil
29
+
30
+ Delayed::Job.delete_all
31
+ end
32
+
33
+ before(:each) do
34
+ SimpleJob.runs = 0
35
+ end
36
+
37
+ it "should set run_at automatically if not set" do
38
+ Delayed::Job.create(:payload_object => ErrorJob.new ).run_at.should_not == nil
39
+ end
40
+
41
+ it "should not set run_at automatically if already set" do
42
+ later = 5.minutes.from_now
43
+ Delayed::Job.create(:payload_object => ErrorJob.new, :run_at => later).run_at.should == later
44
+ end
45
+
46
+ it "should raise ArgumentError when handler doesn't respond_to :perform" do
47
+ lambda { Delayed::Job.enqueue(Object.new) }.should raise_error(ArgumentError)
48
+ end
49
+
50
+ it "should increase count after enqueuing items" do
51
+ Delayed::Job.enqueue SimpleJob.new
52
+ Delayed::Job.count.should == 1
53
+ end
54
+
55
+ it "should be able to set priority when enqueuing items" do
56
+ Delayed::Job.enqueue SimpleJob.new, 5
57
+ Delayed::Job.first.priority.should == 5
58
+ end
59
+
60
+ it "should be able to set run_at when enqueuing items" do
61
+ later = 5.minutes.from_now
62
+ Delayed::Job.enqueue SimpleJob.new, 5, later
63
+
64
+ # use be close rather than equal to because millisecond values cn be lost in DB round trip
65
+ Delayed::Job.first.run_at.should be_close(later, 1)
66
+ end
67
+
68
+ it "should call perform on jobs when running work_off" do
69
+ SimpleJob.runs.should == 0
70
+
71
+ Delayed::Job.enqueue SimpleJob.new
72
+ Delayed::Job.work_off
73
+
74
+ SimpleJob.runs.should == 1
75
+ end
76
+
77
+
78
+ it "should work with eval jobs" do
79
+ $eval_job_ran = false
80
+
81
+ Delayed::Job.enqueue do <<-JOB
82
+ $eval_job_ran = true
83
+ JOB
84
+ end
85
+
86
+ Delayed::Job.work_off
87
+
88
+ $eval_job_ran.should == true
89
+ end
90
+
91
+ it "should work with jobs in modules" do
92
+ M::ModuleJob.runs.should == 0
93
+
94
+ Delayed::Job.enqueue M::ModuleJob.new
95
+ Delayed::Job.work_off
96
+
97
+ M::ModuleJob.runs.should == 1
98
+ end
99
+
100
+ it "should re-schedule by about 1 second at first and increment this more and more minutes when it fails to execute properly" do
101
+ Delayed::Job.enqueue ErrorJob.new
102
+ Delayed::Job.work_off(1)
103
+
104
+ job = Delayed::Job.find(:first)
105
+
106
+ job.last_error.should =~ /did not work/
107
+ job.last_error.should =~ /job_spec.rb:10:in `perform'/
108
+ job.attempts.should == 1
109
+
110
+ job.run_at.should > Delayed::Job.db_time_now - 10.minutes
111
+ job.run_at.should < Delayed::Job.db_time_now + 10.minutes
112
+ end
113
+
114
+ it "should raise an DeserializationError when the job class is totally unknown" do
115
+
116
+ job = Delayed::Job.new
117
+ job['handler'] = "--- !ruby/object:JobThatDoesNotExist {}"
118
+
119
+ lambda { job.payload_object.perform }.should raise_error(Delayed::DeserializationError)
120
+ end
121
+
122
+ it "should try to load the class when it is unknown at the time of the deserialization" do
123
+ job = Delayed::Job.new
124
+ job['handler'] = "--- !ruby/object:JobThatDoesNotExist {}"
125
+
126
+ job.should_receive(:attempt_to_load).with('JobThatDoesNotExist').and_return(true)
127
+
128
+ lambda { job.payload_object.perform }.should raise_error(Delayed::DeserializationError)
129
+ end
130
+
131
+ it "should try include the namespace when loading unknown objects" do
132
+ job = Delayed::Job.new
133
+ job['handler'] = "--- !ruby/object:Delayed::JobThatDoesNotExist {}"
134
+ job.should_receive(:attempt_to_load).with('Delayed::JobThatDoesNotExist').and_return(true)
135
+ lambda { job.payload_object.perform }.should raise_error(Delayed::DeserializationError)
136
+ end
137
+
138
+ it "should also try to load structs when they are unknown (raises TypeError)" do
139
+ job = Delayed::Job.new
140
+ job['handler'] = "--- !ruby/struct:JobThatDoesNotExist {}"
141
+
142
+ job.should_receive(:attempt_to_load).with('JobThatDoesNotExist').and_return(true)
143
+
144
+ lambda { job.payload_object.perform }.should raise_error(Delayed::DeserializationError)
145
+ end
146
+
147
+ it "should try include the namespace when loading unknown structs" do
148
+ job = Delayed::Job.new
149
+ job['handler'] = "--- !ruby/struct:Delayed::JobThatDoesNotExist {}"
150
+
151
+ job.should_receive(:attempt_to_load).with('Delayed::JobThatDoesNotExist').and_return(true)
152
+ lambda { job.payload_object.perform }.should raise_error(Delayed::DeserializationError)
153
+ end
154
+
155
+ context "reschedule" do
156
+ before do
157
+ @job = Delayed::Job.create :payload_object => SimpleJob.new
158
+ end
159
+
160
+ context "and we want to destroy jobs" do
161
+ before do
162
+ Delayed::Job.destroy_failed_jobs = true
163
+ end
164
+
165
+ it "should be destroyed if it failed more than MAX_ATTEMPTS times" do
166
+ @job.should_receive(:destroy)
167
+ Delayed::Job::MAX_ATTEMPTS.times { @job.reschedule 'FAIL' }
168
+ end
169
+
170
+ it "should not be destroyed if failed fewer than MAX_ATTEMPTS times" do
171
+ @job.should_not_receive(:destroy)
172
+ (Delayed::Job::MAX_ATTEMPTS - 1).times { @job.reschedule 'FAIL' }
173
+ end
174
+ end
175
+
176
+ context "and we don't want to destroy jobs" do
177
+ before do
178
+ Delayed::Job.destroy_failed_jobs = false
179
+ end
180
+
181
+ it "should be failed if it failed more than MAX_ATTEMPTS times" do
182
+ @job.reload.failed_at.should == nil
183
+ Delayed::Job::MAX_ATTEMPTS.times { @job.reschedule 'FAIL' }
184
+ @job.reload.failed_at.should_not == nil
185
+ end
186
+
187
+ it "should not be failed if it failed fewer than MAX_ATTEMPTS times" do
188
+ (Delayed::Job::MAX_ATTEMPTS - 1).times { @job.reschedule 'FAIL' }
189
+ @job.reload.failed_at.should == nil
190
+ end
191
+
192
+ end
193
+ end
194
+
195
+ it "should fail after MAX_RUN_TIME" do
196
+ @job = Delayed::Job.create :payload_object => LongRunningJob.new
197
+ Delayed::Job.reserve_and_run_one_job(1.second)
198
+ @job.reload.last_error.should =~ /expired/
199
+ @job.attempts.should == 1
200
+ end
201
+
202
+ it "should never find failed jobs" do
203
+ @job = Delayed::Job.create :payload_object => SimpleJob.new, :attempts => 50, :failed_at => Time.now
204
+ Delayed::Job.find_available(1).length.should == 0
205
+ end
206
+
207
+ context "when another worker is already performing an task, it" do
208
+
209
+ before :each do
210
+ Delayed::Job.worker_name = 'worker1'
211
+ @job = Delayed::Job.create :payload_object => SimpleJob.new, :locked_by => 'worker1', :locked_at => Delayed::Job.db_time_now - 5.minutes
212
+ end
213
+
214
+ it "should not allow a second worker to get exclusive access" do
215
+ @job.lock_exclusively!(4.hours, 'worker2').should == false
216
+ end
217
+
218
+ it "should allow a second worker to get exclusive access if the timeout has passed" do
219
+ @job.lock_exclusively!(1.minute, 'worker2').should == true
220
+ end
221
+
222
+ it "should be able to get access to the task if it was started more then max_age ago" do
223
+ @job.locked_at = 5.hours.ago
224
+ @job.save
225
+
226
+ @job.lock_exclusively! 4.hours, 'worker2'
227
+ @job.reload
228
+ @job.locked_by.should == 'worker2'
229
+ @job.locked_at.should > 1.minute.ago
230
+ end
231
+
232
+ it "should not be found by another worker" do
233
+ Delayed::Job.worker_name = 'worker2'
234
+
235
+ Delayed::Job.find_available(1, 6.minutes).length.should == 0
236
+ end
237
+
238
+ it "should be found by another worker if the time has expired" do
239
+ Delayed::Job.worker_name = 'worker2'
240
+
241
+ Delayed::Job.find_available(1, 4.minutes).length.should == 1
242
+ end
243
+
244
+ it "should be able to get exclusive access again when the worker name is the same" do
245
+ @job.lock_exclusively! 5.minutes, 'worker1'
246
+ @job.lock_exclusively! 5.minutes, 'worker1'
247
+ @job.lock_exclusively! 5.minutes, 'worker1'
248
+ end
249
+ end
250
+
251
+ context "when another worker has worked on a task since the job was found to be available, it" do
252
+
253
+ before :each do
254
+ Delayed::Job.worker_name = 'worker1'
255
+ @job = Delayed::Job.create :payload_object => SimpleJob.new
256
+ @job_copy_for_worker_2 = Delayed::Job.find(@job.id)
257
+ end
258
+
259
+ it "should not allow a second worker to get exclusive access if already successfully processed by worker1" do
260
+ @job.delete
261
+ @job_copy_for_worker_2.lock_exclusively!(4.hours, 'worker2').should == false
262
+ end
263
+
264
+ 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
265
+ @job.update_attributes(:attempts => 1, :run_at => Time.now + 1.day)
266
+ @job_copy_for_worker_2.lock_exclusively!(4.hours, 'worker2').should == false
267
+ end
268
+ end
269
+
270
+ context "#name" do
271
+ it "should be the class name of the job that was enqueued" do
272
+ Delayed::Job.create(:payload_object => ErrorJob.new ).name.should == 'ErrorJob'
273
+ end
274
+
275
+ it "should be the method that will be called if its a performable method object" do
276
+ Delayed::Job.send_later(:clear_locks!)
277
+ Delayed::Job.last.name.should == 'Delayed::Job.clear_locks!'
278
+
279
+ end
280
+ it "should be the instance method that will be called if its a performable method object" do
281
+ story = Story.create :text => "..."
282
+
283
+ story.send_later(:save)
284
+
285
+ Delayed::Job.last.name.should == 'Story#save'
286
+ end
287
+ end
288
+
289
+ context "worker prioritization" do
290
+
291
+ before(:each) do
292
+ Delayed::Job.max_priority = nil
293
+ Delayed::Job.min_priority = nil
294
+ end
295
+
296
+ it "should only work_off jobs that are >= min_priority" do
297
+ Delayed::Job.min_priority = -5
298
+ Delayed::Job.max_priority = 5
299
+ SimpleJob.runs.should == 0
300
+
301
+ Delayed::Job.enqueue SimpleJob.new, -10
302
+ Delayed::Job.enqueue SimpleJob.new, 0
303
+ Delayed::Job.work_off
304
+
305
+ SimpleJob.runs.should == 1
306
+ end
307
+
308
+ it "should only work_off jobs that are <= max_priority" do
309
+ Delayed::Job.min_priority = -5
310
+ Delayed::Job.max_priority = 5
311
+ SimpleJob.runs.should == 0
312
+
313
+ Delayed::Job.enqueue SimpleJob.new, 10
314
+ Delayed::Job.enqueue SimpleJob.new, 0
315
+
316
+ Delayed::Job.work_off
317
+
318
+ SimpleJob.runs.should == 1
319
+ end
320
+
321
+ it "should fetch jobs ordered by priority" do
322
+ number_of_jobs = 10
323
+ number_of_jobs.times { Delayed::Job.enqueue SimpleJob.new, rand(10) }
324
+ jobs = Delayed::Job.find_available(10)
325
+ ordered = true
326
+ jobs[1..-1].each_index{ |i|
327
+ if (jobs[i].priority < jobs[i+1].priority)
328
+ ordered = false
329
+ break
330
+ end
331
+ }
332
+ ordered.should == true
333
+ end
334
+
335
+ end
336
+
337
+ context "when pulling jobs off the queue for processing, it" do
338
+ before(:each) do
339
+ @job = Delayed::Job.create(
340
+ :payload_object => SimpleJob.new,
341
+ :locked_by => 'worker1',
342
+ :locked_at => Delayed::Job.db_time_now - 5.minutes)
343
+ end
344
+
345
+ it "should leave the queue in a consistent state and not run the job if locking fails" do
346
+ SimpleJob.runs.should == 0
347
+ @job.stub!(:lock_exclusively!).with(any_args).once.and_return(false)
348
+ Delayed::Job.should_receive(:find_available).once.and_return([@job])
349
+ Delayed::Job.work_off(1)
350
+ SimpleJob.runs.should == 0
351
+ end
352
+
353
+ end
354
+
355
+ context "while running alongside other workers that locked jobs, it" do
356
+ before(:each) do
357
+ Delayed::Job.worker_name = 'worker1'
358
+ Delayed::Job.create(:payload_object => SimpleJob.new, :locked_by => 'worker1', :locked_at => (Delayed::Job.db_time_now - 1.minutes))
359
+ Delayed::Job.create(:payload_object => SimpleJob.new, :locked_by => 'worker2', :locked_at => (Delayed::Job.db_time_now - 1.minutes))
360
+ Delayed::Job.create(:payload_object => SimpleJob.new)
361
+ Delayed::Job.create(:payload_object => SimpleJob.new, :locked_by => 'worker1', :locked_at => (Delayed::Job.db_time_now - 1.minutes))
362
+ end
363
+
364
+ it "should ingore locked jobs from other workers" do
365
+ Delayed::Job.worker_name = 'worker3'
366
+ SimpleJob.runs.should == 0
367
+ Delayed::Job.work_off
368
+ SimpleJob.runs.should == 1 # runs the one open job
369
+ end
370
+
371
+ it "should find our own jobs regardless of locks" do
372
+ Delayed::Job.worker_name = 'worker1'
373
+ SimpleJob.runs.should == 0
374
+ Delayed::Job.work_off
375
+ SimpleJob.runs.should == 3 # runs open job plus worker1 jobs that were already locked
376
+ end
377
+ end
378
+
379
+ context "while running with locked and expired jobs, it" do
380
+ before(:each) do
381
+ Delayed::Job.worker_name = 'worker1'
382
+ exp_time = Delayed::Job.db_time_now - (1.minutes + Delayed::Job::MAX_RUN_TIME)
383
+ Delayed::Job.create(:payload_object => SimpleJob.new, :locked_by => 'worker1', :locked_at => exp_time)
384
+ Delayed::Job.create(:payload_object => SimpleJob.new, :locked_by => 'worker2', :locked_at => (Delayed::Job.db_time_now - 1.minutes))
385
+ Delayed::Job.create(:payload_object => SimpleJob.new)
386
+ Delayed::Job.create(:payload_object => SimpleJob.new, :locked_by => 'worker1', :locked_at => (Delayed::Job.db_time_now - 1.minutes))
387
+ end
388
+
389
+ it "should only find unlocked and expired jobs" do
390
+ Delayed::Job.worker_name = 'worker3'
391
+ SimpleJob.runs.should == 0
392
+ Delayed::Job.work_off
393
+ SimpleJob.runs.should == 2 # runs the one open job and one expired job
394
+ end
395
+
396
+ it "should ignore locks when finding our own jobs" do
397
+ Delayed::Job.worker_name = 'worker1'
398
+ SimpleJob.runs.should == 0
399
+ Delayed::Job.work_off
400
+ SimpleJob.runs.should == 3 # runs open job plus worker1 jobs
401
+ # This is useful in the case of a crash/restart on worker1, but make sure multiple workers on the same host have unique names!
402
+ end
403
+
404
+ end
405
+
406
+ end