From 571dde0ca00d2fae1df7b79b755c6b9cff6715d2 Mon Sep 17 00:00:00 2001 From: Jorge Revuelta Date: Mon, 26 Feb 2018 21:07:09 +0100 Subject: [PATCH 1/3] Adds task success report on ensure_block --- queue_worker.rb | 12 ++++++++++++ specs/work_queue_spec.rb | 25 +++++++++++++++++++++++++ task.rb | 2 ++ 3 files changed, 39 insertions(+) diff --git a/queue_worker.rb b/queue_worker.rb index 0f40455..07ee569 100644 --- a/queue_worker.rb +++ b/queue_worker.rb @@ -32,10 +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 + 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 1 or 2, got #{@current_task.ensure_block.arity}" + end + end @current_task.ensure_block.call if @current_task.ensure_block rescue StandardError => e # Oh noes, our ensure block raised something diff --git a/specs/work_queue_spec.rb b/specs/work_queue_spec.rb index deabc91..2a630bc 100644 --- a/specs/work_queue_spec.rb +++ b/specs/work_queue_spec.rb @@ -38,6 +38,31 @@ 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) + 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) + end + it 'Executes 2 blocks of work with just 1 worker' do queue = TaskQueue.new(name: 'test queue') diff --git a/task.rb b/task.rb index 8863e7c..44df00f 100644 --- a/task.rb +++ b/task.rb @@ -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 @@ -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 From f6868a86e73e62ac46ec1800f0e0a4360f19310e Mon Sep 17 00:00:00 2001 From: Jorge Revuelta Date: Mon, 26 Feb 2018 21:12:18 +0100 Subject: [PATCH 2/3] Typo --- queue_worker.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/queue_worker.rb b/queue_worker.rb index 07ee569..b9a3c76 100644 --- a/queue_worker.rb +++ b/queue_worker.rb @@ -45,7 +45,7 @@ def finish_task when 1 @current_task.ensure_block.call(@current_task.finished_successfully) else - raise "Unexpected number of arguments in `ensure_block`, expected 1 or 2, got #{@current_task.ensure_block.arity}" + raise "Unexpected number of arguments in `ensure_block`, expected 0 or 1, got #{@current_task.ensure_block.arity}" end end @current_task.ensure_block.call if @current_task.ensure_block From 67d81083b8890d638512b7be43acb65c8d221b53 Mon Sep 17 00:00:00 2001 From: Jorge Revuelta Date: Mon, 26 Feb 2018 21:40:22 +0100 Subject: [PATCH 3/3] Fixes and review additions --- queue_worker.rb | 13 ++++++------- specs/work_queue_spec.rb | 2 ++ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/queue_worker.rb b/queue_worker.rb index b9a3c76..83e0a4e 100644 --- a/queue_worker.rb +++ b/queue_worker.rb @@ -40,15 +40,14 @@ def finish_task # if we have an ensure block to run, run it now 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}" + 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 - @current_task.ensure_block.call if @current_task.ensure_block rescue StandardError => e # Oh noes, our ensure block raised something puts("finish_task failed with exception: #{e.message}") diff --git a/specs/work_queue_spec.rb b/specs/work_queue_spec.rb index 2a630bc..27cd07d 100644 --- a/specs/work_queue_spec.rb +++ b/specs/work_queue_spec.rb @@ -47,6 +47,7 @@ def wait_for_task_to_complete(task: nil) 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 @@ -61,6 +62,7 @@ def wait_for_task_to_complete(task: nil) 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