Class: SidekiqAsyncTask::TransactionSupport

Inherits:
Object
  • Object
show all
Extended by:
AsyncJobScheduler
Includes:
ActiveSupport::Callbacks
Defined in:
lib/sidekiq_async_task/transaction_support.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AsyncJobScheduler

perform_with_transaction_async, perform_with_transaction_at, perform_with_transaction_future, perform_with_transaction_in

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



15
16
17
# File 'lib/sidekiq_async_task/transaction_support.rb', line 15

def arguments
  @arguments
end

#task_idObject

Returns the value of attribute task_id.



13
14
15
# File 'lib/sidekiq_async_task/transaction_support.rb', line 13

def task_id
  @task_id
end

#task_rescheduledObject

Returns the value of attribute task_rescheduled.



14
15
16
# File 'lib/sidekiq_async_task/transaction_support.rb', line 14

def task_rescheduled
  @task_rescheduled
end

Instance Method Details

#after_performObject



42
43
44
45
46
47
48
# File 'lib/sidekiq_async_task/transaction_support.rb', line 42

def after_perform
  return if task_rescheduled == true
  if self.task_id.present?
    task = AsyncTask.find_by_id(self.task_id)
    task.update_attributes!(state: :completed, completed_at: Time.now)
  end
end

#async_reschedule_after(perform_after, *args) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/sidekiq_async_task/transaction_support.rb', line 50

def async_reschedule_after(perform_after, *args)
  self.task_rescheduled = true
  if self.task_id.present?
    task = AsyncTask.find_by_id(self.task_id)
    self.class.perform_in(perform_after, *args, { async_external_hash: "#{task.external_hash}__#{task.id}" })
  else
    self.class.perform_in(perform_after, *args)
  end
end

#before_performObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sidekiq_async_task/transaction_support.rb', line 28

def before_perform
  if self.task_id.present?
    task = AsyncTask.find_by_id(self.task_id)
    if task.uninitiated? || task.scheduled?
      task.update_attributes!(job_id: self.jid, state: :started, started_at: Time.now)
    elsif task.started?
      retry_count = task.retry_count + 1
      task.update_attributes!(retry_count: retry_count)
    else
      raise InternalServerError.new(msg: "AsyncTask #{task.id} should not be started now.")
    end
  end
end

#perform(*args) ⇒ Object



21
22
23
24
25
26
# File 'lib/sidekiq_async_task/transaction_support.rb', line 21

def perform( *args )
  set_args_and_task(*args)
  run_callbacks :perform do
    perform_with_callback( *(self.arguments) )
  end
end