Class: QB::IPC::STDIO::Server::Service
- Inherits:
-
Object
- Object
- QB::IPC::STDIO::Server::Service
- Includes:
- NRSER::Log::Mixin
- Defined in:
- lib/qb/ipc/stdio/server/service.rb
Overview
STDIO as a service exposed on a UNIX socket so that modules can stream
their output to it, which is in turn printed to the console qb
is running
in.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#env_var_name ⇒ String
readonly
TODO document
env_var_name
attribute. -
#name ⇒ Synbol
readonly
The service's name, like
:in
,:out
,:err
. -
#path ⇒ Pathname
readonly
Absolute path to socket file.
-
#server ⇒ UNIXServer?
readonly
The UNIX socket server.
-
#socket ⇒ UNIXSocket
readonly
The socket we accept from the server.
-
#thread ⇒ attr_type
readonly
TODO document
thread
attribute.
Instance Method Summary collapse
-
#close! ⇒ nil
We're done here, clean up!.
-
#initialize(name:, socket_dir:) ⇒ Service
constructor
Construct an IO service.
- #open! ⇒ Object
-
#to_s ⇒ String
A short string describing the instance.
Constructor Details
#initialize(name:, socket_dir:) ⇒ Service
Construct an IO service.
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/qb/ipc/stdio/server/service.rb', line 93 def initialize name:, socket_dir: @name = name @thread = nil @server = nil @socket = nil @env_var_name = QB::IPC::STDIO.path_env_var_name name @path = socket_dir.join "#{ name }.sock" self.logger = create_logger logger.debug "Initialized" end |
Instance Attribute Details
#env_var_name ⇒ String (readonly)
TODO document env_var_name
attribute.
66 67 68 |
# File 'lib/qb/ipc/stdio/server/service.rb', line 66 def env_var_name @env_var_name end |
#name ⇒ Synbol (readonly)
The service's name, like :in
, :out
, :err
.
45 46 47 |
# File 'lib/qb/ipc/stdio/server/service.rb', line 45 def name @name end |
#path ⇒ Pathname (readonly)
Absolute path to socket file.
52 53 54 |
# File 'lib/qb/ipc/stdio/server/service.rb', line 52 def path @path end |
#server ⇒ UNIXServer? (readonly)
The UNIX socket server.
73 74 75 |
# File 'lib/qb/ipc/stdio/server/service.rb', line 73 def server @server end |
#socket ⇒ UNIXSocket (readonly)
The socket we accept from the server.
80 81 82 |
# File 'lib/qb/ipc/stdio/server/service.rb', line 80 def socket @socket end |
#thread ⇒ attr_type (readonly)
TODO document thread
attribute.
59 60 61 |
# File 'lib/qb/ipc/stdio/server/service.rb', line 59 def thread @thread end |
Instance Method Details
#close! ⇒ nil
Not sure how correct this is... fucking threading. Seems to work...
We're done here, clean up!
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/qb/ipc/stdio/server/service.rb', line 194 def close! logger.debug "Closing...", socket: socket, server: server, path_exists: path.exist?, thread: thread, env_var: { env_var_name => ENV[env_var_name], } # Remove the path from the ENV so if we do anything after this the # old one isn't hanging around ENV.delete env_var_name # Kill the thread first so that it can't try to do anything else thread.kill if thread && thread.alive? socket.close unless socket.nil? @socket = nil server.close unless server.nil? @server = nil FileUtils.rm( path ) if path.exist? logger.debug "Closed.", socket: socket, server: server, path_exists: path.exist?, thread: thread, env_var: { env_var_name => ENV[env_var_name], } nil end |
#open! ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/qb/ipc/stdio/server/service.rb', line 156 def open! logger.debug "Opening..." # make sure env var is not already set (basically just prevents you from # accidentally opening two instances with the same name) if ENV.key? env_var_name raise "env already contains key #{ env_var_name }" \ "with value #{ ENV[env_var_name] }" end @thread = Thread.new do Thread.current.name = name logger.trace "thread started." @server = UNIXServer.new path.to_s while true do @socket = server.accept work_in_thread end end # set the env key so children can find the socket path ENV[env_var_name] = path.to_s logger.debug "Set env var", env_var_name => ENV[env_var_name] logger.debug "Service open." end |
#to_s ⇒ String
Returns a short string describing the instance. Used to set the name for instance loggers.
151 152 153 |
# File 'lib/qb/ipc/stdio/server/service.rb', line 151 def to_s "#<#{ self.class.name } name=#{ name.inspect } path=#{ path.to_s }>" end |