Class: Taskr::Models::Task

Inherits:
Base
  • Object
show all
Defined in:
lib/taskr/models.rb

Instance Method Summary collapse

Instance Method Details

#next_trigger_timeObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/taskr/models.rb', line 110

def next_trigger_time
  # TODO: need to figure out how to calulate trigger_time for these.. for now return :unknown
  return :unknown unless schedule_method == 'at' || schedule_method == 'in'
      
  if schedule_method == 'in'
    return (created_on || Time.now) + Taskr.scheduler.duration_to_f(schedule_when)
  end
  
  # Time parsing code from Rails
  time_hash = Date._parse(schedule_when)
  time_hash[:sec_fraction] = ((time_hash[:sec_fraction].to_f % 1) * 1_000_000).to_i
  time_array = time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction)
  # treat 0000-00-00 00:00:00 as nil
  Time.send(Base.default_timezone, *time_array) rescue DateTime.new(*time_array[0..5]) rescue nil
end

#schedule!(scheduler) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/taskr/models.rb', line 42

def schedule!(scheduler)
  case schedule_method
  when 'cron'
    method = :schedule
  when 'at'
    method = :schedule_at
  when 'in'
    method = :schedule_in
  when 'every'
    method = :schedule_every
  end
  
  if method == :schedule_at || method == :schedule_in
    t = next_trigger_time
    method = :schedule_at
    if t < Time.now
      $LOG.warn "Task #{name.inspect} will not be scheduled because its trigger time is in the past (#{t.inspect})."
      return nil
    end
  end
  
  $LOG.debug "Scheduling task #{name.inspect}: #{self.inspect}"
  
  if task_actions.length == 1
    ta = task_actions.first
    
    parameters = {}
    ta.action_parameters.each{|p| parameters[p.name] = p.value}
    
    action = (ta.action_class.kind_of?(Class) ? ta.action_class : ta.action_class.constantize).new(parameters)
    action.task = self
  elsif task_actions.length > 1
    action = Taskr::Actions::Multi.new
    task_actions.each do |ta|
      parameters = {}
      ta.action_parameters.each{|p| parameters[p.name] = p.value}
      
      a = (ta.action_class.kind_of?(Class) ? ta.action_class : ta.action_class.constantize).new(parameters)
      a.task = self
      
      action.actions << a 
    end
    action.task = self
  else
    $LOG.warn "Task #{name.inspect} has no actions and as a result will not be scheduled!"
    return false
  end
  
  job_id = scheduler.send(method, t || schedule_when, :schedulable => action)
  
  if job_id
    $LOG.debug "Task #{name.inspect} scheduled with job id #{job_id}"
  else
    $LOG.error "Task #{name.inspect} was NOT scheduled!"
    return nil
  end
  
  self.update_attribute(:scheduler_job_id, job_id)
  if method == :schedule_at || method == :schedule_in
    job = scheduler.get_job(job_id)
    at = job.schedule_info
    self.update_attribute(:schedule_when, at)
    self.update_attribute(:schedule_method, 'at')
  end
  
  return job_id
end

#to_sObject



126
127
128
# File 'lib/taskr/models.rb', line 126

def to_s
  "#<#{self.class}:#{self.id}>"
end