Class: QB::IPC::STDIO::Server::Service

Inherits:
Object
  • Object
show all
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

InService, LogService, OutService

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, socket_dir:) ⇒ Service

Construct an IO service.

Parameters:

  • name (Symbol)

    What this service is for... :in, :out, :err...

    Used as the thread name.



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_nameString (readonly)

TODO document env_var_name attribute.

Returns:

  • (String)


66
67
68
# File 'lib/qb/ipc/stdio/server/service.rb', line 66

def env_var_name
  @env_var_name
end

#nameSynbol (readonly)

The service's name, like :in, :out, :err.

Returns:

  • (Synbol)


45
46
47
# File 'lib/qb/ipc/stdio/server/service.rb', line 45

def name
  @name
end

#pathPathname (readonly)

Absolute path to socket file.

Returns:

  • (Pathname)


52
53
54
# File 'lib/qb/ipc/stdio/server/service.rb', line 52

def path
  @path
end

#serverUNIXServer? (readonly)

The UNIX socket server.

Returns:

  • (UNIXServer?)


73
74
75
# File 'lib/qb/ipc/stdio/server/service.rb', line 73

def server
  @server
end

#socketUNIXSocket (readonly)

The socket we accept from the server.

Returns:

  • (UNIXSocket)


80
81
82
# File 'lib/qb/ipc/stdio/server/service.rb', line 80

def socket
  @socket
end

#threadattr_type (readonly)

TODO document thread attribute.

Returns:

  • (attr_type)


59
60
61
# File 'lib/qb/ipc/stdio/server/service.rb', line 59

def thread
  @thread
end

Instance Method Details

#close!nil

TODO:

Not sure how correct this is... fucking threading. Seems to work...

We're done here, clean up!

Returns:

  • (nil)


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_sString

Returns a short string describing the instance. Used to set the name for instance loggers.

Returns:

  • (String)

    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