Skip to content

Commit

Permalink
1.1.0 release (#30)
Browse files Browse the repository at this point in the history
Co-authored-by: fsainz <[email protected]>
  • Loading branch information
fsainz and fsainz authored Jul 13, 2023
1 parent 58951a8 commit 580a34a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion lib/worker_tools/basics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/worker_tools/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module WorkerTools
VERSION = '1.0.2'.freeze
VERSION = '1.1.0'.freeze
end
9 changes: 9 additions & 0 deletions test/worker_tools/basics_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 580a34a

Please sign in to comment.