Sha256: f052606a7913110a62786ed80a098a473dcd5c192754260cb3f846c77f2fc06c

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 KB

Contents

class Task < SiteRecord
  FAILED_TASK_DELAY = 2 * 60  # 2 minutes
  MAX_TASK_ATTEMPTS = 5
  
  collection :queue
  field :type, :string
  field :params, :hash
  field :due, :time
  field :created_at, :time
  field :locked, :time
  field :repeat_in, :integer
  field :attempts, :integer, default: 0
  field :stack_trace, :string
  
  def self.add_task(type, params, site, due=nil)
    task = Task.new(site)
    task.type = type.to_s
    task.params = params
    task.due = due
    task.save
  end
  
  def execute
    # TODO: plugins need a way of inserting themselves in to this switch
    case self.type
    when 'deliver_email'
      email = site.emails.find(params['_id'])
      email.perform_delivery(params)
    when 'call_api'
      api_call = site.api_calls.find(params['_id'])
      api_call.perform_call(params)
    when 'perform_destroy_stale_carts'
      Cart.perform_destroy_stale_carts(site)
    end
    
    # if the task repeats, place it on the queue again by reseting the
    # due time and removing the locked time making it available again
    if repeat_in?
      self.due = Time.at(Time.now.utc.to_i + repeat_in)
      self.locked = nil
      self.save
    else
      self.destroy
    end
    
  rescue
    # increase the attempt count after any exceptions. after N attempts
    # the task is halted and assumed to have failed. manual intervention
    # is required to start it again. delay its execution on the queue for
    # a certain amount of time to potentially allow resource issues to
    # "fix themselves" (such as loss of network).
    self.attempts += 1
    
    if self.attempts < MAX_TASK_ATTEMPTS
      self.due = Time.at(Time.now.utc.to_i + FAILED_TASK_DELAY)
      self.locked = nil
    end
    
    self.stack_trace = "Error: #{$!}\n#{$@.join("\n")}"
    self.save
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
yodel-0.0.7 lib/yodel/task_queue/task.rb
yodel-0.0.4 lib/yodel/task_queue/task.rb
yodel-0.0.3 lib/yodel/task_queue/task.rb
yodel-0.0.2 lib/yodel/task_queue/task.rb
yodel-0.0.1 lib/yodel/task_queue/task.rb