class RailsWorkflow::OperationRunner

Workflow::OperationRunner responsible for operation execution

Attributes

operation[RW]

Public Class Methods

new(operation) click to toggle source
# File lib/rails_workflow/operation_runner.rb, line 17
def initialize(operation)
  @operation = operation
end
start(operations) click to toggle source
# File lib/rails_workflow/operation_runner.rb, line 11
def self.start(operations)
  operations.each do |operation|
    new(operation).start
  end
end

Public Instance Methods

cancel() click to toggle source
# File lib/rails_workflow/operation_runner.rb, line 73
def cancel
  complete Status::CANCELED
end
complete(to_status = Status::DONE) click to toggle source
# File lib/rails_workflow/operation_runner.rb, line 62
def complete(to_status = Status::DONE)
  return unless completable?

  context&.save
  update_attributes(
    status: to_status,
    completed_at: Time.zone.now
  )
  process_runner.operation_completed(operation)
end
execute_in_transaction() click to toggle source
# File lib/rails_workflow/operation_runner.rb, line 52
def execute_in_transaction
  with_transaction do
    child_process_runner.start if child_process.present?
    operation.execute if operation.respond_to?(:execute)
    complete
  end
rescue => exception
  handle_exception(exception)
end
skip() click to toggle source
# File lib/rails_workflow/operation_runner.rb, line 77
def skip
  complete Status::SKIPPED
end
start() click to toggle source
# File lib/rails_workflow/operation_runner.rb, line 21
def start
  can_start? ? starting : waiting
rescue => exception
  error_builder.handle(
    exception,
    parent: operation, target: :operation_runner, method: :start
  )
end
starting() click to toggle source
# File lib/rails_workflow/operation_runner.rb, line 30
def starting
  update_attribute(:status, Status::IN_PROGRESS)

  if is_background && config.activejob_enabled
    OperationExecutionJob.perform_later(operation.id)
  else
    OperationExecutionJob.perform_now(operation.id)
  end
end
waiting() click to toggle source
# File lib/rails_workflow/operation_runner.rb, line 40
def waiting
  update_attribute(:status, Status::WAITING)
  start_waiting if respond_to? :start_waiting
rescue => exception
  error_builder.handle(
    exception,
    parent: operation,
    target: :operation_runner,
    method: :waiting
  )
end

Private Instance Methods

child_process_runner() click to toggle source
# File lib/rails_workflow/operation_runner.rb, line 101
def child_process_runner
  @child_process_runner ||= config.process_runner.new(child_process)
end
config() click to toggle source
# File lib/rails_workflow/operation_runner.rb, line 109
def config
  RailsWorkflow.config
end
error_builder() click to toggle source
# File lib/rails_workflow/operation_runner.rb, line 83
def error_builder
  config.error_builder
end
handle_exception(exception) click to toggle source
# File lib/rails_workflow/operation_runner.rb, line 87
def handle_exception(exception)
  error_builder.handle(
    exception,
    parent: operation, target: :operation_runner,
    method: :execute_in_transaction
  )
end
process_runner() click to toggle source
# File lib/rails_workflow/operation_runner.rb, line 105
def process_runner
  @process_runner ||= config.process_runner.new(operation.process)
end
with_transaction() { || ... } click to toggle source
# File lib/rails_workflow/operation_runner.rb, line 95
def with_transaction
  operation.class.transaction(requires_new: true) do
    yield
  end
end