Sha256: 02b8ee14c901c9d5e7edcb619f627c0c869a5c94c368a460f4b84c1a739a7392

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

module Vagrant
  module Notify
    module Middleware
      class StopServer
        def initialize(app, env)
          @app = app
        end

        def call(env)
          # TODO: Need to handle multi VMs setup

          if pid = server_is_running?(env)
            env[:ui].info('Stopping notification server...')
            stop_server(pid)
            cleanup_local_data(env)
          end

          @app.call(env)
        end

        private

        def stop_server(pid)
          Process.kill('KILL', pid.to_i) rescue nil
        end

        def cleanup_local_data(env)
          uuid = env[:vm].uuid.to_s
          local_data = env[:vm].env.local_data
          local_data['vagrant-notify'] ||= Vagrant::Util::HashWithIndifferentAccess.new
          local_data['vagrant-notify'].delete(uuid)
          local_data.commit
        end

        # REFACTOR: This is duplicated on Middleware::StartServer
        def server_is_running?(env)
          uuid = env[:vm].uuid.to_s
          begin
            pid = env[:vm].env.local_data.
              fetch('vagrant-notify', {}).
              fetch(uuid, {}).
              fetch('pid', nil)
            return false unless pid

            Process.getpgid(pid.to_i)
            pid
          rescue Errno::ESRCH
            false
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vagrant-notify-0.2.0 burn/middleware/stop_server.rb
vagrant-notify-0.1.0 lib/vagrant-notify/middleware/stop_server.rb