Class: Async::Background::Queue::SocketWaker

Inherits:
Object
  • Object
show all
Defined in:
lib/async/background/queue/socket_waker.rb

Constant Summary collapse

CLOSE_GRACE =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ SocketWaker

Returns a new instance of SocketWaker.



15
16
17
18
19
20
21
22
23
# File 'lib/async/background/queue/socket_waker.rb', line 15

def initialize(path)
  @path = path
  @server = nil
  @notification = Runtime::Notification.new
  @running = false
  @accept_task = nil
  @clients = Runtime::TaskGroup.new
  @sockets = {}
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/async/background/queue/socket_waker.rb', line 13

def path
  @path
end

Instance Method Details

#closeObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/async/background/queue/socket_waker.rb', line 48

def close
  return unless @running || @server

  @running = false
  stop_accept_loop
  @notification.signal_all
  hang_up_clients
  stop_clients

  @server&.close rescue nil
  @server = nil
  @accept_task = nil
  File.unlink(@path) rescue nil
end

#open!Object



25
26
27
28
29
30
31
32
33
# File 'lib/async/background/queue/socket_waker.rb', line 25

def open!
  ensure_directory
  cleanup_stale_socket
  @server = UNIXServer.new(@path)
  @running = true
  self
rescue Errno::EADDRINUSE
  raise "Socket #{@path} is already in use by another process"
end

#signalObject



43
44
45
46
# File 'lib/async/background/queue/socket_waker.rb', line 43

def signal
  @notification.signal_all
  true
end

#start_accept_loop(_parent_task = nil) ⇒ Object



35
36
37
# File 'lib/async/background/queue/socket_waker.rb', line 35

def start_accept_loop(_parent_task = nil)
  @accept_task = Runtime.spawn(name: 'socket-waker-accept') { accept_loop }
end

#wait(timeout: nil) ⇒ Object



39
40
41
# File 'lib/async/background/queue/socket_waker.rb', line 39

def wait(timeout: nil)
  @notification.wait(timeout)
end