Class: LocalDevelopmentGateway::Client

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

Constant Summary collapse

CONTAINER_FORMAT =
"{{.ID}}\t{{.Label \"com.docker.compose.project\"}}\t{{.Label \"com.docker.compose.service\"}}\t{{.Label \"local-gateway\"}}"

Instance Method Summary collapse

Constructor Details

#initialize(runner: Docker.new, timeout: 30, poll_interval: 0.25, sleeper: ->(seconds) { sleep seconds }, clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }) ⇒ Client

Returns a new instance of Client.



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/local_development_gateway.rb', line 50

def initialize(
  runner: Docker.new,
  timeout: 30,
  poll_interval: 0.25,
  sleeper: ->(seconds) { sleep seconds },
  clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
)
  @runner = runner
  @timeout = timeout
  @poll_interval = poll_interval
  @sleeper = sleeper
  @clock = clock
end

Instance Method Details

#compose(*args, capture: true) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/local_development_gateway.rb', line 111

def compose(*args, capture: true)
  @runner.call(
    "compose",
    "--project-name",
    PROJECT_NAME,
    "--file",
    COMPOSE_FILE,
    *args,
    capture: capture
  )
end

#ensure_running(*compose_args) ⇒ Object Also known as: start



64
65
66
67
68
69
70
# File 'lib/local_development_gateway.rb', line 64

def ensure_running(*compose_args)
  return :reused if compose_args.empty? && ready?

  compose("up", "-d", "--remove-orphans", *compose_args)
  wait_until_ready
  :started
end

#logs(*args, follow: false) ⇒ Object



85
86
87
# File 'lib/local_development_gateway.rb', line 85

def logs(*args, follow: false)
  compose("logs", *(follow ? ["--follow"] : []), *args, capture: false)
end

#ready?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
# File 'lib/local_development_gateway.rb', line 74

def ready?
  network_exists? &&
    gateway_container_ids(status: "running").any? do |id|
      healthy_container?(id)
    end
end

#statusObject



81
82
83
# File 'lib/local_development_gateway.rb', line 81

def status
  compose("ps", "--all")
end

#stop_if_unusedObject



89
90
91
92
93
94
95
# File 'lib/local_development_gateway.rb', line 89

def stop_if_unused
  return :not_running unless gateway_exists?
  return :in_use if non_gateway_containers_attached?

  compose("down", "--remove-orphans")
  :stopped
end

#with_running(ensure_running: true) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/local_development_gateway.rb', line 97

def with_running(ensure_running: true)
  begin
    self.ensure_running if ensure_running
    yield
  ensure
    block_error = $!
    begin
      stop_if_unused
    rescue StandardError => cleanup_error
      raise cleanup_error unless block_error
    end
  end
end