This repository has been archived by the owner on Dec 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathRakefile
110 lines (87 loc) · 2.94 KB
/
Rakefile
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
98
99
100
101
102
103
104
105
106
107
108
109
110
require 'resque/tasks'
require File.join(File.dirname(__FILE__), 'workers/graphite_worker')
require File.join(File.dirname(__FILE__), 'workers/redis_worker')
namespace :resque do
task :setup
desc "Restart running workers"
task :restart_workers do
Rake::Task['resque:stop_workers'].invoke
Rake::Task['resque:start_workers'].invoke
end
desc "Quit running workers"
task :stop_workers do
stop_workers
end
desc "Start workers"
task :start_workers do
run_worker("process_graphite_namespace,process_redis_metrics", 22)
run_worker("process_redis_metrics,process_graphite_namespace", 22)
end
def store_pids(pids, mode)
pids_to_store = pids
pids_to_store += read_pids if mode == :append
# Make sure the pid file is writable.
File.open('/var/run/oculus/resque.pid', 'w') do |f|
f << pids_to_store.join(',')
end
end
def read_pids
pid_file_path = "/var/run/oculus/resque.pid"
return [] if ! File.exists?(pid_file_path)
File.open(pid_file_path, 'r') do |f|
f.read
end.split(',').collect {|p| p.to_i }
end
def stop_workers
pids = read_pids
if pids.empty?
puts "No workers to kill"
else
syscmd = "kill -s QUIT #{pids.join(' ')}"
puts "$ #{syscmd}"
`#{syscmd}`
store_pids([], :write)
end
end
# Start a worker with proper env vars and output redirection
def run_worker(queue, count = 1)
if !File.exists?("/var/run/oculus")
puts "/var/run/oculus doesn't exist. Please create it."
exit 1
end
if !File.readable?("/var/run/oculus")
puts "/var/run/oculus isn't readable by this user. Please check permissions."
exit 1
end
if !File.writable?("/var/run/oculus")
puts "/var/run/oculus isn't writable by this user. Please check permissions."
exit 1
end
if !File.exists?("/var/log/oculus")
puts "/var/log/oculus doesn't exist. Please create it."
exit 1
end
if !File.readable?("/var/log/oculus")
puts "/var/log/oculus isn't readable by this user. Please check permissions."
exit 1
end
if !File.writable?("/var/log/oculus")
puts "/var/log/oculus isn't writable by this user. Please check permissions."
exit 1
end
puts "Starting #{count} worker(s) with QUEUE: #{queue}"
## make sure log/resque_err, log/resque_stdout are writable.
ops = {:pgroup => true, :err => ["/var/log/oculus/resque_err", "a"],
:out => ["/var/log/oculus/resque_stdout", "a"]}
env_vars = {"REDIS_URL" => "http://oculusredis01:6379", "QUEUE" => queue.to_s, "TERM_CHILD" => "1"}
pids = []
count.times do
## Using Kernel.spawn and Process.detach because regular system() call would
## cause the processes to quit when capistrano finishes
pid = spawn(env_vars, "rake resque:work", ops)
Process.detach(pid)
pids << pid
end
store_pids(pids, :append)
end
end