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



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

def complete_with_warnings_note_levels
  %w[error warning]
end

#finalizeObject



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

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



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

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

#perform(model_id = nil) ⇒ Object



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

def perform(model_id = nil)
  @model_id = model_id

  default_reset
  with_wrappers(wrapper_methods) do
    send(run_method)
  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

#silent_error?(error) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#with_wrapper_basics(&block) ⇒ Object



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

def with_wrapper_basics(&block)
  custom_reset if respond_to?(:custom_reset, true)
  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
  # rubocop:enable Lint/RescueException
  save_state_without_validate('failed')
  raise unless silent_error?(e)
end

#with_wrappers(wrapper_symbols, &block) ⇒ Object



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

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



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

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