Class: Hotswap::SocketServer

Inherits:
Object
  • Object
show all
Defined in:
lib/hotswap/socket_server.rb

Constant Summary collapse

CONNECTION_TIMEOUT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket_path: Hotswap.socket_path, stderr_socket_path: Hotswap.stderr_socket_path) ⇒ SocketServer

Returns a new instance of SocketServer.



10
11
12
13
14
15
16
17
18
# File 'lib/hotswap/socket_server.rb', line 10

def initialize(socket_path: Hotswap.socket_path, stderr_socket_path: Hotswap.stderr_socket_path)
  @socket_path = socket_path
  @stderr_socket_path = stderr_socket_path
  @server = nil
  @stderr_server = nil
  @thread = nil
  @stderr_client = nil
  @stderr_mutex = Mutex.new
end

Instance Attribute Details

#socket_pathObject (readonly)

Returns the value of attribute socket_path.



6
7
8
# File 'lib/hotswap/socket_server.rb', line 6

def socket_path
  @socket_path
end

#stderr_socket_pathObject (readonly)

Returns the value of attribute stderr_socket_path.



6
7
8
# File 'lib/hotswap/socket_server.rb', line 6

def stderr_socket_path
  @stderr_socket_path
end

Instance Method Details

#startObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hotswap/socket_server.rb', line 20

def start
  cleanup_stale_socket(@socket_path)
  cleanup_stale_socket(@stderr_socket_path)

  FileUtils.mkdir_p(File.dirname(@socket_path))
  @server = UNIXServer.new(@socket_path)
  @stderr_server = UNIXServer.new(@stderr_socket_path)

  @thread = Thread.new { accept_loop }
  @thread.report_on_exception = false

  logger.info "listening on #{@socket_path}"
  logger.info "stderr socket on #{@stderr_socket_path}"
  logger.info "managing #{Hotswap.databases.size} database(s): #{Hotswap.databases.map(&:path).join(', ')}"
  self
end

#stopObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/hotswap/socket_server.rb', line 37

def stop
  logger.info "shutting down"
  @server&.close
  @stderr_server&.close
  @thread&.kill
  @stderr_mutex.synchronize do
    @stderr_client&.close rescue nil
    @stderr_client = nil
  end
  [@socket_path, @stderr_socket_path].each do |path|
    File.delete(path) if path && File.exist?(path)
  end
end