class SidekiqAsyncTask::AsyncTask

Attributes

should_schedule_job[RW]

Public Class Methods

create_future_job!(task_name, payload, perform_after = nil) click to toggle source
# File lib/sidekiq_async_task/async_task.rb, line 23
def self.create_future_job!(task_name, payload, perform_after = nil)
  self.create!(state: :uninitiated, task_name: task_name, perform_after: perform_after, payload: payload, external_hash: get_external_hash)
end
create_job(task_name, payload, perform_after = nil) click to toggle source
# File lib/sidekiq_async_task/async_task.rb, line 16
def self.create_job(task_name, payload, perform_after = nil)
  async_task = self.new(task_name: task_name, perform_after: perform_after, payload: payload, external_hash: get_external_hash)
  async_task.should_schedule_job = true
  async_task.save!
  async_task
end
try_get_task_id_from_hash(task_hash) click to toggle source
# File lib/sidekiq_async_task/async_task.rb, line 45
def self.try_get_task_id_from_hash(task_hash)
  if task_hash.is_a?(Hash) && task_hash.keys == ['async_external_hash']
    task_hash, task_id = task_hash['async_external_hash'].split('__')
    task = AsyncTask.find_by(id: task_id.to_i, external_hash: task_hash)
    raise InternalServerError.new(msg: "Invalid Task Hash Provided") unless task.present?
    task.id
  end
end

Private Class Methods

get_external_hash() click to toggle source
# File lib/sidekiq_async_task/async_task.rb, line 80
def self.get_external_hash
  "async_external_hash_#{SecureRandom.hex}"
end

Public Instance Methods

create_task_hash() click to toggle source
# File lib/sidekiq_async_task/async_task.rb, line 40
def create_task_hash
  "#{self.external_hash}__#{self.id}"
end
get_task_hask() click to toggle source
# File lib/sidekiq_async_task/async_task.rb, line 62
def get_task_hask
  { 'async_external_hash' => self.create_task_hash }
end
schedule_job() click to toggle source
# File lib/sidekiq_async_task/async_task.rb, line 27
def schedule_job
  worker = self.task_name.constantize
  if self.perform_after.present?
    worker.perform_in((self.perform_after).seconds, *(self.payload), { async_external_hash: create_task_hash })
  else
    worker.perform_async(*(self.payload), { async_external_hash: "#{self.external_hash}__#{self.id}" })
  end
rescue StandardError => e
  error_data = [e.message, e.backtrace]
  error_msg = "Error occurred while scheduling job in the AsyncTask with id #{self.id}. Error: #{error_data}. Please resolve immediately."
  raise InternalServerError.new(msg: error_msg) 
end
scheduled?() click to toggle source
# File lib/sidekiq_async_task/async_task.rb, line 58
def scheduled?
  self.state.to_sym == :scheduled
end
scheduled_in_past?() click to toggle source
# File lib/sidekiq_async_task/async_task.rb, line 66
def scheduled_in_past?
  (self.created_at + (self.perform_after.to_i).seconds + 1.minute) < Time.now
end
started?() click to toggle source
# File lib/sidekiq_async_task/async_task.rb, line 54
def started?
  self.state.to_sym == :started
end
uninitiated?() click to toggle source
# File lib/sidekiq_async_task/async_task.rb, line 74
def uninitiated?
  self.state.to_sym == :uninitiated
end
uninitiated_in_past?() click to toggle source
# File lib/sidekiq_async_task/async_task.rb, line 70
def uninitiated_in_past?
  (self.updated_at + (self.perform_after.to_i).seconds + 1.minute) < Time.now
end