Class: QB::Util::STDIO::Service
- Inherits:
-
Object
- Object
- QB::Util::STDIO::Service
- Defined in:
- lib/qb/util/stdio.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 Method Summary collapse
-
#close! ⇒ Object
open.
- #debug(*args) ⇒ Object
-
#initialize(name) ⇒ Service
constructor
A new instance of Service.
- #open! ⇒ Object
Constructor Details
#initialize(name) ⇒ Service
Returns a new instance of Service.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/qb/util/stdio.rb', line 71 def initialize name @name = name @thread = nil @server = nil @socket = nil @env_key = "QB_STDIO_#{ name.upcase }" unless SOCKET_DIR.exist? FileUtils.mkdir SOCKET_DIR end @path = SOCKET_DIR.join "#{ name }.#{ SecureRandom.uuid }.sock" @debug_header = "#{ name }@#{ @path.to_s }" end |
Instance Method Details
#close! ⇒ Object
open
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/qb/util/stdio.rb', line 121 def close! # clean up. # # TODO not sure how correct this is... # debug "closing..." @socket.close unless @socket.nil? @socket = nil @server.close unless @server.nil? @server = nil FileUtils.rm(@path) if @path.exist? @thread.kill unless @thread.nil? debug "closed." end |
#debug(*args) ⇒ Object
87 88 89 |
# File 'lib/qb/util/stdio.rb', line 87 def debug *args QB.debug "#{ @debug_header }", *args end |
#open! ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/qb/util/stdio.rb', line 91 def open! 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_key raise <<-END.squish env already contains key #{ @env_key } with value #{ ENV[@env_key] } END end @thread = Thread.new do debug "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_key] = @path.to_s debug "set env var #{ @env_key }=#{ ENV[@env_key] }" debug "service open." end |