Class: Resque::Master
- Inherits:
-
Object
- Object
- Resque::Master
- Defined in:
- lib/resque/master.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
- .daemonize(options) ⇒ Object
- .defaults ⇒ Object
- .optparse! ⇒ Object
- .process(config_path) ⇒ Object
- .setup(options) ⇒ Object
Instance Method Summary collapse
- #after_fork(&block) ⇒ Object
- #before_fork(&block) ⇒ Object
- #daemonize(yes_no) ⇒ Object
-
#initialize(path) ⇒ Master
constructor
A new instance of Master.
- #pid(pidfile) ⇒ Object
- #preload_app(yes_no) ⇒ Object
- #run ⇒ Object
- #setup(&block) ⇒ Object
- #stderr_path(path) ⇒ Object
- #stdout_path(path) ⇒ Object
- #teardown(&block) ⇒ Object
- #work_interval(interval) ⇒ Object
- #worker_processes(count) ⇒ Object
- #worker_queues(queues) ⇒ Object
- #worker_timeout(timeout) ⇒ Object
- #working_directory(path) ⇒ Object
Constructor Details
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/resque/master.rb', line 7 def @options end |
Class Method Details
.daemonize(options) ⇒ Object
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/resque/master.rb', line 121 def self.daemonize() $pidfile = [:pidfile] rd, wr = IO.pipe stdout = [:stdout_path] || "/dev/null" stderr = [:stderr_path] || "/dev/null" # wait for child process to start running before exiting parent process fork do rd.close Process.setsid fork do begin wr.write "Prepare to fork: #{.inspect}, stdout: #{stdout.inspect}, stderr: #{stderr.inspect}, stdin: /dev/null" Process.setsid Dir.chdir([:runpath]) File.umask 0000 STDIN.reopen "/dev/null" STDOUT.reopen stdout, "a" STDERR.reopen stderr, "a" wr.flush wr.close # signal to our parent we're up and running, this lets the parent exit Resque.fork! # fork to startup the workers rescue => e if wr.closed? STDERR.puts "#{e.} #{e.backtrace.join("\n")}" else wr.write e. wr.write e.backtrace.join("\n") wr.write "\n" wr.write "ERROR!" wr.flush wr.close end end end wr.close end wr.close output = rd.read puts output rd.close end |
.defaults ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/resque/master.rb', line 217 def self.defaults { :config => nil, :logfile => nil, :stderr_path => "/dev/null", :stdout_path => "/dev/null", :pidfile => nil, :daemon => false, :preload_app => true, :worker_queues => ['*'], :worker_processes => 1, :work_interval => 5 } end |
.optparse! ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/resque/master.rb', line 169 def self.optparse! = {} optparse = OptionParser.new do|opts| # Set a banner, displayed at the top # of the help screen. opts. = "Usage: optparse1.rb [options] file1 file2 ..." opts.on( '-d', '--daemon', 'Run the master process as daemon' ) do [:daemon] = true end opts.on( '-p', '--pid FILE', 'Write pid file to FILE' ) do|file| [:pidfile] = file end opts.on( '-r', '--run PATH', 'Where to run the forked daemon' ) do|file| [:runpath] = file end opts.on( '-e', '--stderr PATH', 'Where to send stderr output' ) do|file| [:stderr_path] = file end opts.on( '-o', '--stdout PATH', 'Where to send stdout output' ) do|file| [:stdout_path] = file end opts.on( '-c', '--config PATH', 'Path to a configuration file to load' ) do|file| [:config] = file end opts.on( '-l', '--logfile FILE', 'Write log to FILE' ) do|file| [:logfile] = file end # This displays the help screen, all programs are # assumed to have this option. opts.on( '-h', '--help', 'Display this screen' ) do puts opts exit end end optparse.parse! end |
.process(config_path) ⇒ Object
70 71 72 73 74 |
# File 'lib/resque/master.rb', line 70 def self.process(config_path) loader = Resque::Master.new(config_path) loader.run loader. end |
.setup(options) ⇒ Object
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 |
# File 'lib/resque/master.rb', line 82 def self.setup() [:runpath] = Dir.pwd if [:runpath].nil? Resque.setup do |forker| if [:preload_app] begin $:.unshift [:runpath] # Makes 1.9.2 happy require [:runpath] + "/config/environment" puts "Loaded Rails: #{Rails.env}" forker.logger = Rails.logger rescue => e STDERR.puts e. STDERR.puts e.backtrace.join("\n") raise e end end File.open([:pidfile],"wb") {|f| f << Process.pid } if [:pidfile] forker.workload = [:worker_queues] * [:worker_processes].to_i if [:worker_queues] && [:worker_processes] forker..interval = [:work_interval].to_i if .key?(:work_interval) end Resque.teardown do|forker| File.unlink([:pidfile]) if [:pidfile] && File.exist?([:pidfile]) end Resque.before_first_fork do [:before_first_fork].call if [:before_first_fork].is_a?(Proc) end Resque.before_fork do [:before_fork].call if [:before_fork].is_a?(Proc) end Resque.after_fork do [:after_fork].call if [:after_fork].is_a?(Proc) end end |
Instance Method Details
#after_fork(&block) ⇒ Object
53 54 55 |
# File 'lib/resque/master.rb', line 53 def after_fork(&block) @options[:after_fork] = block end |
#before_fork(&block) ⇒ Object
49 50 51 |
# File 'lib/resque/master.rb', line 49 def before_fork(&block) @options[:before_fork] = block end |
#daemonize(yes_no) ⇒ Object
61 62 63 |
# File 'lib/resque/master.rb', line 61 def daemonize(yes_no) @options[:daemon] = yes_no end |
#pid(pidfile) ⇒ Object
29 30 31 |
# File 'lib/resque/master.rb', line 29 def pid(pidfile) @options[:pidfile] = pidfile end |
#preload_app(yes_no) ⇒ Object
57 58 59 |
# File 'lib/resque/master.rb', line 57 def preload_app(yes_no) @options[:preload_app] = yes_no end |
#run ⇒ Object
76 77 78 79 80 |
# File 'lib/resque/master.rb', line 76 def run self.instance_eval(File.read(@config_path), @config_path, 0) rescue => e raise "Config error: '#{e.}' at #{e.backtrace[0].gsub(/:in `run'/,'')}" end |
#setup(&block) ⇒ Object
41 42 43 |
# File 'lib/resque/master.rb', line 41 def setup(&block) @options[:setup] = block end |
#stderr_path(path) ⇒ Object
33 34 35 |
# File 'lib/resque/master.rb', line 33 def stderr_path(path) @options[:stderr_path] = path end |
#stdout_path(path) ⇒ Object
37 38 39 |
# File 'lib/resque/master.rb', line 37 def stdout_path(path) @options[:stdout_path] = path end |
#teardown(&block) ⇒ Object
45 46 47 |
# File 'lib/resque/master.rb', line 45 def teardown(&block) @options[:teardown] = block end |
#work_interval(interval) ⇒ Object
21 22 23 |
# File 'lib/resque/master.rb', line 21 def work_interval(interval) @options[:work_interval] = interval end |
#worker_processes(count) ⇒ Object
9 10 11 |
# File 'lib/resque/master.rb', line 9 def worker_processes(count) @options[:worker_processes] = count end |
#worker_queues(queues) ⇒ Object
13 14 15 |
# File 'lib/resque/master.rb', line 13 def worker_queues(queues) @options[:worker_queues] = queues end |
#worker_timeout(timeout) ⇒ Object
17 18 19 |
# File 'lib/resque/master.rb', line 17 def worker_timeout(timeout) @options[:worker_timeout] = timeout end |
#working_directory(path) ⇒ Object
25 26 27 |
# File 'lib/resque/master.rb', line 25 def working_directory(path) @options[:runpath] = path end |