Class: Taskr::Controllers::Tasks

Inherits:
REST
  • Object
show all
Includes:
Models
Defined in:
lib/taskr/controllers.rb

Instance Method Summary collapse

Instance Method Details

#createObject

Create and schedule a new task.



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
160
161
162
# File 'lib/taskr/controllers.rb', line 70

def create
  puts @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)
      
      
      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.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/taskr/controllers.rb', line 181

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

#listObject

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

#newObject

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

#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

#run(id) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/taskr/controllers.rb', line 164

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
  
  render :view_task
end