-
Notifications
You must be signed in to change notification settings - Fork 15
/
Rakefile
154 lines (131 loc) · 3.79 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
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
require 'fileutils'
require 'bundler/setup'
require 'yaml'
require 'redis'
require 'resque/tasks'
require 'resque/scheduler/tasks'
ROOT_PATH = File.dirname(__FILE__) + '/../'
# Start a worker with proper env vars and output redirection
def run_worker(queue, count = 1)
puts "Starting #{count} worker(s) with QUEUE: #{queue}"
ops = {:pgroup => true, :err => [(ROOT_PATH + "log/resque.workers.error.log").to_s, "a"],
:out => [(ROOT_PATH + "log/resque.workers.log").to_s, "a"]}
env_vars = {"QUEUE" => queue.to_s}
count.times {
## 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)
}
end
# Start a scheduler, requires resque_scheduler >= 2.0.0.f
def run_scheduler
puts "Starting resque scheduler"
env_vars = {
"BACKGROUND" => "1",
"PIDFILE" => (ROOT_PATH + "/pid/resque-scheduler.pid").to_s,
"VERBOSE" => "1"
}
ops = {:pgroup => true, :err => [(ROOT_PATH + "log/resque.scheduler.error.log").to_s, "a"],
:out => [(ROOT_PATH + "log/resque.scheduler.log").to_s, "a"]}
pid = spawn(env_vars, "rake resque:scheduler", ops)
Process.detach(pid)
end
namespace :resque do
task :environment do
require 'resque'
require 'resque_scheduler'
require 'resque/scheduler'
Resque.redis = 'localhost:6379'
Resque.schedule = YAML.load_file('config/schedule.yml')
require_relative 'jobs'
end
task :setup => :environment
desc "Restart running workers"
task :restart_workers => :environment do
Rake::Task['resque:stop_workers'].invoke
Rake::Task['resque:start_workers'].invoke
end
desc "Quit running workers"
task :stop_workers => :environment do
pids = Array.new
Resque.workers.each do |worker|
pids.concat(worker.worker_pids)
end
if pids.empty?
puts "No workers to kill"
else
syscmd = "kill -s QUIT #{pids.join(' ')}"
puts "Running syscmd: #{syscmd}"
system(syscmd)
end
end
desc "Start workers"
task :start_workers => :environment do
run_worker("*", 2)
run_worker("high", 1)
end
desc "Restart scheduler"
task :restart_scheduler => :environment do
Rake::Task['resque:stop_scheduler'].invoke
Rake::Task['resque:start_scheduler'].invoke
end
desc "Quit scheduler"
task :stop_scheduler => :environment do
pidfile = ROOT_PATH + "pid/resque-scheduler.pid"
if !File.exists?(pidfile)
puts "Scheduler not running"
else
pid = File.read(pidfile).to_i
syscmd = "kill -s QUIT #{pid}"
puts "Running syscmd: #{syscmd}"
system(syscmd)
FileUtils.rm_f(pidfile)
end
end
desc "Start scheduler"
task :start_scheduler => :environment do
run_scheduler
end
desc "Reload schedule"
task :reload_schedule => :environment do
pidfile = ROOT_PATH + "pid/resque-scheduler.pid"
if !File.exists?(pidfile)
puts "Scheduler not running"
else
pid = File.read(pidfile).to_i
syscmd = "kill -s USR2 #{pid}"
puts "Running syscmd: #{syscmd}"
system(syscmd)
end
end
end
# # #
def gemspec
name = Dir['*.gemspec'].first
@gemspec ||= eval(File.read(name), binding, name)
end
desc "Build the gem"
task :gem => :gemspec do
sh "gem build #{gemspec.name}.gemspec"
FileUtils.mkdir_p 'pkg'
FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", 'pkg'
end
desc "Install the gem locally"
task :install => :gem do
sh %{gem install pkg/#{gemspec.name}-#{gemspec.version}.gem --no-doc}
end
desc "Generate the gemspec"
task :generate do
puts gemspec.to_ruby
end
desc "Validate the gemspec"
task :gemspec do
gemspec.validate
end
desc 'rspec specs'
task :spec do
sh %[rspec spec]
end
task :default => :spec
task :test => :spec