This repository has been archived by the owner on Jan 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.rb
97 lines (81 loc) · 2.43 KB
/
server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
require 'cgi'
require 'resque/server'
require 'resque/scheduler/server'
# Extend Resque::Server to add tabs.
module GMQ
module Server
# Adds `resque-retry` web interface elements to `resque-web`
#
# @api private
def self.included(base)
base.class_eval {
# get '/gmq' do
# erb local_template('retry.erb')
# end
get '/gmq/:timestamp' do
erb local_template('retry_timestamp.erb')
end
get '/gmq' do
erb local_template('retryAll.erb')
end
get '/gmq-slow-retry' do
erb local_template('retryAllSlowly.erb')
end
post '/gmq/:timestamp/remove' do
Resque.delayed_timestamp_peek(params[:timestamp], 0, 0).each do |job|
cancel_retry(job)
end
redirect u('retry')
end
post '/gmq/:timestamp/jobs/:id/remove' do
job = Resque.decode(params[:id])
cancel_retry(job)
redirect u("retry/#{params[:timestamp]}")
end
}
end
# Helper methods used by retry tab.
module Helpers
# builds a retry key for the specified job.
def retry_key_for_job(job)
klass = get_class(job)
if klass.respond_to?(:redis_retry_key)
klass.redis_retry_key(job['args'])
else
nil
end
end
# gets the number of retry attempts for a job.
def retry_attempts_for_job(job)
Resque.redis.get(retry_key_for_job(job))
end
# gets the failure details hash for a job.
def retry_failure_details(retry_key)
Resque.decode(Resque.redis.get("failure-#{retry_key}"))
end
# reads a 'local' template file.
def local_template(path)
# Is there a better way to specify alternate template locations with sinatra?
File.read(File.join(File.dirname(__FILE__), "server/views/#{path}"))
end
# cancels job retry
def cancel_retry(job)
klass = get_class(job)
retry_key = retry_key_for_job(job)
Resque.remove_delayed(klass, *job['args'])
Resque.redis.del("failure-#{retry_key}")
Resque.redis.del(retry_key)
end
private
def get_class(job)
Resque::Job.new(nil, nil).constantize(job['class'])
end
end
end
end
Resque::Server.tabs << 'GMQ'
Resque::Server.tabs << 'GMQ-Slow-Retry'
Resque::Server.class_eval do
include GMQ::Server
helpers GMQ::Server::Helpers
end