Class: Task

Inherits:
SiteRecord show all
Defined in:
lib/yodel/task_queue/task.rb

Constant Summary collapse

FAILED_TASK_DELAY =

2 minutes

2 * 60
MAX_TASK_ATTEMPTS =
5

Constants inherited from AbstractRecord

AbstractRecord::CALLBACKS, AbstractRecord::FIELD_CALLBACKS, AbstractRecord::ORDERS

Instance Attribute Summary

Attributes inherited from SiteRecord

#site

Attributes inherited from AbstractRecord

#changed, #errors, #stash, #typecast, #values

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SiteRecord

#default_values, #initialize, #inspect_hash, #perform_reload, #prepare_reload_params, #site_id

Methods included from SiteModel

#load, #scoped, #scoped_for

Methods included from MongoModel

#collection, #load, #scoped

Methods included from AbstractModel

#embed_many, #embed_one, #field, #fields, #many, #modify_field, #one, #remove_field

Methods inherited from MongoRecord

#collection, #default_values, #fields, #id, #increment!, #inspect_hash, #load_from_mongo, #load_mongo_document, #perform_destroy, #perform_reload, #perform_save, #set_id

Methods inherited from AbstractRecord

#changed!, #changed?, #clear_key, #default_values, #destroy, #destroyed?, #eql?, #errors?, #field, #field?, #field_was, #fields, #from_json, #get, #get_meta, #get_raw, #hash, #id, #increment!, inherited, #initialize, #inspect, #inspect_hash, #inspect_value, #method_missing, #new?, #prepare_reload_params, #present?, #reload, #save, #save_without_validation, #search_terms, #set, #set_meta, #set_raw, #to_json, #to_str, #trigger_field_callback, #update, #valid?

Constructor Details

This class inherits a constructor from SiteRecord

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class AbstractRecord

Class Method Details

.add_task(type, params, site, due = nil) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/yodel/task_queue/task.rb', line 15

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

Instance Method Details

#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