Class: SidekiqAsyncTask::AsyncTask

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/sidekiq_async_task/async_task.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#should_schedule_jobObject

Returns the value of attribute should_schedule_job.



10
11
12
# File 'lib/sidekiq_async_task/async_task.rb', line 10

def should_schedule_job
  @should_schedule_job
end

Class Method Details

.create_future_job!(task_name, payload, perform_after = nil) ⇒ Object



23
24
25
# 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) ⇒ Object



16
17
18
19
20
21
# 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) ⇒ Object



45
46
47
48
49
50
51
52
# 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

Instance Method Details

#create_task_hashObject



40
41
42
# File 'lib/sidekiq_async_task/async_task.rb', line 40

def create_task_hash
  "#{self.external_hash}__#{self.id}"
end

#get_task_haskObject



62
63
64
# File 'lib/sidekiq_async_task/async_task.rb', line 62

def get_task_hask
  { 'async_external_hash' => self.create_task_hash }
end

#schedule_jobObject



27
28
29
30
31
32
33
34
35
36
37
38
# 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?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/sidekiq_async_task/async_task.rb', line 58

def scheduled?
  self.state.to_sym == :scheduled
end

#scheduled_in_past?Boolean

Returns:

  • (Boolean)


66
67
68
# 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?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/sidekiq_async_task/async_task.rb', line 54

def started?
  self.state.to_sym == :started
end

#uninitiated?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/sidekiq_async_task/async_task.rb', line 74

def uninitiated?
  self.state.to_sym == :uninitiated
end

#uninitiated_in_past?Boolean

Returns:

  • (Boolean)


70
71
72
# 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