Class: Mxrb::RubyApp::Supervisor

Inherits:
Object
  • Object
show all
Defined in:
lib/mxrb/ruby_app.rb

Overview

Starts the Ruby API and Vite dev server as one supervised command.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, host: '127.0.0.1', api_port: 9292, frontend_port: 5173, npm: ENV.fetch('MXRB_NPM', 'npm'), frontend: true, environment: nil) ⇒ Supervisor

Returns a new instance of Supervisor.



1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
# File 'lib/mxrb/ruby_app.rb', line 1275

def initialize(root, host: '127.0.0.1', api_port: 9292, frontend_port: 5173,
               npm: ENV.fetch('MXRB_NPM', 'npm'), frontend: true, environment: nil)
  @root = File.expand_path(root)
  @host = host.to_s
  @api_port = Integer(api_port)
  @frontend_port = Integer(frontend_port)
  @npm = npm.to_s
  @frontend = frontend
  @environment = if environment.is_a?(Environment)
                   environment
                 else
                   Environment.load(
                     environment, root: @root
                   )
                 end
end

Instance Attribute Details

#api_portObject (readonly)

Returns the value of attribute api_port.



1273
1274
1275
# File 'lib/mxrb/ruby_app.rb', line 1273

def api_port
  @api_port
end

#frontend_portObject (readonly)

Returns the value of attribute frontend_port.



1273
1274
1275
# File 'lib/mxrb/ruby_app.rb', line 1273

def frontend_port
  @frontend_port
end

#hostObject (readonly)

Returns the value of attribute host.



1273
1274
1275
# File 'lib/mxrb/ruby_app.rb', line 1273

def host
  @host
end

#rootObject (readonly)

Returns the value of attribute root.



1273
1274
1275
# File 'lib/mxrb/ruby_app.rb', line 1273

def root
  @root
end

Instance Method Details

#shutdownObject



1314
1315
1316
1317
1318
1319
1320
1321
# File 'lib/mxrb/ruby_app.rb', line 1314

def shutdown
  @server&.shutdown
  terminate(@frontend_pid)
  terminate(@backend_pid)
  @backend&.join(2)
rescue Errno::ESRCH, Errno::ECHILD
  nil
end

#startObject



1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
# File 'lib/mxrb/ruby_app.rb', line 1292

def start
  if external_backend?
    @backend_pid = spawn_backend
  else
    @server = Server.new(root, host:, port: api_port, environment: @environment)
    @backend = Thread.new { @server.start }
  end
  @frontend_pid = spawn_frontend if @frontend
  yield(self) if block_given?
  if @frontend_pid
    Process.wait(@frontend_pid)
  elsif @backend_pid
    Process.wait(@backend_pid)
  else
    @backend.join
  end
rescue Interrupt, Errno::ECHILD
  nil
ensure
  shutdown
end