Class: Daemonizer::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/daemonizer/cli.rb

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



10
11
12
13
# File 'lib/daemonizer/cli.rb', line 10

def initialize(*)
  super
  Daemonizer.daemonfile = options[:daemonfile] || "Daemonfile"
end

Instance Method Details

#debug(pool_name = nil) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/daemonizer/cli.rb', line 87

def debug(pool_name = nil)
  if pool_name.nil?
    puts "You should supply pool_name to debug"
    exit 1
  end
  control_pools_loop(pool_name, "execution ended", true) do |pool|
    STDOUT.sync = true

    engine = Engine.new(pool)
    engine.debug!

    print_pool pool.name,  " Done!"
    exit(0)
  end
  return true
end

#listObject



70
71
72
73
74
75
76
77
78
# File 'lib/daemonizer/cli.rb', line 70

def list
  puts "List of configured pools:"
  puts ""
  Daemonizer.find_pools(nil).each do |pool|
    puts "  * #{pool.name}"
  end
  puts ""
  return true
end

#restart(pool_name = nil) ⇒ Object



81
82
83
84
# File 'lib/daemonizer/cli.rb', line 81

def restart(pool_name = nil)
  invoke :stop, pool_name
  invoke :start, pool_name
end

#start(pool_name = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/daemonizer/cli.rb', line 16

def start(pool_name = nil)
  control_pools_loop(pool_name, "successfully started") do |pool|
    # Pid file check
    if Daemonize.check_pid(pool.pid_file)
      print_pool pool.name,  "Can't start, another process exists!"
      exit(1)
    end

    print_pool pool.name,  "Starting pool"

    app_name = "#{pool.name} monitor\0"

    Daemonize.daemonize(app_name)

    Dir.chdir(Daemonizer.root) # Make sure we're in the working directory

    # Pid file creation
    Daemonize.create_pid(pool.pid_file)

    # Workers processing
    engine = Engine.new(pool)
    engine.start!

    # Workers exited, cleaning up
    File.delete(pool.pid_file) rescue nil
  end
  return true
end

#stats(pool_name = nil) ⇒ Object



105
106
107
108
109
110
# File 'lib/daemonizer/cli.rb', line 105

def stats(pool_name = nil)
  Daemonizer.find_pools(pool_name).each do |pool|
    statistics = Daemonizer::Stats::MemoryStats.new(pool)
    statistics.print
  end
end

#stop(pool_name = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/daemonizer/cli.rb', line 46

def stop(pool_name = nil)
  control_pools_loop(pool_name, "successfully stoped") do |pool|
    STDOUT.sync = true
    unless Daemonize.check_pid(pool.pid_file)
      print_pool pool.name, "No pid file or a stale pid file!"
      exit 1
    end

    pid = Daemonize.read_pid(pool.pid_file)
    print_pool pool.name,  "Killing the process: #{pid}: "

    loop do
      Process.kill('SIGTERM', pid)
      sleep(1)
      break unless Daemonize.check_pid(pool.pid_file)
    end

    print_pool pool.name,  " Done!"
    exit(0)
  end
  return true
end