Class: BackOps::Worker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker
Defined in:
lib/back_ops/worker.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.perform_async(globals, actions) ⇒ Object

Class Methods ========================================================



16
17
18
19
# File 'lib/back_ops/worker.rb', line 16

def self.perform_async(globals, actions)
  operation = setup_operation_and_actions(globals, actions)
  super(operation.id)
end

.perform_at(interval, globals, action) ⇒ Object



26
27
28
# File 'lib/back_ops/worker.rb', line 26

def self.perform_at(interval, globals, action)
  perform_in(interval, globals, action)
end

.perform_in(interval, globals, actions) ⇒ Object



21
22
23
24
# File 'lib/back_ops/worker.rb', line 21

def self.perform_in(interval, globals, actions)
  operation = setup_operation_and_actions(globals, actions)
  super(interval, operation.id)
end

.setup_operation_and_actions(globals, branches) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/back_ops/worker.rb', line 30

def self.setup_operation_and_actions(globals, branches)
  raise ArgumentError, 'Cannot process empty actions' if branches.blank?

  globals ||= {}
  globals.deep_stringify_keys!

  operation = BackOps::Operation.create_or_find_by({
    params_hash: Digest::MD5.hexdigest("#{globals}|#{branches}"),
    name: ancestors[1]
  })
  operation.globals.merge!(globals)
  operation.save!

  counter = 0

  branches.each do |branch, actions|
    actions.each do |action_with_options|
      action_name, options = [*action_with_options]

      options = {
        'perform_at' => nil
      }.merge(options.try(:deep_stringify_keys) || {})

      action = BackOps::Action.create_or_find_by({
        operation: operation,
        branch: branch,
        name: action_name,
        perform_at: options['perform_at'],
        order: counter
      })

      counter += 1
    end
  end

  operation.next_action = operation.first_action
  operation.save!

  operation
end

Instance Method Details

#perform(operation_id) ⇒ Object

Instance Methods =====================================================



73
74
75
76
# File 'lib/back_ops/worker.rb', line 73

def perform(operation_id)
  operation = BackOps::Operation.find(operation_id)
  process(operation)
end