Skip to content

Commit

Permalink
Merge pull request #4 from minuscorp/ensure-reports-status
Browse files Browse the repository at this point in the history
Adds task success report on ensure_block
  • Loading branch information
taquitos authored Feb 26, 2018
2 parents 3743bd3 + 67d8108 commit ffa7145
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
13 changes: 12 additions & 1 deletion queue_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,22 @@ def finish_task
@busy = false
@current_task.completed = true
@current_task.completed.freeze # Sorry, you can't run this task again
@current_task.finished_successfully = true
@current_task.finished_successfully.freeze
end

begin
# if we have an ensure block to run, run it now
@current_task.ensure_block.call if @current_task.ensure_block
unless @current_task.ensure_block.nil?
case @current_task.ensure_block.arity
when 0
@current_task.ensure_block.call
when 1
@current_task.ensure_block.call(@current_task.finished_successfully)
else
raise "Unexpected number of arguments in `ensure_block`, expected 0 or 1, got #{@current_task.ensure_block.arity}"
end
end
rescue StandardError => e
# Oh noes, our ensure block raised something
puts("finish_task failed with exception: #{e.message}")
Expand Down
27 changes: 27 additions & 0 deletions specs/work_queue_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,33 @@ def wait_for_task_to_complete(task: nil)
expect(ensured).to be(true)
end

it 'Reports success state when task completed without exceptions' do
queue = TaskQueue.new(name: 'test queue')
work_completed = false
success_task = false
task = Task.new(work_block: proc { work_completed = true }, ensure_block: proc { |success| success_task = true })
queue.add_task_async(task: task)
wait_for_task_to_complete(task: task)
expect(work_completed).to be(true)
expect(success_task).to be(true)
expect(task.finished_successfully).to be(true)
end

it 'Reports unsuccess state when task completed with exceptions' do
ensured = false
success_task = nil
expect {
queue = TaskQueue.new(name: 'test queue')
task = Task.new(work_block: proc { raise "Oh noes" }, ensure_block: proc { |success| ensured = true; success_task = success })
queue.add_task_async(task: task)
wait_for_task_to_complete(task: task)
}.to raise_error(RuntimeError, "Oh noes")

expect(ensured).to be(true)
expect(success_task).to be(false)
expect(task.finished_successfully).to be(false)
end

it 'Executes 2 blocks of work with just 1 worker' do
queue = TaskQueue.new(name: 'test queue')

Expand Down
2 changes: 2 additions & 0 deletions task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Task
attr_accessor :ensure_block
attr_accessor :completed
attr_accessor :submitted
attr_accessor :finished_successfully

def initialize(name: nil, description: nil, work_block: nil, ensure_block: nil)
self.work_block = work_block
Expand All @@ -19,6 +20,7 @@ def initialize(name: nil, description: nil, work_block: nil, ensure_block: nil)
self.description.freeze
self.completed = false
self.submitted = false
self.finished_successfully = false
end
end
end

0 comments on commit ffa7145

Please sign in to comment.