Class: MKIt::ShellClient

Inherits:
Object
  • Object
show all
Defined in:
lib/mkit/cmd/shell_client.rb

Direct Known Subclasses

DockerExecCommand, DockerLogListener

Instance Method Summary collapse

Constructor Details

#initialize(command:) ⇒ ShellClient

Returns a new instance of ShellClient.



6
7
8
9
10
# File 'lib/mkit/cmd/shell_client.rb', line 6

def initialize(command:)
  @command = command
  @logger = MKItLogger
  @logger.debug("Command initialized: [#{@command}]")
end

Instance Method Details

#closeObject



24
25
26
# File 'lib/mkit/cmd/shell_client.rb', line 24

def close
  # no op
end

#registerObject



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
# File 'lib/mkit/cmd/shell_client.rb', line 28

def register
  @client = Thread.new {
    begin
      PTY.spawn( @command ) do |stdout, stdin, pid |
        begin
          yield stdout, stdin, pid if block_given?
        rescue Errno::EIO
          @logger.warn(
            "Errno:EIO error, but this probably just means that the process has finished giving output"
          )
        rescue ExitShell
          @logger.info("#{@command} ended")
        ensure
          stdin.close rescue @logger.warn("Failed to close stdin")
          stdout.close rescue @logger.warn("Failed to close stdout")
          begin
            close
          rescue => e
            @logger.warn("Error closing client", e)
          end
          Process.wait(pid)
        end
      end
    rescue PTY::ChildExited
      @logger.info("#{@command} exited.")
    end
  }
end

#unregisterObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/mkit/cmd/shell_client.rb', line 12

def unregister
  @logger.info("ending [#{@command}]...")
  if @client
    begin
      @client.raise ExitShell.new
    rescue
      @logger.error("Failed to raise ExitShell")
    end
    @client.exit rescue @logger.warn("Failed to exit client thread")
  end
end