Class: Operationable::Runners::Separate

Inherits:
Base
  • Object
show all
Defined in:
lib/operationable/runners/separate.rb

Instance Attribute Summary

Attributes inherited from Base

#callbacks, #params, #record, #result, #user

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#check_status?, #ensure_enqueue, #initialize, #job_async_execute_method, #job_class, #job_sync_execute_method, #persist?, #store_callback

Methods included from Wrappable

#add_part, #callbacks, #inherited_callbacks, #new, #push_to_queue

Constructor Details

This class inherits a constructor from Operationable::Runners::Base

Class Method Details

.call(params) ⇒ Object



38
39
40
41
42
43
# File 'lib/operationable/runners/separate.rb', line 38

def call(params)
  q_options = extract_q_options(params)
  props = extract_props(params)
  
  q_options[:callback_class_name].constantize.new(props, q_options).method(q_options[:callback_method_name]).call
end

.extract_props(params) ⇒ Object



45
46
47
# File 'lib/operationable/runners/separate.rb', line 45

def extract_props(params)
  params['props'].deep_symbolize_keys
end

.extract_q_options(params) ⇒ Object



49
50
51
# File 'lib/operationable/runners/separate.rb', line 49

def extract_q_options(params)
  params['q_options'].deep_symbolize_keys
end

.operation_nameObject



34
35
36
# File 'lib/operationable/runners/separate.rb', line 34

def operation_name
  self.to_s.split('::').last(3).first(2).map(&:underscore).join(':')
end

Instance Method Details

#get_delayed_params(callback_method_name) ⇒ Object



21
22
23
24
# File 'lib/operationable/runners/separate.rb', line 21

def get_delayed_params(callback_method_name)
  delay = delayer&.try("delay_#{callback_method_name}".to_sym)
  delay.nil? ? {} : delay
end

#process(callback_method_name:, job_class_name: nil, queue: nil, params:) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/operationable/runners/separate.rb', line 9

def process(callback_method_name:, job_class_name: nil, queue: nil, params:)
  # callback can be class
  # callback_class = callback_method_name.to_s.safe_constantize

  args = {
    q_options: q_options(callback_method_name, queue).to_h,
    props: props.merge(params).to_h
  }.to_h.deep_stringify_keys
  
  queue.blank? ? self.class.call(args) : perform(job_class_name, args, get_delayed_params(callback_method_name))
end

#q_options(callback_method_name, queue) ⇒ Object



26
27
28
29
30
31
# File 'lib/operationable/runners/separate.rb', line 26

def q_options(callback_method_name, queue)
  store_callback({ type: 'separate',
    callback_class_name: callback_class_name,
    callback_method_name: callback_method_name,
    queue: queue })
end

#runObject



5
6
7
# File 'lib/operationable/runners/separate.rb', line 5

def run
  check_callbacks.each { |callback| process(callback) }
end