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.



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/local_development_gateway.rb', line 53

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



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/local_development_gateway.rb', line 117

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



67
68
69
70
71
72
73
# File 'lib/local_development_gateway.rb', line 67

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



91
92
93
# File 'lib/local_development_gateway.rb', line 91

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

#ready?Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
# File 'lib/local_development_gateway.rb', line 77

def ready?
  network_exists? && obsolete_services_absent? &&
    SERVICE_NAMES.all? do |service|
      gateway_container_ids(
        service: service,
        status: "running"
      ).any? { |id| healthy_container?(id) }
    end
end

#statusObject



87
88
89
# File 'lib/local_development_gateway.rb', line 87

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

#stop_if_unusedObject



95
96
97
98
99
100
101
# File 'lib/local_development_gateway.rb', line 95

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



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/local_development_gateway.rb', line 103

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