Class: Resque::Pool::Dynamic
- Inherits:
-
Object
- Object
- Resque::Pool::Dynamic
- Defined in:
- lib/resque/pool/dynamic/shell.rb,
lib/resque/pool/dynamic.rb,
lib/resque/pool/dynamic/logfile.rb,
lib/resque/pool/dynamic/version.rb
Overview
Add-on to resque-pool to allow manually controlled, dynamic sessions
Defined Under Namespace
Constant Summary collapse
- VERSION =
"0.1.3"
Instance Attribute Summary collapse
-
#pid ⇒ Integer, NilClass
readonly
PID of the resque-pool process, or nil if it’s not running.
Class Method Summary collapse
-
.shell(options = {}) ⇒ Object
Run a dynamic resque pool session.
Instance Method Summary collapse
- #config(args = nil) ⇒ Object
-
#config_path ⇒ String
Path to the temporary config file.
-
#config_tempfile ⇒ Tempfile
Tempfile instance for configuration file.
-
#has_log? ⇒ Boolean
True if we have an open log file.
-
#initialize ⇒ Dynamic
constructor
A new instance of Dynamic.
-
#kill!(sig) ⇒ Object
Send signal to a running resque-pool.
-
#log ⇒ Logfile
Open log of resque-pool process.
-
#log_path ⇒ String
Logfile path.
-
#loop_with_index {|i| ... } ⇒ Object
Loop, providing index to the block.
-
#parse_config_string(spec) ⇒ Hash
Parse workers configuration.
-
#pid! ⇒ Integer
Return pid of running resque-pool or raise an exception.
-
#pstree ⇒ Object
Show child process tree by calling ‘pstree` system command.
-
#reload ⇒ Object
Reload resque-pool configuration.
-
#run_pool ⇒ Object
Start resque-pool master This is executed in a forked child process.
-
#start ⇒ Object
Start resque-pool, showing startup logs.
-
#start! ⇒ Integer
Fork a child process for resque-pool master.
-
#status ⇒ Object
Show current status.
-
#stop ⇒ Object
Stop running resque-pool, show shutdown logs.
-
#stop! ⇒ Object
Stop running resque-pool.
-
#write_config ⇒ Object
Write temporary configuration file.
Constructor Details
#initialize ⇒ Dynamic
Returns a new instance of Dynamic.
15 16 17 18 |
# File 'lib/resque/pool/dynamic.rb', line 15 def initialize # Make myself a project group leader Process.setpgid(0, 0) end |
Instance Attribute Details
#pid ⇒ Integer, NilClass (readonly)
PID of the resque-pool process, or nil if it’s not running
13 14 15 |
# File 'lib/resque/pool/dynamic.rb', line 13 def pid @pid end |
Class Method Details
Instance Method Details
#config ⇒ Hash #config(opts) ⇒ Hash
Default configuration is taken from environment variable “WORKERS”, as interpreted by the #parse_config_string method.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/resque/pool/dynamic.rb', line 66 def config(args=nil) @config ||= parse_config_string(ENV['WORKERS']) if args oldconfig = config.clone args.each do |w, n| w = w.to_s if n.nil? || n==0 @config.delete w else @config[w] = n end end if pid && @config != oldconfig write_config reload end end @config end |
#config_path ⇒ String
Path to the temporary config file
29 30 31 |
# File 'lib/resque/pool/dynamic.rb', line 29 def config_path config_tempfile.path end |
#config_tempfile ⇒ Tempfile
Tempfile instance for configuration file
22 23 24 |
# File 'lib/resque/pool/dynamic.rb', line 22 def config_tempfile @config_tempfile ||= Tempfile.new(%w(resque-pool-dynamic .yaml)) end |
#has_log? ⇒ Boolean
True if we have an open log file
24 25 26 |
# File 'lib/resque/pool/dynamic/logfile.rb', line 24 def has_log? !!@log end |
#kill!(sig) ⇒ Object
Send signal to a running resque-pool
154 155 156 |
# File 'lib/resque/pool/dynamic.rb', line 154 def kill!(sig) Process.kill(sig, pid!) end |
#log ⇒ Logfile
Open log of resque-pool process
17 18 19 20 |
# File 'lib/resque/pool/dynamic/logfile.rb', line 17 def log # pid! call will raise an exception if process is not running yet @log ||= Logfile.open(log_path, :pid => pid!) end |
#log_path ⇒ String
Logfile path
9 10 11 12 |
# File 'lib/resque/pool/dynamic/logfile.rb', line 9 def log_path # pid! call will raise an exception if process is not running yet @log_path ||= ENV['RESQUE_POOL_LOG'] || "resque-pool.#{pid!}.log" end |
#loop_with_index {|i| ... } ⇒ Object
Loop, providing index to the block
97 98 99 100 101 102 103 |
# File 'lib/resque/pool/dynamic/shell.rb', line 97 def loop_with_index index = 0 loop do yield(index) index += 1 end end |
#parse_config_string(spec) ⇒ Hash
Parse workers configuration
40 41 42 43 44 45 46 |
# File 'lib/resque/pool/dynamic.rb', line 40 def parse_config_string(spec) return {} unless spec Hash[ spec.split(':').map { |w| k, v = w.split('=') ; [ k, v.to_i ] } ] end |
#pid! ⇒ Integer
Return pid of running resque-pool or raise an exception
147 148 149 |
# File 'lib/resque/pool/dynamic.rb', line 147 def pid! pid or raise "Not started!" end |
#pstree ⇒ Object
Show child process tree by calling ‘pstree` system command
189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/resque/pool/dynamic.rb', line 189 def pstree system case RUBY_PLATFORM when /darwin/ "pstree -w #{pid!}" when /linux/ "pstree -l -a -p #{pid!}" else "pstree #{pid!}" end nil end |
#reload ⇒ Object
Reload resque-pool configuration
181 182 183 184 185 |
# File 'lib/resque/pool/dynamic.rb', line 181 def reload puts "Reloading resque-pool-master #{pid!} configuration" write_config kill!('HUP') end |
#run_pool ⇒ Object
Start resque-pool master This is executed in a forked child process.
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/resque/pool/dynamic.rb', line 100 def run_pool @pid = $$ ENV["RESQUE_POOL_CONFIG"] = config_path $stdin.reopen '/dev/null' log = File.new(log_path, "a") $stdout.reopen log $stderr.reopen log $stdout.sync = $stderr.sync = true require 'rake' Rake::Task['resque:pool'].invoke end |
#start ⇒ Object
Start resque-pool, showing startup logs
134 135 136 137 138 139 140 141 142 |
# File 'lib/resque/pool/dynamic.rb', line 134 def start unless pid start! log.rewind log.tail_until /started manager/ else warn "Already started as #{pid}" end end |
#start! ⇒ Integer
Fork a child process for resque-pool master
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/resque/pool/dynamic.rb', line 117 def start! raise "Already running: #{pid}" if pid write_config @pid = fork { self.run_pool } Process.setpgid(pid, 0) # don't broadcast ^C to child if 30.times { |i| sleep 1 ; break if File.exist? log_path } # Loop will return nil if broken. If value is returned, file does not exist. raise "Log file #{log_path} still not present after #{i} seconds, giving up" stop! end pid end |
#status ⇒ Object
Show current status
203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/resque/pool/dynamic.rb', line 203 def status puts( '', "Status: " << ( pid ? "running, pid: #{pid}" : "not running" ), "Configuration:", YAML::dump(config).lines.grep(/^(?!---\s*$)/).map { |v| " " << v } ) if pid puts "Process tree:" pstree end puts end |
#stop ⇒ Object
Stop running resque-pool, show shutdown logs
171 172 173 174 175 176 177 |
# File 'lib/resque/pool/dynamic.rb', line 171 def stop puts "Shutting down resque-pool-master #{pid}" log.ff if has_log? status = stop! log.tail if has_log? return status end |
#stop! ⇒ Object
Stop running resque-pool
160 161 162 163 164 165 166 167 |
# File 'lib/resque/pool/dynamic.rb', line 160 def stop! if pid kill! "INT" wpid, status = Process.wait2(pid) @pid = nil status end end |
#write_config ⇒ Object
Write temporary configuration file
92 93 94 95 96 |
# File 'lib/resque/pool/dynamic.rb', line 92 def write_config File.open(config_path, 'w') do |cf| cf.write(YAML::dump(config)) end end |