Class: Taskr::Models::Task
- Inherits:
-
Base
- Object
- Base
- Taskr::Models::Task
- Defined in:
- lib/taskr/models.rb
Instance Method Summary collapse
- #next_trigger_time ⇒ Object
- #prepare_action ⇒ Object
- #schedule!(scheduler = Taskr.scheduler) ⇒ Object
- #to_s ⇒ Object
Instance Method Details
#next_trigger_time ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/taskr/models.rb', line 131 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 |
#prepare_action ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/taskr/models.rb', line 101 def prepare_action 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 action.task_action = ta 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 a.task_action = ta action.actions << a end action.task = self else raise "Task #{name.inspect} has no actions!" end action end |
#schedule!(scheduler = Taskr.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 |
# File 'lib/taskr/models.rb', line 42 def schedule!(scheduler = Taskr.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 self.new_record? # Need to distinguish between the edit/create cases. "Edit" needs to reload the task_actions or nothing works; "Create" needs NOT to relaod the actions, or the validations kick in and nothing works. FIXME!!!!! if task_actions.length > 0 action = prepare_action else $LOG.warn "Task #{name.inspect} has no actions and as a result will not be scheduled!" return false end else if task_actions(true).length > 0 action = prepare_action else $LOG.warn "Task #{name.inspect} has no actions and as a result will not be scheduled!" return false end 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_s ⇒ Object
148 149 150 |
# File 'lib/taskr/models.rb', line 148 def to_s "#{name.inspect}@#{schedule_when}" end |