Sha256: beadb7bb721fd985b3080d3378ab0e915e4cb28090897df33f25fb3cbadca8dd

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

require 'fileutils'
require 'spoon'
require 'guard/rack/command'
require 'guard/rack/custom_process'

module Guard
  class RackRunner
    attr_reader :options, :pid

    def initialize(options)
      @options = options
    end

    def start
      kill_unmanaged_pid! if options[:force_run]
      @pid = run_rack_command!
      true
    end

    def stop
      # Rely on kill_unmanaged_pid if there's no pid
      return true unless @pid

      exitstatus = kill(@pid)
      @pid = nil
      if exitstatus && exitstatus != 0
        UI.info "Rackup exited with non-zero exit status (#{exitstatus}) whilst trying to stop."
        return false
      end

      true
    end

    def restart
      stop && start
    end

    private

    def kill(pid, force = false)
      Guard::Rack::CustomProcess.new(options).kill pid, force
    end

    def run_rack_command!
      command = Guard::Rack::Command.new(options).build
      UI.debug("Running Rack with command: #{command}")
      spawn(*command)
    end

    def spawn(* args)
      Spoon.spawnp(* args)
    end

    def kill_unmanaged_pid!
      pid = unmanaged_pid
      kill(pid, true) if pid
    end

    def unmanaged_pid
      `lsof -n -i TCP:#{options[:port]}`.each_line do |line|
        return line.split("\s")[1].to_i if line["*:#{options[:port]} "]
      end
      nil
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
guard-rack-2.2.1 lib/guard/rack/runner.rb
guard-rack-2.2.0 lib/guard/rack/runner.rb