Class: DockerSync::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/docker-sync/command.rb

Overview

based on ‘Backticks::Command` from `Backticks` gem

Constant Summary collapse

FOREVER =
86_400 * 365
CHUNK =
1_024

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pid, stdin, stdout, stderr) ⇒ Command

Returns a new instance of Command.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/docker-sync/command.rb', line 49

def initialize(pid, stdin, stdout, stderr)
  @pid = pid
  @stdin = stdin
  @stdout = stdout
  @stderr = stderr
  @status = nil

  @captured_input  = String.new(encoding: Encoding::BINARY)
  @captured_output = String.new(encoding: Encoding::BINARY)
  @captured_error  = String.new(encoding: Encoding::BINARY)
end

Instance Attribute Details

#captured_errorString (readonly)

Returns all output to stderr that has been captured so far.

Returns:

  • (String)

    all output to stderr that has been captured so far



26
27
28
# File 'lib/docker-sync/command.rb', line 26

def captured_error
  @captured_error
end

#captured_inputString (readonly)

Returns all input that has been captured so far.

Returns:

  • (String)

    all input that has been captured so far



20
21
22
# File 'lib/docker-sync/command.rb', line 20

def captured_input
  @captured_input
end

#captured_outputString (readonly)

Returns all output that has been captured so far.

Returns:

  • (String)

    all output that has been captured so far



23
24
25
# File 'lib/docker-sync/command.rb', line 23

def captured_output
  @captured_output
end

#pidInteger (readonly)

Returns child process ID.

Returns:

  • (Integer)

    child process ID



14
15
16
# File 'lib/docker-sync/command.rb', line 14

def pid
  @pid
end

#statusnil, Process::Status (readonly)

Returns result of command if it has ended; nil if still running.

Returns:

  • (nil, Process::Status)

    result of command if it has ended; nil if still running



17
18
19
# File 'lib/docker-sync/command.rb', line 17

def status
  @status
end

Class Method Details

.run(*argv, dir: nil) ⇒ Object

Run a command. The parameters are same as ‘Kernel#spawn`.

Usage:

run('docker-compose', '--file=joe.yml', 'up', '-d', 'mysvc')


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/docker-sync/command.rb', line 32

def self.run(*argv, dir: nil)
  nopty = !defined?(PTY)

  stdin_r, stdin = nopty ? IO.pipe : PTY.open
  stdout, stdout_w = nopty ? IO.pipe : PTY.open
  stderr, stderr_w = IO.pipe

  chdir = dir || Dir.pwd
  pid = spawn(*argv, in: stdin_r, out: stdout_w, err: stderr_w, chdir: chdir)

  stdin_r.close
  stdout_w.close
  stderr_w.close

  self.new(pid, stdin, stdout, stderr)
end

Instance Method Details

#join(limit = FOREVER) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/docker-sync/command.rb', line 65

def join(limit = FOREVER)
  return self if @status

  tf = Time.now + limit
  until (t = Time.now) >= tf
    capture(tf - t)
    res = Process.waitpid(@pid, Process::WNOHANG)
    if res
      @status = $?
      return self
    end
  end

  nil
end

#success?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/docker-sync/command.rb', line 61

def success?
  status.success?
end