Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Changing existing Worker class to work with the new Master

  • Loading branch information...
commit b0899401e5b99c6081186081c4b29263267e89b0 1 parent 48016f5
@andrewtimberlake authored
View
27 lib/cascade/worker.rb
@@ -1,8 +1,13 @@
module Cascade
class Worker
- def self.start
- trap('TERM') { $exit = true }
- trap('INT') { $exit = true }
+ def initialize(number)
+ @number = number
+ end
+ attr_reader :number
+
+ def start
+ proc_name = $0
+ $0 = [proc_name.sub(/master/, '').strip, "worker #{number}"].join(' ')
loop do
result = run
@@ -18,7 +23,7 @@ def self.start
end
end
- def self.run
+ def run
success = 0
failure = 0
@@ -36,7 +41,7 @@ def self.run
[success, failure]
end
- def self.run_forked(job_spec)
+ def run_forked(job_spec)
read, write = IO.pipe
pid = fork do
@@ -70,7 +75,7 @@ def self.run_forked(job_spec)
return result == '1'
end
- def self.run_job(job_spec, job)
+ def run_job(job_spec, job)
job_spec.re_run = false
completed_successully = true
begin
@@ -122,15 +127,15 @@ def self.enqueue(job_class, *args)
job_spec
end
- def self.name=(name)
+ def name=(name)
@name = name
end
- def self.name
+ def name
@name ||= generate_name
end
- def self.generate_name
+ def generate_name
"#{Socket.gethostname} pid:#{Process.pid}" rescue "pid:#{Process.pid}"
end
@@ -158,7 +163,7 @@ def self.queue
end
private
- def self.find_available(num = 10)
+ def find_available(num = 10)
right_now = Time.now.utc
conditions = {
@@ -171,7 +176,7 @@ def self.find_available(num = 10)
job_specs
end
- def self.lock_exclusively!(job_spec)
+ def lock_exclusively!(job_spec)
right_now = Time.now.utc
conditions = {
View
14 spec/lib/cascade/job/callbacks_spec.rb
@@ -56,14 +56,14 @@ module Cascade
it "runs after_fork after forking the job" do
job_spec = TestJob.enqueue(:raise_error)
- Worker.run_forked job_spec
+ Worker.new(1).run_forked job_spec
job_spec.reload['after_fork'].should be_true
end
it "should run before_queue, before_run, on_success and after_run callbacks on a successful job run" do
job_spec = TestJob.enqueue
job = job_spec.job
- Worker.run_job(job_spec, job)
+ Worker.new(1).run_job(job_spec, job)
job.history.should == [:before_queue, :before_run, :on_success, :after_run]
end
@@ -71,7 +71,7 @@ module Cascade
it "should run before_queue, before_run, on_error and after_run callbacks on a successful job run" do
job_spec = TestJob.enqueue(:raise_error)
job = job_spec.job
- Worker.run_job(job_spec, job)
+ Worker.new(1).run_job(job_spec, job)
job.history.should == [:before_queue, :before_run, :on_error, :after_run]
end
@@ -80,7 +80,7 @@ module Cascade
it "should run the parent callbacks on a successful job run" do
job_spec = SubClassJob.enqueue
job = job_spec.job
- Worker.run_job(job_spec, job)
+ Worker.new(1).run_job(job_spec, job)
job.history.should == [:before_queue, :before_run, :on_success, :after_run]
end
@@ -88,7 +88,7 @@ module Cascade
it "should run it's own callbacks along with the parent callbacks on a successful job run" do
job_spec = SecondSubClassJob.enqueue
job = job_spec.job
- Worker.run_job(job_spec, job)
+ Worker.new(1).run_job(job_spec, job)
job.history.should == [:before_queue, :before_run, 'SecondSubClassJob#before_run', :on_success, :after_run]
end
@@ -98,7 +98,7 @@ module Cascade
it "runs the parent's parent callbacks" do
job_spec = SubSubClassJob.enqueue
job = job_spec.job
- Worker.run_job(job_spec, job)
+ Worker.new(1).run_job(job_spec, job)
job.history.should == [:before_queue, :before_run, :on_success, :after_run]
end
@@ -107,7 +107,7 @@ module Cascade
it "can cancel itself in the before_run callback by returning false" do
job_spec = CancellingJob.enqueue
job = job_spec.job
- Worker.run_job(job_spec, job)
+ Worker.new(1).run_job(job_spec, job)
job.history.should == [:before_queue, :before_run, :after_run]
end
View
10 spec/lib/cascade/worker_spec.rb
@@ -59,7 +59,7 @@ module Cascade
MyJob.enqueue
ErrorJob.enqueue
- result = Worker.run
+ result = Worker.new(1).run
@success = result[0]
@failure = result[1]
@@ -83,7 +83,7 @@ module Cascade
before do
job_spec
- Worker.run
+ Worker.new(1).run
end
it "should set the last error if a job fails" do
@@ -113,7 +113,7 @@ module Cascade
before do
job_spec
- Worker.run
+ Worker.new(1).run
end
it "should set the last error if a job fails" do
@@ -143,7 +143,7 @@ module Cascade
before do
job_spec
- Worker.run
+ Worker.new(1).run
end
it "should the last error if a job fails" do
@@ -173,7 +173,7 @@ module Cascade
before do
job_spec
- Worker.run
+ Worker.new(1).run
end
it "should remove the locks" do
Please sign in to comment.
Something went wrong with that request. Please try again.