Sha256: 19975a47444cfe7bf8c1d0a6f0994eeb84a3f4c4bc93482fac69eb67daa9a783

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

require 'socket'

module Guard
  class Yard
    class Server
      attr_accessor :pid, :port

      def initialize(port, stdout, stderr)
        @port = port || '8808'
        @stdout = stdout
        @stderr = stderr
      end

      def spawn
        self.pid = fork
        raise 'Fork failed' if pid == -1

        unless pid
          Signal.trap('QUIT', 'IGNORE')
          Signal.trap('INT', 'IGNORE')
          Signal.trap('TSTP', 'IGNORE')

          command = ["yard server -p #{port}"]
          command << "2> #{@stderr}" if @stderr
          command << "1> #{@stdout}" if @stdout
          exec command.join(' ')
        end
        pid
      end

      def kill
        Process.kill('KILL', pid) unless pid.nil?
        true
      end

      def verify
        5.times do
          sleep 1
          begin
            TCPSocket.new('localhost', port.to_i).close
          rescue Errno::ECONNREFUSED
            next
          end
          UI.info "[Guard::Yard] Server successfully started."
          return true
        end
        UI.error "[Guard::Yard] Error starting documentation server."
        Notifier.notify "[Guard::Yard] Server NOT started.",
          :title => 'yard', :image => :failed
        false
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
guard-yard-2.0.1 lib/guard/yard/server.rb
guard-yard-2.0.0 lib/guard/yard/server.rb