Class: RQ::Relayer
- Inherits:
-
MainHelper
- Object
- MainHelper
- RQ::Relayer
- Defined in:
- lib/rq/relayer.rb
Constant Summary collapse
- DEFAULT_MIN_SLEEP =
–{{{
42
- DEFAULT_MAX_SLEEP =
240
- DEFAULT_RELAY =
16
Constants included from Logging
Logging::DIV0, Logging::DIV1, Logging::DIV2, Logging::DIV3, Logging::EOL, Logging::SEC0, Logging::SEC1, Logging::SEC2, Logging::SEC3
Class Attribute Summary collapse
-
.max_sleep ⇒ Object
Returns the value of attribute max_sleep.
-
.min_sleep ⇒ Object
–{{{.
-
.relay ⇒ Object
Returns the value of attribute relay.
Attributes inherited from MainHelper
#argv, #cmd, #dot_rq_dir, #env, #fields, #job_stdin, #loops, #main, #mode, #options, #program, #q, #qpath, #quiet, #stdin
Instance Method Summary collapse
- #daemon ⇒ Object
-
#gen_pidfile(name = nil) ⇒ Object
–}}}.
-
#gen_relayer_name(path) ⇒ Object
–}}}.
-
#handle_signal ⇒ Object
–}}}.
-
#install_signal_handlers ⇒ Object
–}}}.
-
#reap ⇒ Object
–}}}.
-
#reap_and_sow ⇒ Object
TODO - this will need to map jids here to jids there.
-
#relax ⇒ Object
–}}}.
- #relay ⇒ Object
-
#throttle(rate = @min_sleep) ⇒ Object
–}}}.
-
#transaction ⇒ Object
–}}}.
Methods inherited from MainHelper
#dumping_yaml_tuples, #field_match, #init_job_stdin!, #initialize, #loadio, #loadyaml, #set_q
Methods included from Logging
Methods included from Logging::LogMethods
#debug, #error, #fatal, #info, #logerr, #logger, #logger=, #warn
Methods included from Util
#alive?, append_features, #btrace, #columnize, #defval, #emsg, #erreq, #errmsg, #escape, #escape!, #exec, export, #fork, #getopt, #hashify, #hms, #host, #hostname, #klass, #maim, #mcp, #realpath, #stamptime, #system, #timestamp, #tmpnam, #uncache, #which_ruby
Constructor Details
This class inherits a constructor from RQ::MainHelper
Class Attribute Details
.max_sleep ⇒ Object
Returns the value of attribute max_sleep.
22 23 24 |
# File 'lib/rq/relayer.rb', line 22 def max_sleep @max_sleep end |
.min_sleep ⇒ Object
–{{{
21 22 23 |
# File 'lib/rq/relayer.rb', line 21 def min_sleep @min_sleep end |
.relay ⇒ Object
Returns the value of attribute relay.
23 24 25 |
# File 'lib/rq/relayer.rb', line 23 def relay @relay end |
Instance Method Details
#daemon ⇒ Object
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 |
# File 'lib/rq/relayer.rb', line 98 def daemon #--{{{ if @options['daemon'] fork do Process::setsid fork do Dir::chdir(Util.realpath('~')) File::umask 0 open('/dev/null','r+') do |f| STDIN.reopen f STDOUT.reopen f STDERR.reopen f end @daemon = true yield exit EXIT_SUCCESS end exit! end exit! else @daemon = false yield exit EXIT_SUCCESS end #--}}} end |
#gen_pidfile(name = nil) ⇒ Object
–}}}
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 |
# File 'lib/rq/relayer.rb', line 125 def gen_pidfile name = nil #--{{{ name ||= gen_relayer_name(@options['name'] || @qpath) @pidfile = begin open name, File::CREAT | File::EXCL | File::RDWR rescue open name, File::RDWR end unless @pidfile and @pidfile.posixlock(File::LOCK_EX | File::LOCK_NB) pid = IO::read(name) rescue nil pid ||= 'unknown' if @options['quiet'] exit EXIT_FAILURE else raise "process <#{ pid }> is already relaying from this queue" end else @pidfile.rewind @pidfile.sync = true @pidfile.print Process::pid @pidfile.truncate @pidfile.pos at_exit{ FileUtils::rm_f name rescue nil } end #--}}} end |
#gen_relayer_name(path) ⇒ Object
–}}}
151 152 153 154 155 156 |
# File 'lib/rq/relayer.rb', line 151 def gen_relayer_name path #--{{{ path = Util::realpath(path).gsub(%r|/|o, '_') File::join(Util::realpath('~'), ".#{ path }.relayer") #--}}} end |
#handle_signal ⇒ Object
–}}}
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/rq/relayer.rb', line 183 def handle_signal #--{{{ if $rq_sigterm or $rq_sigint reap_jobs(reap_only = true) until nothing_running? info{ "** STOPPING **" } @jrd.shutdown rescue nil @pidfile.posixlock File::LOCK_UN exit EXIT_SUCCESS end if $rq_sighup reap_jobs(reap_only = true) until nothing_running? info{ "** RESTARTING **" } @jrd.shutdown rescue nil Util::uncache __FILE__ @pidfile.posixlock File::LOCK_UN Util::exec @cmd end #--}}} end |
#install_signal_handlers ⇒ Object
–}}}
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/rq/relayer.rb', line 157 def install_signal_handlers #--{{{ if @daemon $rq_signaled = false $rq_sighup = false $rq_sigterm = false $rq_sigint = false trap('SIGHUP') do $rq_signaled = $rq_sighup = 'SIGHUP' warn{ "signal <SIGHUP>" } warn{ "finishing running jobs before handling signal" } end trap('SIGTERM') do $rq_signaled = $rq_sigterm = 'SIGTERM' warn{ "signal <SIGTERM>" } warn{ "finishing running jobs before handling signal" } end trap('SIGINT') do $rq_signaled = $rq_sigint = 'SIGINT' warn{ "signal <SIGINT>" } warn{ "finishing running jobs before handling signal" } end @jrd.install_signal_handlers end #--}}} end |
#reap ⇒ Object
–}}}
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/rq/relayer.rb', line 256 def reap #--{{{ debug{ "reaping finished/dead jobs" } sql = <<-sql select jid from jobs where or state='running' sql tuples = hdb.execute sql hjids = tuples.map{|t| t['jid']} unless jids.empty where_clauses = hjids.map{|hjid| "jid=#{ hjid }" } where_clause = where_clauses.join ' or ' sql = <<-sql select jid from jobs where state='finished' or state='dead' and (#{ where_clause }) sql end debug{ "reaped finished/dead jobs" } self #--}}} end |
#reap_and_sow ⇒ Object
TODO - this will need to map jids here to jids there
251 252 253 254 255 |
# File 'lib/rq/relayer.rb', line 251 def reap_and_sow #--{{{ transaction{ reap and sow } #--}}} end |
#relax ⇒ Object
–}}}
240 241 242 243 244 245 246 |
# File 'lib/rq/relayer.rb', line 240 def relax #--{{{ seconds = rand(@max_sleep - @min_sleep + 1) + @min_sleep debug{ "relaxing <#{ seconds }>" } sleep seconds #--}}} end |
#relay ⇒ Object
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 |
# File 'lib/rq/relayer.rb', line 27 def relay #--{{{ daemon do gen_pidfile @main.init_logging @logger = @main.logger set_q # # munge @q/@qpath to set there # @here = @q @qpath = realpath @main.argv.shift set_q @there = @q @q = @here @hdb = @here.qdb @tdb = @there.qdb @pid = Process::pid @cmd = @main.cmd @started = Util:: @min_sleep = Integer(@options['min_sleep'] || defval('min_sleep')) @max_sleep = Integer(@options['max_sleep'] || defval('max_sleep')) @relay = Integer(@options['number'] || defval('relay')) @transactions = {} install_signal_handlers info{ "** STARTED **" } info{ "version <#{ RQ::VERSION }>" } info{ "cmd <#{ @cmd }>" } info{ "pid <#{ @pid }>" } info{ "pidfile <#{ @pidfile.path }>" } info{ "here <#{ @here.path }>" } info{ "there <#{ @there.path }>" } debug{ "mode <#{ @mode }>" } debug{ "min_sleep <#{ @min_sleep }>" } debug{ "max_sleep <#{ @max_sleep }>" } debug{ "relay <#{ @relay }>" } exit loop do handle_signal if $rq_signaled throttle(@min_sleep) do reap_and_sow relax end end =begin loop do handle_signal if $rq_signaled throttle(@min_sleep) do start_jobs unless busy? if nothing_running? relax else reap_jobs end end end =end end #--}}} end |
#throttle(rate = @min_sleep) ⇒ Object
–}}}
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/rq/relayer.rb', line 203 def throttle rate = @min_sleep #--{{{ if Numeric === rate and rate > 0 if defined? @last_throttle_time and @last_throttle_time elapsed = Time.now - @last_throttle_time timeout = rate - elapsed if timeout > 0 timeout = timeout + rand(rate * 0.10) debug{ "throttle rate of <#{ rate }> exceeded - sleeping <#{ timeout }>" } sleep timeout end end @last_throttle_time = Time.now end yield #--}}} end |
#transaction ⇒ Object
–}}}
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/rq/relayer.rb', line 220 def transaction #--{{{ ret = nil if @in_transaction ret = yield else begin @in_transaction = true @here.transaction do @there.transaction do ret = yield end end ensure @in_transaction = false end end ret #--}}} end |