Skip to content

Commit

Permalink
Added Worker#shell_run method.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbalatero committed Jun 10, 2009
1 parent 4a2c5d4 commit a2ecb2e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/queue_stick/worker.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module QueueStick
class Worker
class ShellCommandError < StandardError; end

# TODO(dbalatero): configurable?
MAX_ERRORS = 5

Expand All @@ -22,6 +24,24 @@ def get_message_from_queue
def delete_message_from_queue(message)
raise NotImplementedError, "#delete_message_from_queue needs to be implemented in a subclass."
end

# This method will run a shell command, and return the
# output of the command's run.
#
# It will loudly raise an error if the shell command fails,
# which, if raised in your process() method, will be
# guaranteed to hit your recover() method.
def shell_run(cmd)
result = `#{cmd}`
process_status = $?

if !process_status.success?
# TODO(dbalatero): get the stderr output.
raise ShellCommandError, "Error running shell command: #{cmd}"
end

result
end

def run_loop
# Exit the run loop if we get a shutdown request.
Expand Down
24 changes: 24 additions & 0 deletions spec/queue_stick/worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,30 @@
end
end

describe "shell_run" do
before(:all) do
@worker = QueueStick::Worker.new
end

it "should raise an error if a process fails" do
lambda {
@worker.shell_run("ls -l /fds8aufj92njkdsajfdslafjdsafdsa 2>/dev/null")
}.should raise_error(QueueStick::Worker::ShellCommandError)
end

it "should not raise an error if a process doesn't fail" do
lambda {
@worker.shell_run("ls -l /")
}.should_not raise_error
end

it "should return its output" do
result = @worker.shell_run("ls -l /")
result.should_not be_empty
result.should =~ /bin/ # is this a bad assumption?
end
end

describe "run_loop" do
class RunLoopTestWorker < QueueStick::Worker
end
Expand Down

0 comments on commit a2ecb2e

Please sign in to comment.