Class: ResqueController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/resque_controller.rb

Instance Method Summary collapse

Instance Method Details

#add_scheduled_jobObject



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
# File 'app/controllers/resque_controller.rb', line 119

def add_scheduled_job
  errors = []
  if Resque.schedule.keys.include?(params[:name])
    errors << 'Name already exists'
  end
  if params[:ip].blank?
    errors << 'You must enter an ip address for the server you want this job to run on.'
  end
  if params[:cron].blank?
    errors << 'You must enter the cron schedule.'
  end
  if errors.blank?
    config = {params['name'] => {'class' => params['class'],
                                 'ip' => params['ip'],
                                 'cron' => params['cron'],
                                 'args' => Resque.decode(params['args'].blank? ? nil : params['args']),
                                 'description' => params['description']}
    }
    Resque.redis.rpush(:scheduled, Resque.encode(config))
    ResqueScheduler.restart('ip')
  else
    flash[:error] = errors.join('<br>')
  end
  redirect_to(:action => 'schedule')
end

#cleanerObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'app/controllers/resque_controller.rb', line 199

def cleaner
  load_cleaner_filter

  @jobs = @cleaner.select
  @stats, @total = {}, {"total" => 0, "1h" => 0, "3h" => 0, "1d" => 0, "3d" => 0, "7d" => 0}
  @jobs.each do |job|
    klass = job["payload"]["class"]
    failed_at = Time.parse job["failed_at"]

    @stats[klass] ||= {"total" => 0, "1h" => 0, "3h" => 0, "1d" => 0, "3d" => 0, "7d" => 0}
    items = [@stats[klass], @total]

    items.each { |a| a["total"] += 1 }
    items.each { |a| a["1h"] += 1 } if failed_at >= hours_ago(1)
    items.each { |a| a["3h"] += 1 } if failed_at >= hours_ago(3)
    items.each { |a| a["1d"] += 1 } if failed_at >= hours_ago(24)
    items.each { |a| a["3d"] += 1 } if failed_at >= hours_ago(24*3)
    items.each { |a| a["7d"] += 1 } if failed_at >= hours_ago(24*7)
  end
end

#cleaner_dumpObject



259
260
261
262
263
264
265
266
267
# File 'app/controllers/resque_controller.rb', line 259

def cleaner_dump
  load_cleaner_filter

  block = filter_block
  failures = @cleaner.select(&block)
  # pretty generate throws an error with the json gem on jruby
  output = JSON.pretty_generate(failures) rescue failures.to_json
  render :json => output
end

#cleaner_execObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'app/controllers/resque_controller.rb', line 236

def cleaner_exec
  load_cleaner_filter

  if params[:select_all_pages]!="1"
    @sha1 = {}
    params[:sha1].split(",").each { |s| @sha1[s] = true }
  end

  block = filter_block

  @count =
      case params[:form_action]
        when "clear" then
          @cleaner.clear(&block)
        when "retry_and_clear" then
          @cleaner.requeue(true, &block)
        when "retry" then
          @cleaner.requeue(false, {}, &block)
      end

  @link_url = "cleaner_list?c=#{@klass}&ex=#{@exception}&f=#{@from}&t=#{@to}"
end

#cleaner_listObject



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'app/controllers/resque_controller.rb', line 220

def cleaner_list
  load_cleaner_filter

  block = filter_block

  @failed = @cleaner.select(&block).reverse

  url = "cleaner_list?c=#{@klass}&ex=#{@exception}&f=#{@from}&t=#{@to}"
  @dump_url = "cleaner_dump?c=#{@klass}&ex=#{@exception}&f=#{@from}&t=#{@to}"
  @paginate = Paginate.new(@failed, url, params[:p].to_i)

  @klasses = @cleaner.stats_by_class.keys
  @exceptions = @cleaner.stats_by_exception.keys
  @count = @cleaner.select(&block).size
end

#cleaner_staleObject



269
270
271
272
# File 'app/controllers/resque_controller.rb', line 269

def cleaner_stale
  @cleaner.clear_stale
  redirect_to :action => "cleaner"
end

#clear_statusesObject



179
180
181
182
# File 'app/controllers/resque_controller.rb', line 179

def clear_statuses
  Resque::Status.clear
  redirect_to(:action => 'statuses')
end

#continue_workerObject



67
68
69
70
71
# File 'app/controllers/resque_controller.rb', line 67

def continue_worker
  worker = find_worker(params[:worker])
  worker.continue if worker
  redirect_to(:action => "workers")
end

#indexObject



22
23
24
# File 'app/controllers/resque_controller.rb', line 22

def index
  redirect_to(:action => 'overview')
end

#killObject



191
192
193
194
195
196
197
# File 'app/controllers/resque_controller.rb', line 191

def kill
  Resque::Status.kill(params[:id])
  s = Resque::Status.get(params[:id])
  s.status = 'killed'
  Resque::Status.set(params[:id], s)
  redirect_to(:action => 'statuses')
end

#pause_workerObject



61
62
63
64
65
# File 'app/controllers/resque_controller.rb', line 61

def pause_worker
  worker = find_worker(params[:worker])
  worker.pause if worker
  redirect_to(:action => "workers")
end

#pollObject



34
35
36
37
# File 'app/controllers/resque_controller.rb', line 34

def poll
  @polling = true
  render(:text => (render_to_string(:action => "#{params[:page]}.html", :layout => false, :resque => Resque)).gsub(/\s{1,}/, ' '))
end

#queuesObject



30
31
32
# File 'app/controllers/resque_controller.rb', line 30

def queues
  render('_queues', :locals => {:partial => nil})
end

#remove_from_scheduleObject



145
146
147
148
149
150
151
152
153
154
155
# File 'app/controllers/resque_controller.rb', line 145

def remove_from_schedule
  Resque.list_range(:scheduled, 0, -0).each do |s|

    if s[params['job_name']]
      Resque.redis.lrem(:scheduled, 0, s.to_json)
      # Restart the scheduler on the server that has changed it's schedule
      ResqueScheduler.restart(params['ip'])
    end
  end
  redirect_to(:action => 'schedule')
end

#remove_jobObject



50
51
52
53
# File 'app/controllers/resque_controller.rb', line 50

def remove_job
  Resque.dequeue(params['class'].constantize, *Resque.decode(params['args']))
  redirect_to request.referrer
end

#restart_workerObject



73
74
75
76
77
# File 'app/controllers/resque_controller.rb', line 73

def restart_worker
  worker = find_worker(params[:worker])
  worker.restart if worker
  redirect_to(:action => "workers")
end

#scheduleObject

resque-scheduler actions



109
110
111
# File 'app/controllers/resque_controller.rb', line 109

def schedule
  @farm_status = ResqueScheduler.farm_status
end

#schedule_requeueObject



113
114
115
116
117
# File 'app/controllers/resque_controller.rb', line 113

def schedule_requeue
  config = Resque.schedule[params['job_name']]
  Resque::Scheduler.enqueue_from_config(config)
  redirect_to(:action => 'overview')
end

#start_schedulerObject



157
158
159
160
# File 'app/controllers/resque_controller.rb', line 157

def start_scheduler
  ResqueScheduler.start(params[:ip])
  redirect_to(:action => 'schedule')
end

#start_workerObject



79
80
81
82
# File 'app/controllers/resque_controller.rb', line 79

def start_worker
  Resque::Worker.start(params[:hosts], params[:queues])
  redirect_to(:action => "workers")
end

#statsObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/resque_controller.rb', line 84

def stats
  unless params[:id]
    redirect_to(:action => 'stats', :id => 'resque')
  end

  if params[:id] == 'txt'
    info = Resque.info

    stats = []
    stats << "resque.pending=#{info[:pending]}"
    stats << "resque.processed+=#{info[:processed]}"
    stats << "resque.failed+=#{info[:failed]}"
    stats << "resque.workers=#{info[:workers]}"
    stats << "resque.working=#{info[:working]}"

    Resque.queues.each do |queue|
      stats << "queues.#{queue}=#{Resque.size(queue)}"
    end

    render(:text => stats.join("</br>").html_safe)
  end
end

#statusObject



184
185
186
187
188
189
# File 'app/controllers/resque_controller.rb', line 184

def status
  @status = Resque::Status.get(params[:id])
  if params[:format] == 'js'
    render :text => @status.to_json
  end
end

#status_pollObject



39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/resque_controller.rb', line 39

def status_poll
  @polling = true

  @start = params[:start].to_i
  @end = @start + (params[:per_page] || 20)
  @statuses = Resque::Status.statuses(@start, @end)
  @size = Resque::Status.status_ids.size

  render(:text => (render_to_string(:action => 'statuses.html', :layout => false)))
end

#statusesObject

resque-status actions



169
170
171
172
173
174
175
176
177
# File 'app/controllers/resque_controller.rb', line 169

def statuses
  @start = params[:start].to_i
  @end = @start + (params[:per_page] || 20)
  @statuses = Resque::Status.statuses(@start, @end)
  @size = Resque::Status.status_ids.size
  if params[:format] == 'js'
    render :text => @statuses.to_json
  end
end

#stop_schedulerObject



162
163
164
165
# File 'app/controllers/resque_controller.rb', line 162

def stop_scheduler
  ResqueScheduler.quit(params[:ip])
  redirect_to(:action => 'schedule')
end

#stop_workerObject



55
56
57
58
59
# File 'app/controllers/resque_controller.rb', line 55

def stop_worker
  worker = find_worker(params[:worker])
  worker.quit if worker
  redirect_to(:action => "workers")
end

#workingObject



26
27
28
# File 'app/controllers/resque_controller.rb', line 26

def working
  render('_working')
end