Skip to content
Permalink
Browse files
Workers need a #state.
In order for `resque list` to work, we need to know what state
the workers have. This method was in the CLI, but didn't end up actually
existing!

Other changes:

1. So that we can run test/resque/worker_test.rb by itself, some lines
   were added so that worker.rb stands alone.
2. cli_test.rb was fixed up so that it doesn't mock things forever. This
   meant that I couldn't mock things in the other test...
3. Extracted worker construction to a method to make it easier to stub.
   I'm pretty sure this isn't threadsafe, though...
  • Loading branch information
steveklabnik committed Apr 10, 2013
1 parent ecb5343 commit 5963a64e9474eefc074f69149b1f12bcd60995b9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
@@ -1,5 +1,9 @@
require 'time'
require 'redis/distributed'
require 'resque/logging'
require 'resque/core_ext/hash'
require 'resque/worker_registry'
require 'resque/errors'

module Resque
# A Resque Worker processes jobs. On platforms that support fork(2),
@@ -63,11 +67,14 @@ def initialize(queues = [], options = {})
@paused = nil
@cant_fork = false
@reconnected = false
@worker_registry = WorkerRegistry.new(self)

validate_queues
end

def worker_registry
@worker_registry ||= WorkerRegistry.new(self)
end

# This is the main workhorse method. Called on a Worker instance,
# it begins the worker life cycle.
#
@@ -103,9 +110,9 @@ def work(&block)
end
end

@worker_registry.unregister
worker_registry.unregister
rescue Exception => exception
@worker_registry.unregister(exception)
worker_registry.unregister(exception)
end

# DEPRECATED. Processes a single job. If none is given, it will
@@ -114,7 +121,7 @@ def process(job = nil, &block)
return unless job ||= reserve

job.worker = self
@worker_registry.working_on job
worker_registry.working_on job
perform(job, &block)
ensure
done_working
@@ -143,7 +150,7 @@ def shutdown(remote = false)
Resque.logger.info 'Exiting...'

@shutdown = true
@worker_registry.remote_shutdown if remote
worker_registry.remote_shutdown if remote
end

# Kill the child and shutdown immediately.
@@ -154,7 +161,7 @@ def shutdown!

# Should this worker shutdown as soon as current job is finished?
def shutdown?
@shutdown || @worker_registry.remote_shutdown?
@shutdown || worker_registry.remote_shutdown?
end

# Kills the forked child immediately with minimal remorse. The job it
@@ -247,12 +254,16 @@ def failed

# Boolean - true if working, false if not
def working?
@worker_registry.state == :working
worker_registry.state == :working
end

# Boolean - true if idle, false if not
def idle?
@worker_registry.state == :idle
worker_registry.state == :idle
end

def state
worker_registry.state
end

# Is this worker the same as another worker?
@@ -337,7 +348,7 @@ def startup
register_signal_handlers
prune_dead_workers
run_hook :before_first_fork, self
@worker_registry.register
worker_registry.register

# Fix buffering so we can `rake resque:work > resque.log` and
# get output from the child in there.
@@ -443,7 +454,7 @@ def run_hook(name, *args)
# and tells Redis we processed a job.
def done_working
processed!
@worker_registry.done
worker_registry.done
end

# A worker must be given a queue, otherwise it won't know what to
@@ -469,7 +480,7 @@ def wait_for_child
def process_job(job, &block)
Resque.logger.info "got: #{job.inspect}"
job.worker = self
@worker_registry.working_on job
worker_registry.working_on job

@child = fork(job) do
reconnect
@@ -5,14 +5,12 @@
describe Resque::CLI do
describe "#work" do
it "does its thing" do
worker = MiniTest::Mock.new
new_method = MiniTest::Mock.new.expect(:work, "did some work!")
worker.expect(:new, new_method, [["first", "second"], {:timeout => 2, :interval => 666, :daemon => true}])
worker = MiniTest::Mock.new.expect(:work, "did some work!")

Resque::Worker = worker

cli = Resque::CLI.new([], ["-c", "test/fixtures/resque.yml", "-i", "666", "-q", "first,second", "-r", "path/to/file"])
assert_equal "did some work!", cli.invoke(:work)
Resque::Worker.stub(:new, worker) do
cli = Resque::CLI.new([], ["-c", "test/fixtures/resque.yml", "-i", "666", "-q", "first,second", "-r", "path/to/file"])
assert_equal "did some work!", cli.invoke(:work)
end
end
end

@@ -0,0 +1,16 @@
require 'test_helper'

require 'resque/worker'

describe Resque::Worker do
describe "#state" do
it "gives us the current state" do
worker = Resque::Worker.new :queue => "foo"
registry = MiniTest::Mock.new.expect(:state, "working")

worker.stub(:worker_registry, registry) do
assert_equal "working", worker.state
end
end
end
end

0 comments on commit 5963a64

Please sign in to comment.