Skip to content

Commit

Permalink
adds run modes (#29)
Browse files Browse the repository at this point in the history
Co-authored-by: fsainz <[email protected]>
  • Loading branch information
fsainz and fsainz authored Jun 9, 2023
1 parent 478d844 commit 58951a8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/worker_tools/basics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def perform(model_id = nil)

default_reset
with_wrappers(wrapper_methods) do
run
send(run_method)
end
end

Expand Down Expand Up @@ -92,6 +92,23 @@ def silent_error?(error)

private

def run_mode
model.try(:options).try(:[], 'run_mode')
end

def run_mode_option
model.try(:options).try(:[], 'run_mode_option').try(:to_sym)
end

def run_method
return :run unless run_mode.present?

method_name = "run_in_#{run_mode}_mode"
return method_name.to_sym if respond_to?(method_name, true)

raise "Missing method #{method_name}"
end

def default_reset
model.attributes = { notes: [], meta: {}, state: 'running' }
model.save!(validate: false)
Expand Down
1 change: 1 addition & 0 deletions test/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
t.string 'state', default: 'waiting', null: false
t.json 'notes', default: []
t.json 'meta', default: {}
t.json 'options', default: {}
t.timestamps
end
end
23 changes: 23 additions & 0 deletions test/worker_tools/basics_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,29 @@ def run
importer.expects(:custom_reset)
importer.perform
end

describe 'run modes' do
it 'calls run with the correct mode if defined in options' do
import = create_import
import.update!(options: { run_mode: 'foo', run_mode_option: 'bar' })
importer = Importer.new
importer.model = import
importer.expects(:run_in_foo_mode)
importer.perform

assert_equal(importer.send(:run_mode_option), :bar)
end

it 'raises if the run mode is not defined' do
import = create_import
import.update!(options: { run_mode: 'foo' })
importer = Importer.new
importer.model = import

err = assert_raises(StandardError) { importer.perform }
assert_includes err.message, 'run_in_foo_mode'
end
end
end

describe '#finalize' do
Expand Down

0 comments on commit 58951a8

Please sign in to comment.