class BackOps::Worker

Public Class Methods

perform_async(globals, actions) click to toggle source

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

Calls superclass method
# 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) click to toggle source
# 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) click to toggle source
Calls superclass method
# 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) click to toggle source
# 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

Public Instance Methods

perform(operation_id) click to toggle source

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

# File lib/back_ops/worker.rb, line 73
def perform(operation_id)
  operation = BackOps::Operation.find(operation_id)
  process(operation)
end

Private Instance Methods

process(operation) click to toggle source
# File lib/back_ops/worker.rb, line 80
def process(operation)
  action = operation.next_action
  return true if action.blank?
  return process_next(operation, at: action.perform_at.to_f) if action.premature?

  begin
    action.name.constantize.call(action)
    action.mark_completed

    operation.next_action = BackOps::Action.after(action)
    operation.save!

    if operation.next_action.present?
      process_next(operation)
    else
      operation.completed_at = action.completed_at
      operation.save!
    end

  rescue => e
    action.mark_errored(e)
    raise
  end
end
process_next(operation, options = {}) click to toggle source
# File lib/back_ops/worker.rb, line 105
def process_next(operation, options = {})
  options.deep_stringify_keys!

  Sidekiq::Client.push({
    'class' => self.class.name,
    'args' => [operation.id]
  }.merge(options))

  true
end