Class: MilkTea::Run::WasmPreviewServer
- Inherits:
-
Object
- Object
- MilkTea::Run::WasmPreviewServer
- Defined in:
- lib/milk_tea/tooling/run.rb
Constant Summary collapse
- HOST =
"127.0.0.1"- DEFAULT_IDLE_TIMEOUT =
600
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
-
#initialize(root_dir:, host: HOST, port: nil, idle_timeout: DEFAULT_IDLE_TIMEOUT) ⇒ WasmPreviewServer
constructor
A new instance of WasmPreviewServer.
- #listen! ⇒ Object
- #serve_forever(trap_signals: true) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #url_for(entry_name) ⇒ Object
Constructor Details
#initialize(root_dir:, host: HOST, port: nil, idle_timeout: DEFAULT_IDLE_TIMEOUT) ⇒ WasmPreviewServer
Returns a new instance of WasmPreviewServer.
15 16 17 18 19 20 21 22 23 |
# File 'lib/milk_tea/tooling/run.rb', line 15 def initialize(root_dir:, host: HOST, port: nil, idle_timeout: DEFAULT_IDLE_TIMEOUT) @root_dir = File.(root_dir) @host = host @port = port @idle_timeout = idle_timeout @thread = nil @server = nil @running = false end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
25 26 27 |
# File 'lib/milk_tea/tooling/run.rb', line 25 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
25 26 27 |
# File 'lib/milk_tea/tooling/run.rb', line 25 def port @port end |
Instance Method Details
#listen! ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/milk_tea/tooling/run.rb', line 47 def listen! return self if @server @server = TCPServer.new(@host, @port || 0) @port = @server.addr[1] self end |
#serve_forever(trap_signals: true) ⇒ Object
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 |
# File 'lib/milk_tea/tooling/run.rb', line 61 def serve_forever(trap_signals: true) listen! @running = true previous_handlers = trap_signals ? install_signal_handlers : nil last_request_at = Time.now while @running if @idle_timeout.positive? && Time.now - last_request_at > @idle_timeout break end readable, = IO.select([@server], nil, nil, 0.25) next unless readable client = @server.accept Thread.new(client) do |socket| begin handle_client(socket) ensure last_request_at = Time.now socket.close unless socket.closed? end end end ensure close_server restore_signal_handlers(previous_handlers) if previous_handlers end |
#start ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/milk_tea/tooling/run.rb', line 27 def start listen! @thread = Thread.new { serve_forever(trap_signals: false) } self rescue StandardError stop raise end |
#stop ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/milk_tea/tooling/run.rb', line 36 def stop @running = false if @thread wake_server @thread.join(1) end @thread = nil close_server nil end |
#url_for(entry_name) ⇒ Object
55 56 57 58 59 |
# File 'lib/milk_tea/tooling/run.rb', line 55 def url_for(entry_name) raise RunError, "wasm preview server has not been started" unless @port "http://#{@host}:#{@port}/#{entry_name}" end |