Module: WorkerTools::Basics

Extended by:
ActiveSupport::Concern
Defined in:
lib/worker_tools/basics.rb

Instance Method Summary collapse

Instance Method Details

#complete_with_warnings_note_levelsObject



73
74
75
# File 'lib/worker_tools/basics.rb', line 73

def complete_with_warnings_note_levels
  %w[error warning]
end

#finalizeObject



65
66
67
68
69
70
71
# File 'lib/worker_tools/basics.rb', line 65

def finalize
  mark_with_warnings = model.notes.any? do |note|
    complete_with_warnings_note_levels.include?(note.with_indifferent_access[:level].to_s)
  end

  model.update!(state: mark_with_warnings ? :complete_with_warnings : :complete)
end

#modelObject



77
78
79
# File 'lib/worker_tools/basics.rb', line 77

def model
  @model ||= find_model
end

#model_classObject



19
20
21
22
# File 'lib/worker_tools/basics.rb', line 19

def model_class
  # Ex: Import
  raise "model_class has to be defined in #{self}"
end

#model_kindObject



24
25
26
27
# File 'lib/worker_tools/basics.rb', line 24

def model_kind
  # Ex: 'sdom'
  raise "model_kind has to be defined in #{self}"
end

#non_failure_error?(error) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
# File 'lib/worker_tools/basics.rb', line 88

def non_failure_error?(error)
  error.is_a?(WorkerTools::Errors::Invalid)
  # or add your list
  # [WorkerTools::Errors::Invalid, SomeOtherError].any? { |k| e.is_a?(k) }
end

#perform(model_id = nil) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/worker_tools/basics.rb', line 33

def perform(model_id = nil)
  @model_id = model_id

  with_wrappers(wrapper_methods) do
    run
  end
end

#runObject



29
30
31
# File 'lib/worker_tools/basics.rb', line 29

def run
  raise "run has to be defined in #{self}"
end

#with_wrapper_basics(&block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/worker_tools/basics.rb', line 50

def with_wrapper_basics(&block)
  save_state_without_validate('running')
  block.yield
  finalize
# this time we do want to catch Exception to attempt to handle some of the
# critical errors.
# rubocop:disable Lint/RescueException
rescue Exception => e
  return finalize if non_failure_error?(e)

  # rubocop:enable Lint/RescueException
  save_state_without_validate('failed')
  raise
end

#with_wrappers(wrapper_symbols, &block) ⇒ Object



81
82
83
84
85
86
# File 'lib/worker_tools/basics.rb', line 81

def with_wrappers(wrapper_symbols, &block)
  return yield if wrapper_symbols.blank?

  current_wrapper_symbol = wrapper_symbols.shift
  send(current_wrapper_symbol) { with_wrappers(wrapper_symbols, &block) }
end

#wrapper_methodsObject



41
42
43
44
45
46
47
48
# File 'lib/worker_tools/basics.rb', line 41

def wrapper_methods
  self.class.read_wrappers.map do |wrapper|
    symbolized_method = "with_wrapper_#{wrapper}".to_sym
    raise "Missing wrapper #{wrapper}" unless respond_to?(symbolized_method)

    symbolized_method
  end
end