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



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
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
# File 'lib/taskr/controllers.rb', line 66

def create
  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]
    
    @task = Task.new(
      :name => name,
      :created_by => created_by,
      :schedule_method => schedule_method,
      :schedule_when => schedule_when
    )
    
    # 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? OpenWFE::Schedulable
          raise ArgumentError, 
            "#{a[:action_class_name].inspect} cannot be used as an action because it does not include the OpenWFE::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|
        action.action_parameters << TaskActionParameter.new(:name => p, :value => a[p])
      end
      
      @task.task_actions << action
      i += 1
    end
    
    
    unless @task.valid?
      @status = 500
      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



153
154
155
156
157
158
159
160
161
# File 'lib/taskr/controllers.rb', line 153

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
  return redirect(R(Tasks, :list))
end

#listObject



48
49
50
51
52
# File 'lib/taskr/controllers.rb', line 48

def list
  @tasks = Task.find(:all, :include => [:task_actions])
  
  render :tasks_list
end

#newObject



54
55
56
57
58
# File 'lib/taskr/controllers.rb', line 54

def new
  @actions = Taskr::Actions.list
  
  render :new_task
end

#read(id) ⇒ Object



60
61
62
63
64
# File 'lib/taskr/controllers.rb', line 60

def read(id)
  @task = Task.find(id, :include => [:task_actions])
  
  render :view_task
end