From 580a34add9c6a24258409010a1272f4e931f8bcd Mon Sep 17 00:00:00 2001 From: Fernando Sainz <167863+fsainz@users.noreply.github.com> Date: Thu, 13 Jul 2023 09:54:39 +0200 Subject: [PATCH] 1.1.0 release (#30) Co-authored-by: fsainz --- CHANGELOG.md | 15 +++++++++++++++ README.md | 29 +++++++++++++++++++++++++++++ lib/worker_tools/basics.rb | 3 ++- lib/worker_tools/version.rb | 2 +- test/worker_tools/basics_test.rb | 9 +++++++++ 5 files changed, 56 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 555db08..39247f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,20 @@ # Changelog +## [1.1.0] - 2023-07-13 + +### New + +- Custom run_modes +- XlsxInput without headers +- Custom reset hook +- Increment counters by a given amount + +### BREAKING CHANGES + +`WorkerTools::Errors::Invalid` has being replaced with `WorkerTools::Errors::Silent` + +The method `non_failure_error?` has being replaced with `silent_error?` + ## [1.0.0] - 2022-05-20 Compared to 0.2.1 diff --git a/README.md b/README.md index 8cff1e2..77b0717 100644 --- a/README.md +++ b/README.md @@ -269,6 +269,35 @@ def perform(model_id) end ``` +## Run Modes + +It is possible to run the same task in different modes by providing the key `run_mode` inside `options`. For example, by setting it to `:destroy`, the method `run_in_destroy_mode` will get called instead of the usual `run` + +```ruby + # options[:run_mode] = :destroy + + def run_in_destroy_mode + # add_some note + # delete plenty of stuff, take your time + end +``` + +As a convention, use `options[:run_mode_option]` to provide more context: + +```ruby + # options[:run_mode] = :destroy + # options[:run_mode_option] = :all / :only_foos / :only_bars + def run_in_destroy_mode + case run_mode_option + when :all then kaboom + when :only_foos then delete_foos + when :only_bars then delete_bars + end + end +``` + +If the corresponding run method is not available an exeception will be raised. A special case is the run_mode `:repeat` which will try to use the method `:run_in_repeat_mode` and fallback to `run` if not present. + ## Counter There is a counter wrapper that you can use to add custom counters to the meta attribute. To do this, you need to complete the following tasks: diff --git a/lib/worker_tools/basics.rb b/lib/worker_tools/basics.rb index 31befed..1102a21 100644 --- a/lib/worker_tools/basics.rb +++ b/lib/worker_tools/basics.rb @@ -93,7 +93,7 @@ def silent_error?(error) private def run_mode - model.try(:options).try(:[], 'run_mode') + model.try(:options).try(:[], 'run_mode').try(:to_sym) end def run_mode_option @@ -105,6 +105,7 @@ def run_method method_name = "run_in_#{run_mode}_mode" return method_name.to_sym if respond_to?(method_name, true) + return :run if run_mode == :repeat # common fallback raise "Missing method #{method_name}" end diff --git a/lib/worker_tools/version.rb b/lib/worker_tools/version.rb index a2944f6..8c26a1b 100644 --- a/lib/worker_tools/version.rb +++ b/lib/worker_tools/version.rb @@ -1,3 +1,3 @@ module WorkerTools - VERSION = '1.0.2'.freeze + VERSION = '1.1.0'.freeze end diff --git a/test/worker_tools/basics_test.rb b/test/worker_tools/basics_test.rb index a42dc41..45d2e2c 100644 --- a/test/worker_tools/basics_test.rb +++ b/test/worker_tools/basics_test.rb @@ -175,6 +175,15 @@ def run err = assert_raises(StandardError) { importer.perform } assert_includes err.message, 'run_in_foo_mode' end + + it 'uses run if run_mode for repeat is not present' do + import = create_import + import.update!(options: { run_mode: 'repeat' }) + importer = Importer.new + importer.model = import + importer.expects(:run) + importer.perform + end end end