Class: Bluepill::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/bluepill/controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Controller

Returns a new instance of Controller.



7
8
9
10
11
12
13
14
15
# File 'lib/bluepill/controller.rb', line 7

def initialize(options = {})
  self.log_file = options[:log_file]
  self.base_dir = options[:base_dir]
  self.sockets_dir = File.join(base_dir, 'socks')
  self.pids_dir = File.join(base_dir, 'pids')
  
  setup_dir_structure
  cleanup_bluepill_directory
end

Instance Attribute Details

#base_dirObject

Returns the value of attribute base_dir.



5
6
7
# File 'lib/bluepill/controller.rb', line 5

def base_dir
  @base_dir
end

#log_fileObject

Returns the value of attribute log_file.



5
6
7
# File 'lib/bluepill/controller.rb', line 5

def log_file
  @log_file
end

#pids_dirObject

Returns the value of attribute pids_dir.



5
6
7
# File 'lib/bluepill/controller.rb', line 5

def pids_dir
  @pids_dir
end

#sockets_dirObject

Returns the value of attribute sockets_dir.



5
6
7
# File 'lib/bluepill/controller.rb', line 5

def sockets_dir
  @sockets_dir
end

Instance Method Details

#grep_pattern(application, query = nil) ⇒ Object



90
91
92
93
# File 'lib/bluepill/controller.rb', line 90

def grep_pattern(application, query = nil)
  pattern = [application, query].compact.join(':')
  ['\[.*', Regexp.escape(pattern), '.*'].compact.join
end

#handle_command(application, command, *args) ⇒ Object



21
22
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
# File 'lib/bluepill/controller.rb', line 21

def handle_command(application, command, *args)
  case command.to_sym
  when :status
    puts self.send_to_daemon(application, :status, *args)
  when *Application::PROCESS_COMMANDS
    # these need to be sent to the daemon and the results printed out
    affected = self.send_to_daemon(application, command, *args)
    if affected.empty?
      puts "No processes effected"
    else
      puts "Sent #{command} to:"
      affected.each do |process|
        puts "  #{process}"
      end
    end
  when :quit
    pid = pid_for(application)
    if System.pid_alive?(pid)
      ::Process.kill("TERM", pid)
      puts "Killing bluepilld[#{pid}]"
    else
      puts "bluepilld[#{pid}] not running"
    end
  when :log
    log_file_location = self.send_to_daemon(application, :log_file)
    log_file_location = self.log_file if log_file_location.to_s.strip.empty?
    
    requested_pattern = args.first
    grep_pattern = self.grep_pattern(application, requested_pattern)
    
    tail = "tail -n 100 -f #{log_file_location} | grep -E '#{grep_pattern}'"
    puts "Tailing log for #{requested_pattern}..."
    Kernel.exec(tail)
  else
    $stderr.puts "Unknown command `%s` (or application `%s` has not been loaded yet)" % [command, command]
  end
end

#running_applicationsObject



17
18
19
# File 'lib/bluepill/controller.rb', line 17

def running_applications
  Dir[File.join(sockets_dir, "*.sock")].map{|x| File.basename(x, ".sock")}
end

#send_to_daemon(application, command, *args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bluepill/controller.rb', line 59

def send_to_daemon(application, command, *args)
  begin
    Timeout::timeout(Socket::TIMEOUT) do
      verify_version!(application)
      buffer = ""
      socket = Socket.client(base_dir, application) # Something that should be interrupted if it takes too much time...
      socket.puts(([command] + args).join(":"))
      while line = socket.gets
        buffer << line
      end
      if buffer.size > 0
        response = Marshal.load(buffer)
        if response.is_a?(Exception)
          $stderr.puts "Received error from server:"
          $stderr.puts response.inspect
          $stderr.puts response.backtrace.join("\n")
          exit(8)
        else
          response
        end
      else
        abort("No response from server")
      end
    end
  rescue Timeout::Error
    abort("Socket Timeout: Server may not be responding")
  rescue Errno::ECONNREFUSED
    abort("Connection Refused: Server is not running")
  end
end