Sha256: 8d4bd501483f16d79f517507188eee4049f5a87c325a9097e0224bf678c20c07

Contents?: true

Size: 1.47 KB

Versions: 26

Compression:

Stored size: 1.47 KB

Contents

# -*- encoding: utf-8 -*-
require 'socket'

module Bluepill
  module Socket
    TIMEOUT = 60 # Used for client commands
    MAX_ATTEMPTS = 5

    extend self

    def client(base_dir, name, &block)
      UNIXSocket.open(socket_path(base_dir, name), &block)
    end

    def client_command(base_dir, name, command)
      res = nil
      MAX_ATTEMPTS.times do |current_attempt|
        begin
          client(base_dir, name) do |socket|
            Timeout.timeout(TIMEOUT) do
              socket.puts command
              res = Marshal.load(socket.read)
            end
          end
          break
        rescue EOFError, Timeout::Error
          if current_attempt == MAX_ATTEMPTS - 1
            abort("Socket Timeout: Server may not be responding")
          end
          puts "Retry #{current_attempt + 1} of #{MAX_ATTEMPTS}"
        end
      end
      res
    end

    def server(base_dir, name)
      socket_path = self.socket_path(base_dir, name)
      begin
        UNIXServer.open(socket_path)
      rescue Errno::EADDRINUSE
        # if sock file has been created.  test to see if there is a server
        begin
          UNIXSocket.open(socket_path)
        rescue Errno::ECONNREFUSED
          File.delete(socket_path)
          return UNIXServer.open(socket_path)
        else
          logger.err("Server is already running!")
          exit(7)
        end
      end
    end

    def socket_path(base_dir, name)
      File.join(base_dir, 'socks', name + ".sock")
    end
  end
end

Version data entries

26 entries across 26 versions & 5 rubygems

Version Path
bluepill-rwgps-0.0.63 lib/bluepill/socket.rb
bluepill-rwgps-0.0.62 lib/bluepill/socket.rb
bluepill-rwgps-0.0.61 lib/bluepill/socket.rb
cloud66-bluepill-0.0.64 lib/bluepill/socket.rb
bluepill-0.0.68 lib/bluepill/socket.rb
bluepill-0.0.67 lib/bluepill/socket.rb
bluepill-rwgps-0.0.60 lib/bluepill/socket.rb
bluepill-0.0.66 lib/bluepill/socket.rb
bluepill-0.0.65 lib/bluepill/socket.rb
bluepill-0.0.64 lib/bluepill/socket.rb
cloud66-bluepill-0.0.63 lib/bluepill/socket.rb
bluepill-0.0.63 lib/bluepill/socket.rb
cloud66-bluepill-0.0.62 lib/bluepill/socket.rb
bluepill-0.0.62 lib/bluepill/socket.rb
bluepill-0.0.61 lib/bluepill/socket.rb
kostya-bluepill-0.0.60.3 lib/bluepill/socket.rb
skalar-bluepill-0.0.60.1 lib/bluepill/socket.rb
bluepill-0.0.60 lib/bluepill/socket.rb
bluepill-0.0.59 lib/bluepill/socket.rb
bluepill-0.0.58 lib/bluepill/socket.rb