Class: Daemonic::Worker

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/daemonic/worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(options) ⇒ Worker

Returns a new instance of Worker.



7
8
9
10
# File 'lib/daemonic/worker.rb', line 7

def initialize(options)
  @index        = options.fetch(:index)
  @config       = options.fetch(:config)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/daemonic/worker.rb', line 5

def config
  @config
end

#indexObject (readonly)

Returns the value of attribute index.



5
6
7
# File 'lib/daemonic/worker.rb', line 5

def index
  @index
end

Instance Method Details

#hupObject



42
43
44
# File 'lib/daemonic/worker.rb', line 42

def hup
  Process.kill("HUP", pid)
end

#monitorObject



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

#restartObject



36
37
38
39
40
# File 'lib/daemonic/worker.rb', line 36

def restart
  info "#{to_s} Restarting."
  stop
  start
end

#running?Boolean

Returns:

  • (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

#startObject



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

#stopObject



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