Module: RailsWorkflow::Operations::DefaultRunner

Extended by:
ActiveSupport::Concern
Included in:
RailsWorkflow::Operation
Defined in:
app/concerns/rails_workflow/operations/default_runner.rb

Overview

Workflow::Operations::DefaultRunner contains operation starting, completing etc logic.

Instance Method Summary (collapse)

Instance Method Details

- (boolean) can_complete?

Checking if can be completed. By default operation can't be completed if it has not yet completed child process

Returns:

  • (boolean)


100
101
102
103
104
# File 'app/concerns/rails_workflow/operations/default_runner.rb', line 100

def can_complete?
  child_process.present? ?
      child_process.status == RailsWorkflow::Process::DONE :
      true
end

- (Boolean) can_start?

Checks if operation can start. By default just checks if operation status is NOT_STARTED (ready to start).

Returns:

  • (Boolean)


35
36
37
# File 'app/concerns/rails_workflow/operations/default_runner.rb', line 35

def can_start?
  status == Operation::NOT_STARTED
end

- (Object) complete

Completing operation (checks if can complete).



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/concerns/rails_workflow/operations/default_runner.rb', line 108

def complete
  if can_complete?
    on_complete if respond_to? :on_complete
    update_attributes(
        {
            status: self.class::DONE,
            completed_at: Time.zone.now
        })
    manager.operation_complete self
  end
rescue => exception
  RailsWorkflow::Error.create_from(
      exception, {
                   parent: self,
                   target: self,
                   method: :complete

               }
  )
end

- (boolean) completed?

Check if operation is completed. Checking if status is DONE, CANCELED or SKIPPED.

Returns:

  • (boolean)


93
94
95
# File 'app/concerns/rails_workflow/operations/default_runner.rb', line 93

def completed?
  completed_statuses.include? status
end

- (boolean) execute

Main operation method that contains logic that should be executed in operation Should return true if operation was executed successfully or false if not.

Returns:

  • (boolean)


87
88
89
# File 'app/concerns/rails_workflow/operations/default_runner.rb', line 87

def execute
  true
end

- (Object) start

Checking if operation can start. If operation can't start it is switched to WAITING status (for example user operations can't start and they are waiting for user to pickup and complete them)



14
15
16
17
18
# File 'app/concerns/rails_workflow/operations/default_runner.rb', line 14

def start
  can_start? ? starting : waiting
rescue => exception
  RailsWorkflow::Error.create_from exception, parent: self
end

- (Object) starting

Starting operation. Moves operation to IN_PROGRESS. If sidekiq is enabled, operation is added to queue. If sidekiq is disabled then executes operation inline



23
24
25
26
27
28
29
30
31
# File 'app/concerns/rails_workflow/operations/default_runner.rb', line 23

def starting

  update_attribute(:status, self.class::IN_PROGRESS)

  is_background && RailsWorkflow.config.sidekiq_enabled ?
      OperationWorker.perform_async(id) :
      OperationWorker.new.perform(id)

end

- (Object) waiting

Switching operation to WAITING status.



40
41
42
43
44
45
# File 'app/concerns/rails_workflow/operations/default_runner.rb', line 40

def waiting
  update_attribute(:status, self.class::WAITING)
  start_waiting if respond_to? :start_waiting
rescue => exception
  RailsWorkflow::Error.create_from exception, parent: self
end