Class: Bindan::Emulator::GcsServerController
- Inherits:
-
Object
- Object
- Bindan::Emulator::GcsServerController
- Defined in:
- lib/bindan/emulators/gcs_server_controller.rb
Defined Under Namespace
Classes: ContainerCannotOpenError
Constant Summary collapse
- CONTAINER_NAME =
"gcs-server"- WAIT_TIMEOUT =
seconds
15- INITIAL_BACKOFF_KEY_SEC =
seconds
0.1
- BACKOFF_MULTIPLIER =
1.5
Class Method Summary collapse
- .running?(name:) ⇒ bool
- .start(folder:, port: 4443, name: CONTAINER_NAME, close_io: true) ⇒ GcsServerController
- .stop(name:) ⇒ void
Instance Method Summary collapse
-
#initialize(name:, folder:) ⇒ GcsServerController
constructor
A new instance of GcsServerController.
- #running? ⇒ bool
- #stop ⇒ Object
-
#wait_available(timeout: WAIT_TIMEOUT, backoff: INITIAL_BACKOFF_KEY_SEC, multiplier: BACKOFF_MULTIPLIER) ⇒ void
waits container to be running.
Constructor Details
#initialize(name:, folder:) ⇒ GcsServerController
Returns a new instance of GcsServerController.
69 70 71 72 |
# File 'lib/bindan/emulators/gcs_server_controller.rb', line 69 def initialize(name:, folder:) @container_name = name @folder = folder end |
Class Method Details
.running?(name:) ⇒ bool
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/bindan/emulators/gcs_server_controller.rb', line 42 def running?(name:) _stdin, stdout, stderr, = Open3.popen3("docker container list -f name=#{name}") begin # container list not only headers stdout.readlines.size > 1 rescue IOError => e warn "in #{self}" warn " " + stderr.read raise ContainerCannotOpenError.new e end end |
.start(folder:, port: 4443, name: CONTAINER_NAME, close_io: true) ⇒ GcsServerController
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/bindan/emulators/gcs_server_controller.rb', line 24 def start(folder:, port: 4443, name: CONTAINER_NAME, close_io: true) FileUtils.mkdir_p(folder) unless running?(name: name) opts = close_io ? {out: "/dev/null", err: "/dev/null"} : {} Process.spawn( # steep:ignore *"docker run --rm --name #{name} -p #{port}:4443 -v #{folder}:/data fsouza/fake-gcs-server -scheme http".split(" "), **opts ) end new(name: name, folder: folder) end |
.stop(name:) ⇒ void
This method returns an undefined value.
60 61 62 |
# File 'lib/bindan/emulators/gcs_server_controller.rb', line 60 def stop(name:) `docker container stop #{name}` end |
Instance Method Details
#running? ⇒ bool
85 86 87 |
# File 'lib/bindan/emulators/gcs_server_controller.rb', line 85 def running? self.class.running?(name: @container_name) end |
#stop ⇒ Object
77 78 79 |
# File 'lib/bindan/emulators/gcs_server_controller.rb', line 77 def stop self.class.stop(name: @container_name) end |
#wait_available(timeout: WAIT_TIMEOUT, backoff: INITIAL_BACKOFF_KEY_SEC, multiplier: BACKOFF_MULTIPLIER) ⇒ void
This method returns an undefined value.
waits container to be running
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/bindan/emulators/gcs_server_controller.rb', line 98 def wait_available(timeout: WAIT_TIMEOUT, backoff: INITIAL_BACKOFF_KEY_SEC, multiplier: BACKOFF_MULTIPLIER) last_rescued_error = nil count = 1 Timeout.timeout(timeout) do loop do if running? return end rescue ContainerCannotOpenError => e last_rescued_error = e sleep backoff count += 1 backoff *= multiplier**count end end rescue Timeout::Error raise last_rescued_error # steep:ignore end |