Class: Procodile::CLI

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



33
34
35
# File 'lib/procodile/cli.rb', line 33

def initialize
  @options = {}
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



31
32
33
# File 'lib/procodile/cli.rb', line 31

def config
  @config
end

#optionsObject

Returns the value of attribute options.



30
31
32
# File 'lib/procodile/cli.rb', line 30

def options
  @options
end

Class Method Details

.command(name) ⇒ Object



24
25
26
27
28
# File 'lib/procodile/cli.rb', line 24

def self.command(name)
  commands[name] = {:name => name, :description => @description, :options => @options}
  @description = nil
  @options = nil
end

.commandsObject



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

def self.commands
  @commands ||= {}
end

.desc(description) ⇒ Object



16
17
18
# File 'lib/procodile/cli.rb', line 16

def self.desc(description)
  @description = description
end

.options(&block) ⇒ Object



20
21
22
# File 'lib/procodile/cli.rb', line 20

def self.options(&block)
  @options = block
end

.pid_active?(pid) ⇒ Boolean

Returns:

  • (Boolean)


450
451
452
453
454
# File 'lib/procodile/cli.rb', line 450

def self.pid_active?(pid)
  ::Process.getpgid(pid) ? true : false
rescue Errno::ESRCH
  false
end

.start_supervisor(config, options = {}, &after_start) ⇒ Object



474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/procodile/cli.rb', line 474

def self.start_supervisor(config, options = {}, &after_start)
  run_options = {}
  run_options[:respawn] = options[:respawn]
  run_options[:stop_when_none] = options[:stop_when_none]
  run_options[:proxy] = options[:proxy]
  run_options[:force_single_log] = options[:foreground]
  run_options[:port_allocations] = options[:port_allocations]

  tidy_pids(config)

  if options[:clean]
    FileUtils.rm_rf(Dir[File.join(config.pid_root, '*')])
    puts "Emptied PID directory"
  end

  if !Dir[File.join(config.pid_root, "*")].empty?
    raise Error, "The PID directory (#{config.pid_root}) is not empty. Cannot start unless things are clean."
  end

  $0="[procodile] #{config.app_name} (#{config.root})"
  if options[:foreground]
    File.open(config.supervisor_pid_path, 'w') { |f| f.write(::Process.pid) }
    Supervisor.new(config, run_options).start(&after_start)
  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(&after_start)
    end
    ::Process.detach(pid)
    File.open(config.supervisor_pid_path, 'w') { |f| f.write(pid) }
    puts "Started Procodile supervisor with PID #{pid}"
  end
end

.tidy_pids(config) ⇒ Object



512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/procodile/cli.rb', line 512

def self.tidy_pids(config)
  FileUtils.rm_f(config.supervisor_pid_path)
  FileUtils.rm_f(config.sock_path)
  pid_files = Dir[File.join(config.pid_root, '*.pid')]
  pid_files.each do |pid_path|
    file_name = pid_path.split('/').last
    pid = File.read(pid_path).to_i
    if self.pid_active?(pid)
      puts "Could not remove #{file_name} because process (#{pid}) was active"
    else
      FileUtils.rm_f(pid_path)
      puts "Removed #{file_name} because process was not active"
    end
  end
end

Instance Method Details

#dispatch(command) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/procodile/cli.rb', line 37

def dispatch(command)
  if self.class.commands.keys.include?(command.to_sym)
    public_send(command)
  else
    raise Error, "Invalid command '#{command}'"
  end
end