Class: Daemonic::Worker
- Inherits:
-
Object
- Object
- Daemonic::Worker
- Includes:
- Logging
- Defined in:
- lib/daemonic/worker.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
Instance Method Summary collapse
- #hup ⇒ Object
-
#initialize(options) ⇒ Worker
constructor
A new instance of Worker.
- #monitor ⇒ Object
- #restart ⇒ Object
- #running? ⇒ Boolean
- #start ⇒ Object
- #stop ⇒ Object
Methods included from Logging
Constructor Details
#initialize(options) ⇒ Worker
Returns a new instance of Worker.
7 8 9 10 |
# File 'lib/daemonic/worker.rb', line 7 def initialize() @index = .fetch(:index) @config = .fetch(:config) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
5 6 7 |
# File 'lib/daemonic/worker.rb', line 5 def config @config end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
5 6 7 |
# File 'lib/daemonic/worker.rb', line 5 def index @index end |
Instance Method Details
#hup ⇒ Object
42 43 44 |
# File 'lib/daemonic/worker.rb', line 42 def hup Process.kill("HUP", pid) end |
#monitor ⇒ Object
46 47 48 49 50 51 |
# File 'lib/daemonic/worker.rb', line 46 def monitor if not running? warn "#{to_s} Not running." start end end |
#restart ⇒ Object
36 37 38 39 40 |
# File 'lib/daemonic/worker.rb', line 36 def restart info "#{to_s} Restarting." stop start end |
#running? ⇒ Boolean
53 54 55 56 57 58 59 |
# File 'lib/daemonic/worker.rb', line 53 def running? if @pid !Process.waitpid(pid, Process::WNOHANG) rescue false else false end end |
#start ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/daemonic/worker.rb', line 12 def start @pid = Process.spawn( {"DAEMON_WORKER_NUMBER" => index.to_s}, *Array(config.command), {:chdir => config.working_dir} ) sleep 0.1 info "#{to_s} Starting." pidfile.write wait_for { running? } info "#{to_s} Started!" end |
#stop ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/daemonic/worker.rb', line 25 def stop Process.kill("TERM", pid) info "#{to_s} Stopping." Process.waitpid(pid) rescue Errno::ECHILD, Errno::ESRCH warn "#{to_s} Already stopped." ensure pidfile.clean @pid = nil end |