Class: Baykit::BayServer::Docker::Cgi::CgiTrain
- Inherits:
-
Train::Train
- Object
- Train::Train
- Baykit::BayServer::Docker::Cgi::CgiTrain
- Includes:
- Agent, Tours, Tours::ReqContentHandler, Train, Util
- Defined in:
- lib/baykit/bayserver/docker/cgi/cgi_train.rb
Constant Summary collapse
- READ_CHUNK_SIZE =
8192
Instance Attribute Summary collapse
-
#available ⇒ Object
readonly
Returns the value of attribute available.
-
#cgi_docker ⇒ Object
readonly
Returns the value of attribute cgi_docker.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#lock ⇒ Object
readonly
Returns the value of attribute lock.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#std_err ⇒ Object
readonly
Returns the value of attribute std_err.
-
#std_in ⇒ Object
readonly
Returns the value of attribute std_in.
-
#std_out ⇒ Object
readonly
Returns the value of attribute std_out.
Instance Method Summary collapse
- #close_pipes ⇒ Object
- #depart ⇒ Object
-
#initialize(cgi_docker, tur) ⇒ CgiTrain
constructor
A new instance of CgiTrain.
- #on_abort(tur) ⇒ Object
- #on_end_content(tur) ⇒ Object
- #on_read_content(tur, buf, start, len) ⇒ Object
- #start_tour(env) ⇒ Object
Constructor Details
#initialize(cgi_docker, tur) ⇒ CgiTrain
Returns a new instance of CgiTrain.
30 31 32 33 |
# File 'lib/baykit/bayserver/docker/cgi/cgi_train.rb', line 30 def initialize(cgi_docker, tur) super(tur) @cgi_docker = cgi_docker end |
Instance Attribute Details
#available ⇒ Object (readonly)
Returns the value of attribute available.
23 24 25 |
# File 'lib/baykit/bayserver/docker/cgi/cgi_train.rb', line 23 def available @available end |
#cgi_docker ⇒ Object (readonly)
Returns the value of attribute cgi_docker.
21 22 23 |
# File 'lib/baykit/bayserver/docker/cgi/cgi_train.rb', line 21 def cgi_docker @cgi_docker end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
22 23 24 |
# File 'lib/baykit/bayserver/docker/cgi/cgi_train.rb', line 22 def env @env end |
#lock ⇒ Object (readonly)
Returns the value of attribute lock.
24 25 26 |
# File 'lib/baykit/bayserver/docker/cgi/cgi_train.rb', line 24 def lock @lock end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
25 26 27 |
# File 'lib/baykit/bayserver/docker/cgi/cgi_train.rb', line 25 def pid @pid end |
#std_err ⇒ Object (readonly)
Returns the value of attribute std_err.
28 29 30 |
# File 'lib/baykit/bayserver/docker/cgi/cgi_train.rb', line 28 def std_err @std_err end |
#std_in ⇒ Object (readonly)
Returns the value of attribute std_in.
26 27 28 |
# File 'lib/baykit/bayserver/docker/cgi/cgi_train.rb', line 26 def std_in @std_in end |
#std_out ⇒ Object (readonly)
Returns the value of attribute std_out.
27 28 29 |
# File 'lib/baykit/bayserver/docker/cgi/cgi_train.rb', line 27 def std_out @std_out end |
Instance Method Details
#close_pipes ⇒ Object
173 174 175 176 177 |
# File 'lib/baykit/bayserver/docker/cgi/cgi_train.rb', line 173 def close_pipes() @std_in[1].close() @std_out[0].close() @std_err[0].close() end |
#depart ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/baykit/bayserver/docker/cgi/cgi_train.rb', line 59 def depart begin ############### # Handle StdOut HttpUtil.(@std_out[0], @tour.res.headers) if BayServer.harbor.trace_header @tour.res.headers.names.each do |name| @tour.res.headers.values(name).each do |value| BayLog.info("%s CGI: resHeader: %s=%s", @tour, name, value) end end end status = @tour.res.headers.get("Status") #BayLog.debug "Headers: #{@tours.res.headers.headers}" #BayLog.debug "Status: #{status}" if !StringUtil.empty?(status) pos = status.index(" ") if pos code = status[0, pos].to_i else code = status.to_i end @tour.res.headers.status = code end @tour.res.set_consume_listener do |len, resume| if resume @available = true end end @tour.res.send_headers(@tour_id) #BayLog.info("Reading STDOUT") while true buf = StringUtil.alloc(READ_CHUNK_SIZE) c = std_out[0].read(READ_CHUNK_SIZE, buf) if !c break end BayLog.trace("%s CGITrain: read stdout bytes: len=%d", @tour, buf.length) @available = @tour.res.send_content(@tour_id, buf, 0, buf.length) while !@available sleep(0.1) end end #BayLog.info("Reading STDERR") ############### # Handle StdErr ############### while true buf = StringUtil.alloc(READ_CHUNK_SIZE) c = std_err[0].read(READ_CHUNK_SIZE, buf) if !c break end BayLog.warn("%s CGITrain: read stderr bytes: %d", @tour, buf.length) BayLog.warn(buf) end @tour.res.end_content @tour_id rescue HttpException => e raise e rescue => e BayLog.error("%s CGITrain: Catch error: %s", @tour, e) BayLog.error_e(e) raise HttpException.new(HttpStatus::INTERNAL_SERVER_ERROR, "CGI error") ensure begin BayLog.debug("%s CGITrain: waiting process end", @tour) Process.wait(@pid) close_pipes() BayLog.debug("%s CGITrain: process ended", @tour) rescue => e BayLog.error_e(e) end end end |
#on_abort(tur) ⇒ Object
163 164 165 166 167 168 169 170 171 |
# File 'lib/baykit/bayserver/docker/cgi/cgi_train.rb', line 163 def on_abort(tur) BayLog.debug("%s CGITrain:abort", tur) close_pipes() BayLog.debug("%s KILL PROCESS!: %d", tur, @pid) Process.kill('KILL', @pid) return false # not aborted immediately end |
#on_end_content(tur) ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/baykit/bayserver/docker/cgi/cgi_train.rb', line 155 def on_end_content(tur) BayLog.trace("%s CGITrain:endContent", tur) if !TrainRunner.post(self) raise HttpException.new(HttpStatus::SERVICE_UNAVAILABLE, "TrainRunner is busy") end end |
#on_read_content(tur, buf, start, len) ⇒ Object
147 148 149 150 151 152 153 |
# File 'lib/baykit/bayserver/docker/cgi/cgi_train.rb', line 147 def on_read_content(tur, buf, start, len) BayLog.info("%s CGITrain:onReadContent: len=%d", tur, len) wrote_len = @std_in[1].write(buf[start, len]) BayLog.info("%s CGITrain:onReadContent: wrote=%d", tur, wrote_len) tur.req.consumed(Tour::TOUR_ID_NOCHECK, len) end |
#start_tour(env) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/baykit/bayserver/docker/cgi/cgi_train.rb', line 35 def start_tour(env) @env = env @available = false @lock = Mutex.new() @std_in = IO.pipe() @std_out = IO.pipe() @std_err = IO.pipe() @std_in[1].set_encoding("ASCII-8BIT") @std_out[0].set_encoding("ASCII-8BIT") @std_err[0].set_encoding("ASCII-8BIT") command = @cgi_docker.create_command(@env) BayLog.debug("%s Spawn: %s", @tour, command) @pid = Process.spawn(@env, command, :in => @std_in[0], :out => @std_out[1], :err => @std_err[1]) @std_in[0].close() @std_out[1].close() @std_err[1].close() BayLog.debug("#{@tour} PID: #{pid}") @tour.req.set_content_handler(self) end |