Class: Lipsiadmin::Loops::Worker
- Inherits:
-
Object
- Object
- Lipsiadmin::Loops::Worker
- Defined in:
- lib/loops/worker.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
Instance Method Summary collapse
-
#initialize(name, logger, engine, &blk) ⇒ Worker
constructor
A new instance of Worker.
- #run ⇒ Object
- #running?(verbose = false) ⇒ Boolean
- #shutdown? ⇒ Boolean
- #stop(force = false) ⇒ Object
Constructor Details
#initialize(name, logger, engine, &blk) ⇒ Worker
Returns a new instance of Worker.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/loops/worker.rb', line 8 def initialize(name, logger, engine, &blk) raise "Need a worker block!" unless block_given? @name = name @logger = logger @engine = engine @worker_block = blk @shutdown = false end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
4 5 6 |
# File 'lib/loops/worker.rb', line 4 def logger @logger end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/loops/worker.rb', line 5 def name @name end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
6 7 8 |
# File 'lib/loops/worker.rb', line 6 def pid @pid end |
Instance Method Details
#run ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/loops/worker.rb', line 23 def run return if shutdown? if @engine == 'fork' @pid = Kernel.fork do $0 = "loop worker: #{@name}\0" @pid = Process.pid @worker_block.call exit(0) end elsif @engine == 'thread' @thread = Thread.start do @worker_block.call end else raise "Invalid engine name: #{@engine}" end rescue Exception => e logger.error("Exception from worker: #{e} at #{e.backtrace.first}") end |
#running?(verbose = false) ⇒ Boolean
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/loops/worker.rb', line 43 def running?(verbose = false) return false if shutdown? if @engine == 'fork' return false unless @pid begin Process.waitpid(@pid, Process::WNOHANG) res = Process.kill(0, @pid) logger.debug("KILL(#{@pid}) = #{res}") if verbose return true rescue Errno::ESRCH, Errno::ECHILD, Errno::EPERM => e logger.error("Exception from kill: #{e} at #{e.backtrace.first}") if verbose return false end elsif @engine == 'thread' @thread && @thread.alive? else raise "Invalid engine name: #{@engine}" end end |
#shutdown? ⇒ Boolean
19 20 21 |
# File 'lib/loops/worker.rb', line 19 def shutdown? @shutdown end |
#stop(force = false) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/loops/worker.rb', line 63 def stop(force = false) @shutdown = true if @engine == 'fork' begin sig = force ? 'SIGKILL' : 'SIGTERM' logger.debug("Sending #{sig} to ##{@pid}") Process.kill(sig, @pid) rescue Errno::ESRCH, Errno::ECHILD, Errno::EPERM=> e logger.error("Exception from kill: #{e} at #{e.backtrace.first}") end elsif @engine == 'thread' force && !defined?(::JRuby) ? @thread.kill! : @thread.kill else raise "Invalid engine name: #{@engine}" end end |