Class: Procodile::CLI

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

Instance Method Summary collapse

Constructor Details

#initialize(config, cli_options = {}) ⇒ CLI

Returns a new instance of CLI.



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

def initialize(config, cli_options = {})
  @config = config
  @cli_options = cli_options
end

Instance Method Details

#killObject



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/procodile/cli.rb', line 135

def kill
  Dir[File.join(@config.pid_root, '*.pid')].each do |pid_path|
    name = pid_path.split('/').last.gsub(/\.pid\z/, '')
    pid = File.read(pid_path).to_i
    begin
      ::Process.kill('KILL', pid)
      puts "Sent KILL to #{pid} (#{name})"
    rescue Errno::ESRCH
    end
    FileUtils.rm(pid_path)
  end
end

#reload_configObject



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

def reload_config
  if running?
    ControlClient.run(@config.sock_path, 'reload_config')
    puts "Reloading config for #{@config.app_name}"
  else
    raise Error, "#{@config.app_name} supervisor isn't running"
  end
end

#restartObject



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

def restart
  if running?
    options = {}
    instances = ControlClient.run(@config.sock_path, 'restart', :processes => process_names_from_cli_option)
    if instances.empty?
      puts "There are no processes to restart."
    else
      instances.each do |instance|
        puts "Restarting #{instance['description']} (PID: #{instance['pid']})"
      end
    end
  else
    raise Error, "#{@config.app_name} supervisor isn't running"
  end
end

#run(command) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/procodile/cli.rb', line 15

def run(command)
  if self.class.instance_methods(false).include?(command.to_sym) && command != 'run'
    public_send(command)
  else
    raise Error, "Invalid command '#{command}'"
  end
end

#startObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/procodile/cli.rb', line 23

def start
  if running?
    instances = ControlClient.run(@config.sock_path, 'start_processes', :processes => process_names_from_cli_option)
    if instances.empty?
      raise Error, "No processes were started. The type you entered might already be running or isn't defined."
    else
      instances.each do |instance|
        puts "Started #{instance['description']} (PID: #{instance['pid']})"
      end
      return
    end
  end

  processes = process_names_from_cli_option

  if @cli_options[:clean]
    FileUtils.rm_f(File.join(@config.pid_root, '*.pid'))
    FileUtils.rm_f(File.join(@config.pid_root, '*.sock'))
    puts "Removed all old pid & sock files"
  end

  if @cli_options[:foreground]
    File.open(pid_path, 'w') { |f| f.write(::Process.pid) }
    Supervisor.new(@config).start(:processes => processes)
  else
    FileUtils.rm_f(File.join(@config.pid_root, "*.pid"))
    pid = fork do
      STDOUT.reopen(log_path, 'a')
      STDOUT.sync = true
      STDERR.reopen(log_path, 'a')
      STDERR.sync = true
      Supervisor.new(@config).start(:processes => processes)
    end
    ::Process.detach(pid)
    File.open(pid_path, 'w') { |f| f.write(pid) }
    puts "Started #{@config.app_name} supervisor with PID #{pid}"
  end
end

#statusObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/procodile/cli.rb', line 112

def status
  if running?
    stats = ControlClient.run(@config.sock_path, 'status')
    stats['processes'].each_with_index do |process, index|
      puts unless index == 0
      puts "|| ".color(process['log_color']) + process['name'].color(process['log_color'])
      puts "||".color(process['log_color']) + " Quantity       " + process['quantity'].to_s
      puts "||".color(process['log_color']) + " Command        " + process['command']
      puts "||".color(process['log_color']) + " Respawning     " + "#{process['max_respawns']} every #{process['respawn_window']} seconds"
      puts "||".color(process['log_color']) + " Restart mode   " + process['restart_mode']
      stats['instances'][process['name']].each do |instance|
        print "|| ".color(process['log_color']) + instance['description'].to_s.ljust(20, ' ').color(process['log_color'])
        print "pid " + instance['pid'].to_s.ljust(12, ' ')
        print (instance['running'] ? 'Running' : 'Stopped').to_s.ljust(15, ' ')
        print instance['respawns'].to_s + " respawns"
        puts
      end
    end
  else
    puts "#{@config.app_name} supervisor not running"
  end
end

#stopObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/procodile/cli.rb', line 62

def stop
  if running?
    options = {}
    instances = ControlClient.run(@config.sock_path, 'stop', :processes => process_names_from_cli_option)
    if instances.empty?
      puts "There are no processes to stop."
    else
      instances.each do |instance|
        puts "Stopping #{instance['description']} (PID: #{instance['pid']})"
      end
    end
  else
    raise Error, "#{@config.app_name} supervisor isn't running"
  end
end

#stop_supervisorObject



94
95
96
97
98
99
100
101
# File 'lib/procodile/cli.rb', line 94

def stop_supervisor
  if running?
    ::Process.kill('TERM', current_pid)
    puts "Supervisor will be stopped in a moment."
  else
    raise Error, "#{@config.app_name} supervisor isn't running"
  end
end