Module: WorkerTools::Basics

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

Instance Method Summary collapse

Instance Method Details

#create_model_if_not_availableObject



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

def create_model_if_not_available
  false
end

#finalizeObject



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

def finalize
  model.update!(
    state: 'complete',
    information: information
  )
end

#modelObject



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

def model
  @model ||= find_model
end

#model_classObject



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

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

#model_kindObject



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

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

#perform(model_id = nil) ⇒ Object



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

def perform(model_id = nil)
  @model_id = model_id

  with_wrappers(wrapper_methods) do
    run
  end
end

#runObject



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

def run
  raise "run has to be defined in #{self}"
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)
  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
  # rubocop:enable Lint/RescueException
  model.state = 'failed'
  model.save!(validate: false)
  raise
end

#with_wrappers(wrapper_symbols, &block) ⇒ Object



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

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