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.



8
9
10
# File 'lib/daemonizer/cli.rb', line 8

def initialize(*)
  super
end

Instance Method Details

#debug(pool_name = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/daemonizer/cli.rb', line 77

def debug(pool_name = nil)
  puts "You should supply pool_name to debug" if pool_name.nil?
  control_pools_loop(pool_name, "execution ended", options[:demfile]) do |pool|
    STDOUT.sync = true
    print_pool pool.name,  "Debugging pool: "
    
    engine = Engine.new(pool)
    engine.debug!
    
    print_pool pool.name,  " Done!"
    exit(0)
  end
  return true    
end

#restart(pool_name = nil) ⇒ Object



70
71
72
73
# File 'lib/daemonizer/cli.rb', line 70

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

#start(pool_name = nil) ⇒ Object



14
15
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
# File 'lib/daemonizer/cli.rb', line 14

def start(pool_name = nil)
  control_pools_loop(pool_name, "successfully started", options[:demfile]) 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

#stop(pool_name = nil) ⇒ Object



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

def stop(pool_name = nil)
  control_pools_loop(pool_name, "successfully stoped", options[:demfile]) 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