Class: Thin::Command

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/thin/command.rb

Overview

Run a command through the thin command-line script.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#debug, debug, debug?, #log, log, #log_error, log_error, #silent, #silent=, silent?, #trace, trace, trace?

Constructor Details

#initialize(name, options = {}) ⇒ Command

Returns a new instance of Command.



14
15
16
17
# File 'lib/thin/command.rb', line 14

def initialize(name, options={})
  @name    = name
  @options = options
end

Class Attribute Details

.scriptObject

Path to the thin script used to control the servers. Leave this to default to use the one in the path.



11
12
13
# File 'lib/thin/command.rb', line 11

def script
  @script
end

Class Method Details

.run(*args) ⇒ Object



19
20
21
# File 'lib/thin/command.rb', line 19

def self.run(*args)
  new(*args).run
end

Instance Method Details

#runObject

Send the command to the thin script



24
25
26
27
28
29
30
31
32
# File 'lib/thin/command.rb', line 24

def run
  shell_cmd = shellify
  trace shell_cmd
  trap('INT') {} # Ignore INT signal to pass CTRL+C to subprocess
  Open3.popen3(shell_cmd) do |stdin, stdout, stderr|
    log stdout.gets until stdout.eof?
    log stderr.gets until stderr.eof?
  end
end

#shellifyObject

Turn into a runnable shell command

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/thin/command.rb', line 35

def shellify
  shellified_options = @options.inject([]) do |args, (name, value)|
    option_name = name.to_s.tr("_", "-")
    case value
    when NilClass,
         TrueClass then args << "--#{option_name}"
    when FalseClass
    when Array     then value.each { |v| args << "--#{option_name}=#{v.inspect}" }
    else                args << "--#{option_name}=#{value.inspect}"
    end
    args
  end
  
  raise ArgumentError, "Path to thin script can't be found, set Command.script" unless self.class.script
  
  "#{self.class.script} #{@name} #{shellified_options.compact.join(' ')}"
end