feat: add option for multiple instances
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Glenn Y. Rolland 2024-02-14 15:29:36 +01:00
parent a007211e29
commit 9d13f8216f
3 changed files with 12 additions and 4 deletions

View file

@ -28,6 +28,10 @@ module DocMachine::Build
config.port = port.to_i
end
opts.on("-m", "--multiple", "Allow multiple instances per dir" ) do |port|
config.multiple_instances = true
end
opts.on("-t", "--tty", "Enable TTY mode (needed for shell)") do
config.enable_tty = true
end

View file

@ -5,6 +5,7 @@ module DocMachine::Build
property action : String = "watch"
property enable_tty : Bool = false
property port : Int32 = 5100
property multiple_instances : Bool = false
def initialize(@parent : DocMachine::Config)
end

View file

@ -11,7 +11,8 @@ module DocMachine::Build
Log = DocMachine::Build::Log.for("run")
def initialize(@config : DocMachine::Build::Config)
@basehash = Digest::SHA256.hexdigest(@config.data_dir)[0..6]
data = "#{@config.data_dir}:#{@config.port}"
@basehash = Digest::SHA256.hexdigest(data)[0..6]
@docker_name = "docmachine-#{@basehash}"
@docker_image = "glenux/docmachine:latest"
@docker_opts = [] of String
@ -27,8 +28,8 @@ module DocMachine::Build
Log.info { "docker_image = #{@docker_image}" }
Log.info { "action = #{@config.action}" }
self._avoid_duplicates()
self._pull_image()
self._avoid_duplicates() unless @config.multiple_instances
end
private def _find_port(port_base)
@ -48,13 +49,15 @@ module DocMachine::Build
end
private def _avoid_duplicates
Log.info { "Multiple Instances: stopping duplicate containers (for #{@docker_name})" }
docker_cid = %x{docker ps -f "name=#{@docker_name}" -q}.strip
Log.info { "docker_name: #{@docker_name}" }
Log.info { "docker_cid: #{docker_cid}" }
Log.info { "Multiple Instances: docker_name: #{@docker_name}" }
Log.info { "Multiple Instances: docker_cid: #{docker_cid || "-"}" }
if !docker_cid.empty?
Process.run("docker", ["kill", @docker_name])
Process.run("docker", ["rm", @docker_name])
end
end