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

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



470
471
472
473
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
# File 'lib/procodile/cli.rb', line 470

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]

  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

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