Class: Zeusd::Daemon

Inherits:
Object
  • Object
show all
Includes:
DaemonTracker
Defined in:
lib/zeusd/daemon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DaemonTracker

included, #log_file, #logger, #track

Constructor Details

#initialize(options = {}) ⇒ Daemon

Returns a new instance of Daemon.



5
6
7
8
# File 'lib/zeusd/daemon.rb', line 5

def initialize(options = {})
  @cwd     = Pathname.new(options[:cwd] || Dir.pwd).realpath
  @verbose = !!options[:verbose]
end

Instance Attribute Details

#child_processObject (readonly)

Returns the value of attribute child_process.



3
4
5
# File 'lib/zeusd/daemon.rb', line 3

def child_process
  @child_process
end

#cwdObject (readonly)

Returns the value of attribute cwd.



3
4
5
# File 'lib/zeusd/daemon.rb', line 3

def cwd
  @cwd
end

#statusObject (readonly)

Returns the value of attribute status.



3
4
5
# File 'lib/zeusd/daemon.rb', line 3

def status
  @status
end

#verboseObject (readonly)

Returns the value of attribute verbose.



3
4
5
# File 'lib/zeusd/daemon.rb', line 3

def verbose
  @verbose
end

Instance Method Details

#finished?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/zeusd/daemon.rb', line 55

def finished?
  status.finished?
end

#processObject



51
52
53
# File 'lib/zeusd/daemon.rb', line 51

def process
  @process ||= Process.all.find {|p| !!p.command[/zeus.*start$/] && p.cwd == cwd }
end

#restart!(options = {}) ⇒ Object



22
23
24
# File 'lib/zeusd/daemon.rb', line 22

def restart!(options = {})
  stop!.start!(options)
end

#start!(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/zeusd/daemon.rb', line 10

def start!(options = {})
  start_child_process!

  @process = Zeusd::Process.find(child_process.pid)

  if options.fetch(:block, false)
    sleep(3) until status.finished?
  end

  self
end

#status_queueObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/zeusd/daemon.rb', line 40

def status_queue
  queue  = Queue.new
  status = Log::Status.new(File.new(zeus_log.to_path, 'r'))

  queue << status.to_cli
  status.on_update {|x| queue << x.to_cli }
  status.record!

  queue
end

#stop!Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/zeusd/daemon.rb', line 26

def stop!
  return self unless process

  # Kill process tree and wait for exits
  process.kill!(:recursive => true, :signal => "KILL", :wait => true)

  # Clean up socket file if stil exists
  (zeus_socket.delete rescue nil) if zeus_socket.exist?

  @process = nil

  self
end

#to_json(*args) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/zeusd/daemon.rb', line 73

def to_json(*args)
  {
    :class   => self.class.name,
    :cwd     => cwd.to_path,
    :verbose => verbose,
    :process => process
  }.to_json(*args)
end

#verbose?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/zeusd/daemon.rb', line 69

def verbose?
  !!verbose
end

#zeus_logObject



63
64
65
66
67
# File 'lib/zeusd/daemon.rb', line 63

def zeus_log
  cwd.join('log', 'zeus.log').tap do |path|
    FileUtils.touch(path.to_path)
  end
end

#zeus_socketObject



59
60
61
# File 'lib/zeusd/daemon.rb', line 59

def zeus_socket
  cwd.join('.zeus.sock')
end