Method: Task#execute

Defined in:
lib/yodel/task_queue/task.rb

#executeObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/yodel/task_queue/task.rb', line 23

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