Class: RailsExecution::Services::TaskScheduler

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task_id) ⇒ TaskScheduler

Returns a new instance of TaskScheduler.



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

def initialize(task_id)
  @task = Task.find(task_id)
end

Class Method Details

.call(task_id) ⇒ Object



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

def self.call(task_id)
  self.new(task_id).call
end

Instance Method Details

#callObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rails_execution/services/task_scheduler.rb', line 13

def call
  task.with_lock do
    next unless task.is_approved? || task.is_processing? && task.scheduled_at.present?
    execute_service = ::RailsExecution::Services::Execution.new(task)
    if execute_service.call
      task.update(status: :completed) unless task.repeatable?
      task.activities.create(owner: task.owner, message: 'Execute: Task completed successfully')
      ::RailsExecution.configuration.notifier.new(task).after_execute_success(task.owner)
      ::RailsExecution::Services::CreateScheduledJob.new(task).call if task.repeatable?
    else
      task.update(status: :failed)
      ::RailsExecution.configuration.notifier.new(task).after_execute_fail(task.owner)
    end
  end
end