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



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/procodile/cli.rb', line 148

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



106
107
108
109
110
111
112
113
# File 'lib/procodile/cli.rb', line 106

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/procodile/cli.rb', line 81

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
61
62
63
# 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

  run_options = {}
  run_options[:brittle] = @cli_options[:brittle]

  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, run_options).start(:processes => processes)
  else
    FileUtils.rm_f(File.join(@config.pid_root, "*.pid"))
    pid = fork do
      STDOUT.reopen(@config.log_path, 'a')
      STDOUT.sync = true
      STDERR.reopen(@config.log_path, 'a')
      STDERR.sync = true
      Supervisor.new(@config, run_options).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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/procodile/cli.rb', line 115

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']
      puts "||".color(process['log_color']) + " Log path       " + (process['log_path'] || "none specified")
      instances = stats['instances'][process['name']]
      if instances.empty?
        puts "||".color(process['log_color']) + " No processes running."
      else
        instances.each do |instance|
          print "|| ".color(process['log_color']) + instance['description'].to_s.ljust(20, ' ').color(process['log_color'])
          if instance['running']
            print ' Running '.color("42;37")
          else
            print ' Stopped '.color("41;37")
          end
          print "     pid " + instance['pid'].to_s.ljust(12, ' ')
          print instance['respawns'].to_s + " respawns"
          puts
        end
      end
    end
  else
    puts "#{@config.app_name} supervisor not running"
  end
end

#stopObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/procodile/cli.rb', line 65

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



97
98
99
100
101
102
103
104
# File 'lib/procodile/cli.rb', line 97

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