Class: RailsWorkflow::OperationRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_workflow/operation_runner.rb

Overview

Workflow::OperationRunner responsible for operation execution

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operation) ⇒ OperationRunner

Returns a new instance of OperationRunner.



17
18
19
# File 'lib/rails_workflow/operation_runner.rb', line 17

def initialize(operation)
  @operation = operation
end

Instance Attribute Details

#operationObject

Returns the value of attribute operation.



6
7
8
# File 'lib/rails_workflow/operation_runner.rb', line 6

def operation
  @operation
end

Class Method Details

.start(operations) ⇒ Object



11
12
13
14
15
# File 'lib/rails_workflow/operation_runner.rb', line 11

def self.start(operations)
  operations.each do |operation|
    new(operation).start
  end
end

Instance Method Details

#cancelObject



73
74
75
# File 'lib/rails_workflow/operation_runner.rb', line 73

def cancel
  complete Status::CANCELED
end

#complete(to_status = Status::DONE) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# 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_transactionObject



52
53
54
55
56
57
58
59
60
# 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

#skipObject



77
78
79
# File 'lib/rails_workflow/operation_runner.rb', line 77

def skip
  complete Status::SKIPPED
end

#startObject



21
22
23
24
25
26
27
28
# 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

#startingObject



30
31
32
33
34
35
36
37
38
# 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

#waitingObject



40
41
42
43
44
45
46
47
48
49
50
# 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