Module: Takagi::Base::ServerLifecycle
- Included in:
- Takagi::Base
- Defined in:
- lib/takagi/base/server_lifecycle.rb
Overview
Manages server lifecycle operations: booting, running, and spawning servers.
Extracted from Base class to follow Single Responsibility Principle. Handles configuration loading, server instantiation, and process management.
Instance Method Summary collapse
-
#boot!(config_path: 'config/takagi.yml') ⇒ Object
Boots the application by loading configuration and running initializers.
-
#run!(port: nil, config_path: 'config/takagi.yml', protocols: nil, router: nil, banner: true) ⇒ Object
Runs the server in the foreground (blocking).
-
#spawn!(port: 5683, protocols: nil) ⇒ Server, Multi
Spawns servers in background threads.
Instance Method Details
#boot!(config_path: 'config/takagi.yml') ⇒ Object
Boots the application by loading configuration and running initializers
15 16 17 18 |
# File 'lib/takagi/base/server_lifecycle.rb', line 15 def boot!(config_path: 'config/takagi.yml') Takagi.config.load_file(config_path) if File.exist?(config_path) Takagi::Initializer.run! end |
#run!(port: nil, config_path: 'config/takagi.yml', protocols: nil, router: nil, banner: true) ⇒ Object
Runs the server in the foreground (blocking)
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/takagi/base/server_lifecycle.rb', line 27 def run!(port: nil, config_path: 'config/takagi.yml', protocols: nil, router: nil, banner: true) boot!(config_path: config_path) enable_plugins!(app: self) # Show startup banner if ( port: port || Takagi.config.port, protocols: protocols || Takagi.config.protocols, router: router || self.router # Pass router to banner ) end selected_port = port || Takagi.config.port servers = build_servers(protocols || Takagi.config.protocols, selected_port, router: router) run_servers(servers) end |
#spawn!(port: 5683, protocols: nil) ⇒ Server, Multi
Spawns servers in background threads
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/takagi/base/server_lifecycle.rb', line 50 def spawn!(port: 5683, protocols: nil) protos = if protocols Array(protocols) else Takagi.config.protocols end.map(&:to_sym) servers = protos.map do |proto| # Temporary backward compatibility - will use registry after transition proto == :tcp ? Takagi::Server::Tcp.new(port: port) : Takagi::Server::Udp.new(port: port) end if servers.length == 1 server = servers.first thread = Thread.new { server.run! } # Store thread reference on the server instance for cleanup server.instance_variable_set(:@server_thread, thread) server else multi = Takagi::Server::Multi.new(servers) thread = Thread.new { multi.run! } # Store thread reference on the multi instance for cleanup multi.instance_variable_set(:@server_thread, thread) multi end end |