Class: RailsExecution::Services::BackgroundExecution

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_execution/services/background_execution.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task, owner = nil) ⇒ BackgroundExecution

Returns a new instance of BackgroundExecution.



9
10
11
12
# File 'lib/rails_execution/services/background_execution.rb', line 9

def initialize(task, owner = nil)
  @task = task
  @owner = owner
end

Class Method Details

.call(task_id) ⇒ Object



5
6
7
# File 'lib/rails_execution/services/background_execution.rb', line 5

def self.call(task_id)
  self.new(::RailsExecution::Task.find(task_id)).call
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rails_execution/services/background_execution.rb', line 14

def call
  task.with_lock do
    execute_service = ::RailsExecution::Services::Execution.new(task)
    if execute_service.call
      task.activities.create(message: 'Execute: The task is processed successfully by background job')

      if task.repeatable?
        task.update(status: :approved)
        task.activities.create(owner: owner, message: 'Set status to approved because the task is repeatable')
      else
        task.update(status: :completed)
        task.activities.create(owner: owner, message: 'Set status to completed because the task is not repeatable')
      end

      ::RailsExecution.configuration.notifier.new(task).after_execute_success(owner)
    else
      task.activities.create(message: 'Execute: The task failed to be processed by the background job')

      task.update(status: :approved)
      task.activities.create(owner: owner, message: 'Set task status to approved because the task is failed')
      ::RailsExecution.configuration.notifier.new(task).after_execute_fail(owner)
    end
  end
end

#setupObject



39
40
41
42
43
44
45
46
47
# File 'lib/rails_execution/services/background_execution.rb', line 39

def setup
  task.with_lock do
    unless task.is_processing?
      task.activities.create(owner: owner, message: 'Process the task with background job')
      jid = ::RailsExecution.configuration.task_background_executor.call(task.id)
      task.update!(status: :processing, jid: jid.presence)
    end
  end
end