Class: Taskr::Controllers::Tasks
- Inherits:
-
REST
- Object
- REST
- Taskr::Controllers::Tasks
- Includes:
- Models
- Defined in:
- lib/taskr/controllers.rb
Instance Method Summary collapse
-
#create ⇒ Object
Create and schedule a new task.
-
#destroy(id) ⇒ Object
Unschedule and delete an existing task.
- #edit(task_id) ⇒ Object
- #get_action_class(class_name) ⇒ Object
-
#list ⇒ Object
List of tasks.
-
#new ⇒ Object
Input for a new task.
- #normalize_actions_params(input_params) ⇒ Object
- #normalize_input(hsh) ⇒ Object
-
#read(id) ⇒ Object
Retrieve details for an existing task.
-
#reload(id) ⇒ Object
Reload a task.
- #run(id) ⇒ Object
- #update(task_id) ⇒ Object
Instance Method Details
#create ⇒ Object
Create and schedule a new task.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/taskr/controllers.rb', line 178 def create $LOG.debug @input.inspect begin # the "0" is for compatibility with PHP's Zend_Rest_Client task_data = @input[:task] || @input["0"] || @input name = task_data[:name] created_by = @env['REMOTE_HOST'] schedule_method = task_data[:schedule_method] schedule_when = task_data[:schedule_when] memo = task_data[:memo] @task = Task.new( :name => name, :created_by => created_by, :schedule_method => schedule_method, :schedule_when => schedule_when, :memo => memo ) # some gymnastics here to provide compatibility for the way various # REST client libraries submit data actions_data = task_data[:actions] || task_data[:action] raise ArgumentError, "Missing action(s) parameter." if actions_data.blank? if actions_data.kind_of?(Array) actions = actions_data elsif actions_data["0"] actions = [] actions_data.each do |i,a| actions << a end else actions = actions_data[:action] || actions_data[:actions] || actions_data end actions = [actions] unless actions.kind_of? Array #puts actions.inspect i = 0 actions.each do |a| #puts a.inspect action_class_name = a[:action_class_name] action_class_name = "Taskr::Actions::#{action_class_name}" unless action_class_name =~ /^Taskr::Actions::/ begin action_class = action_class_name.constantize unless action_class.include? Rufus::Schedulable raise ArgumentError, "#{a[:action_class_name].inspect} cannot be used as an action because it does not include the Rufus::Schedulable module." end rescue NameError raise ArgumentError, "#{a[:action_class_name].inspect} is not defined (i.e. there is no such action class)." end action = TaskAction.new(:order => a[:order] || i, :action_class_name => action_class_name) $LOG.debug "Action should be initialized and ready for creation: #{action.inspect}" action_class.parameters.each do |p| value = a[p] value = nil if value.blank? action.action_parameters << TaskActionParameter.new(:name => p, :value => value) end @task.task_actions << action i += 1 end unless @task.valid? @status = 500 @actions = Taskr::Actions.list return render(:new_task) end @task.schedule! Taskr.scheduler if @task.save! location = "/tasks/#{@task.id}?format=#{@format}" $LOG.debug "#{@task} saved successfuly. Setting Location header to #{location.inspect}." @headers['Location'] = location end return render(:view_task) rescue => e puts e.inspect puts e.backtrace raise e end end |
#destroy(id) ⇒ Object
Unschedule and delete an existing task.
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/taskr/controllers.rb', line 289 def destroy(id) @task = Task.find(id) if @task.scheduler_job_id $LOG.debug "Unscheduling task #{@task}..." Taskr.scheduler.unschedule(@task.scheduler_job_id) end @task.destroy if @task.frozen? @status = 200 if @format == :XML "" else return redirect(R(Tasks, :list)) end else _error("Task #{id} was not destroyed.", 500) end end |
#edit(task_id) ⇒ Object
69 70 71 72 73 |
# File 'lib/taskr/controllers.rb', line 69 def edit(task_id) @task = Task.find(task_id, :include => [:task_actions]) @actions = Taskr::Actions.list render :edit_task end |
#get_action_class(class_name) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/taskr/controllers.rb', line 161 def get_action_class(class_name) action_class_name = "Taskr::Actions::#{class_name}" unless class_name =~ /^Taskr::Actions::/ begin action_class = action_class_name.constantize unless action_class.include? Rufus::Schedulable raise ArgumentError, "#{a[:action_class_name].inspect} cannot be used as an action because it does not include the Rufus::Schedulable module." end rescue NameError raise ArgumentError, "#{a[:action_class_name].inspect} is not defined (i.e. there is no such action class)." end action_class end |
#list ⇒ Object
List of tasks.
49 50 51 52 53 |
# File 'lib/taskr/controllers.rb', line 49 def list @tasks = Task.find(:all, :include => [:task_actions]) render :tasks_list end |
#new ⇒ Object
Input for a new task.
56 57 58 59 60 |
# File 'lib/taskr/controllers.rb', line 56 def new @actions = Taskr::Actions.list render :new_task end |
#normalize_actions_params(input_params) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/taskr/controllers.rb', line 134 def normalize_actions_params(input_params) $LOG.debug "normalize_actions_params Normalizing: #{input_params.inspect}" # some gymnastics here to provide compatibility for the way various # REST client libraries submit data actions_data = input_params[:actions] || input_params[:action] raise ArgumentError, "Missing action(s) parameter." if actions_data.blank? actions = case actions_data when Array $LOG.debug "normalize_actions_params Plain Array. Returning as-is." actions_data when Hash $LOG.debug "normalize_actions_params Some weird Hash. Injecting." actions_data.inject([]) do |acc,(i,a)| $LOG.debug "normalize_actions_params acc: #{acc.inspect} index: #{i.inspect} array: #{a.inspect}." acc << a acc end else $LOG.debug "normalize_actions_params Not a weird hash.\n\tactions_data[:action]: #{actions_data[:action].inspect}\n\tactions_data[:actions]: #{actions_data[:actions].inspect}\n\tactions_data: #{actions_data.inspect}\n\n" actions_data[:action] || actions_data[:actions] || actions_data end actions = [actions] unless actions.kind_of? Array $LOG.debug "normalize_actions_params DONE. Returning: #{actions.inspect}" actions end |
#normalize_input(hsh) ⇒ Object
130 131 132 |
# File 'lib/taskr/controllers.rb', line 130 def normalize_input(hsh) hsh[:task] || hsh["0"] || hsh end |
#read(id) ⇒ Object
Retrieve details for an existing task.
63 64 65 66 67 |
# File 'lib/taskr/controllers.rb', line 63 def read(id) @task = Task.find(id, :include => [:task_actions]) render :view_task end |
#reload(id) ⇒ Object
Reload a task
310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/taskr/controllers.rb', line 310 def reload(id) @task = Task.find(id) $LOG.debug "Re-scheduling task #{@task}..." if @task.scheduler_job_id Taskr.scheduler.unschedule(@task.scheduler_job_id) $LOG.debug "\t...unscheduled task #{@task}..." end @task.schedule! Taskr.scheduler $LOG.debug "\t...scheduled task #{@task}...\n" redirect R(@task) end |
#run(id) ⇒ Object
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/taskr/controllers.rb', line 272 def run(id) @task = Task.find(id, :include => [:task_actions]) action = @task.prepare_action LogEntry.info(@task, "Manually executing task #{@task}.") begin action.trigger rescue # ok to catch exception silently. it should have gotten logged by the action end redirect R(@task) end |
#update(task_id) ⇒ Object
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/taskr/controllers.rb', line 75 def update(task_id) $LOG.debug "Update Input params: #{@input.inspect}" @task = Task.find(task_id, :include => [:task_actions]) params = normalize_input(@input) @task.attributes= { :name => params[:name], :schedule_method => params[:schedule_method], :schedule_when => params[:schedule_when], :memo => params[:memo] } @task.task_actions.each do |action| $LOG.debug("Updating parameters for #{action.inspect}") action_params = params[:action].delete("action_id_#{action.id}") $LOG.debug("Using values #{action_params.inspect}") next unless action_params action_params.each do |param_name, value| $LOG.debug("Looking up \"#{param_name}\". Setting value to \"#{value}\"") action.parameters.find_by_name(param_name).update_attribute(:value, value) end end # Create new actions/action_parameters for the remaining params unless params[:action].empty? params[:action].map do |num, params| $LOG.debug "Looping remaining action_parameters: #{params.inspect} (num: #{num})" action_class = get_action_class(params[:action_class_name]) action = TaskAction.new(:order => params[:order] || (@task.task_actions.maximum(:order)+1) || num, :action_class_name => action_class.to_s) action_class.parameters.each do |p| value = params[p] value = nil if value.blank? action.action_parameters << TaskActionParameter.new(:name => p, :value => value) end @task.task_actions << action end end unless @task.valid? @status = 500 @actions = Taskr::Actions.list else @task.save! @status = 200 end @task.reload # Ensure any updates to the record goes in Taskr.scheduler.unschedule(@task.scheduler_job_id) @task.schedule! Taskr.scheduler $LOG.info "Task \"#{@task.name}\" (ID: #{@task.id}) updated sucessfully." redirect R(@task) end |