class SidekiqAsyncTask::TransactionSupport

Attributes

arguments[RW]
task_id[RW]
task_rescheduled[RW]

Public Instance Methods

after_perform() click to toggle source
# 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) click to toggle source
# 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_perform() click to toggle source
# 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 ) click to toggle source
# 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

Private Instance Methods

set_args_and_task(*args) click to toggle source
# File lib/sidekiq_async_task/transaction_support.rb, line 62
def set_args_and_task(*args)
  task_hash = args[-1]
  task_id = AsyncTask.try_get_task_id_from_hash(task_hash)
  if task_id.present?
    self.arguments = args[0..-2]
    self.task_id = task_id
  else
    self.arguments = args
  end
end