Skip to content

Commit

Permalink
Merge pull request #737 from SwagDevOps/fix/docker_finalize_container
Browse files Browse the repository at this point in the history
Fix warning related to finalizer in docker backend
  • Loading branch information
mizzy authored Nov 21, 2022
2 parents ded4e3f + 16b046c commit 1c1a54d
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions lib/specinfra/backend/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,33 @@ def initialize(config = {})
@base_image = get_or_pull_image(image)

create_and_start_container
ObjectSpace.define_finalizer(self, proc { cleanup_container })
ObjectSpace.define_finalizer(self, self.class.__send__(:finalizer_for, @container))
elsif container = get_config(:docker_container)
@container = ::Docker::Container.get(container)
else
fail 'Please specify docker_image or docker_container.'
end
end

class << self
protected

# Get a finalizer for given container.
#
# @param [::Docker::Container, nil] container
#
# @return [Proc]
def finalizer_for(container)
proc do
# noinspection RubyNilAnalysis
unless container.nil?
container.stop
container.delete
end
end
end
end

def run_command(cmd, opts={})
cmd = build_command(cmd)
run_pre_command(opts)
Expand Down Expand Up @@ -77,8 +96,7 @@ def create_and_start_container
end

def cleanup_container
@container.stop
@container.delete
self.class.__send__(:finalizer_for, @container).call
end

def current_image
Expand Down

0 comments on commit 1c1a54d

Please sign in to comment.