Module: LogStruct::Integrations::Puma
- Extended by:
- T::Helpers, T::Sig
- Defined in:
- lib/log_struct/integrations/puma.rb
Defined Under Namespace
Modules: EventsPatch, LogWriterPatch, RackHandlerPatch
Constant Summary collapse
- STATE =
T.let( { installed: false, boot_emitted: false, shutdown_emitted: false, handler_pending_started: false, start_info: { mode: nil, puma_version: nil, puma_codename: nil, ruby_version: nil, min_threads: nil, max_threads: nil, environment: nil, pid: nil, listening: [] } }, T::Hash[Symbol, T.untyped] )
Class Method Summary collapse
- .emit_boot_if_needed! ⇒ void
- .emit_shutdown!(_message) ⇒ void
-
.emit_started! ⇒ void
No server hooks; rely on parsing only.
- .handle_integration_error(e) ⇒ void
- .install_patches! ⇒ void
- .process_line(line) ⇒ Boolean
- .setup(config) ⇒ Boolean?
-
.state_reset! ⇒ void
No stdout interception.
Class Method Details
.emit_boot_if_needed! ⇒ void
This method returns an undefined value.
287 288 289 290 |
# File 'lib/log_struct/integrations/puma.rb', line 287 def emit_boot_if_needed! # Intentionally no-op: we no longer emit a boot log STATE[:boot_emitted] = true end |
.emit_shutdown!(_message) ⇒ void
This method returns an undefined value.
316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/log_struct/integrations/puma.rb', line 316 def emit_shutdown!() return if STATE[:shutdown_emitted] STATE[:shutdown_emitted] = true log = Log::Puma::Shutdown.new( process_id: T.cast(STATE[:start_info], T::Hash[Symbol, T.untyped])[:pid] || Process.pid, level: Level::Info, timestamp: Time.now ) LogStruct.info(log) # Only use LogStruct; SemanticLogger routes to STDOUT in test # Let SemanticLogger appender write to STDOUT end |
.emit_started! ⇒ void
This method returns an undefined value.
No server hooks; rely on parsing only
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/log_struct/integrations/puma.rb', line 295 def emit_started! si = T.cast(STATE[:start_info], T::Hash[Symbol, T.untyped]) log = Log::Puma::Start.new( mode: T.cast(si[:mode], T.nilable(String)), puma_version: T.cast(si[:puma_version], T.nilable(String)), puma_codename: T.cast(si[:puma_codename], T.nilable(String)), ruby_version: T.cast(si[:ruby_version], T.nilable(String)), min_threads: T.cast(si[:min_threads], T.nilable(Integer)), max_threads: T.cast(si[:max_threads], T.nilable(Integer)), environment: T.cast(si[:environment], T.nilable(String)), process_id: T.cast(STATE[:start_info], T::Hash[Symbol, T.untyped])[:pid] || Process.pid, listening_addresses: T.cast(T.cast(STATE[:start_info], T::Hash[Symbol, T.untyped])[:listening], T::Array[String]), level: Level::Info, timestamp: Time.now ) LogStruct.info(log) STATE[:handler_pending_started] = false # Only use LogStruct; SemanticLogger routes to STDOUT in test end |
.handle_integration_error(e) ⇒ void
This method returns an undefined value.
141 142 143 144 145 146 147 148 |
# File 'lib/log_struct/integrations/puma.rb', line 141 def handle_integration_error(e) server_mode = ::LogStruct.server_mode? if defined?(::Rails) && ::Rails.respond_to?(:env) && ::Rails.env.test? && !server_mode raise e else LogStruct.handle_exception(e, source: Source::Puma) end end |
.install_patches! ⇒ void
This method returns an undefined value.
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 |
# File 'lib/log_struct/integrations/puma.rb', line 98 def install_patches! return if STATE[:installed] STATE[:installed] = true state_reset! begin begin require "puma" rescue => e handle_integration_error(e) end puma_mod = ::Object.const_defined?(:Puma) ? T.unsafe(::Object.const_get(:Puma)) : nil # rubocop:disable Sorbet/ConstantsFromStrings # rubocop:disable Sorbet/ConstantsFromStrings if puma_mod&.const_defined?(:LogWriter) T.unsafe(::Object.const_get("Puma::LogWriter")).prepend(LogWriterPatch) end if puma_mod&.const_defined?(:Events) ev = T.unsafe(::Object.const_get("Puma::Events")) ev.prepend(EventsPatch) end # Patch Rack::Handler::Puma.run to emit lifecycle logs using options if ::Object.const_defined?(:Rack) rack_mod = T.unsafe(::Object.const_get(:Rack)) if rack_mod.const_defined?(:Handler) handler_mod = T.unsafe(rack_mod.const_get(:Handler)) if handler_mod.const_defined?(:Puma) handler = T.unsafe(handler_mod.const_get(:Puma)) handler.singleton_class.prepend(RackHandlerPatch) end end end # Avoid patching CLI/Server; rely on log parsing # Avoid patching CLI to minimize version-specific risks # rubocop:enable Sorbet/ConstantsFromStrings rescue => e handle_integration_error(e) end # Rely on Puma patches to observe lines end |
.process_line(line) ⇒ Boolean
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/log_struct/integrations/puma.rb', line 172 def process_line(line) l = line.to_s.strip return false if l.empty? # Suppress non-JSON rails banners return true if l.start_with?("=> ") # Ignore boot line return true if l.start_with?("=> Booting Puma") if l.start_with?("Puma starting in ") # Example: Puma starting in single mode... T.cast(STATE[:start_info], T::Hash[Symbol, T.untyped])[:mode] = l.sub("Puma starting in ", "").sub(" mode...", "") return true end if (m = l.match(/^(?:\*\s*)?Puma version: (\S+)(?:.*"([^\"]+)")?/)) T.cast(STATE[:start_info], T::Hash[Symbol, T.untyped])[:puma_version] = m[1] if m[2] T.cast(STATE[:start_info], T::Hash[Symbol, T.untyped])[:puma_codename] = m[2] end return true end if (m = l.match(/^\* Ruby version: (.+)$/)) T.cast(STATE[:start_info], T::Hash[Symbol, T.untyped])[:ruby_version] = m[1] return true end if (m = l.match(/^(?:\*\s*)?Min threads: (\d+)/)) T.cast(STATE[:start_info], T::Hash[Symbol, T.untyped])[:min_threads] = m[1].to_i return true end if (m = l.match(/^(?:\*\s*)?Max threads: (\d+)/)) T.cast(STATE[:start_info], T::Hash[Symbol, T.untyped])[:max_threads] = m[1].to_i return true end if (m = l.match(/^(?:\*\s*)?Environment: (\S+)/)) T.cast(STATE[:start_info], T::Hash[Symbol, T.untyped])[:environment] = m[1] return true end if (m = l.match(/^(?:\*\s*)?PID:\s+(\d+)/)) T.cast(STATE[:start_info], T::Hash[Symbol, T.untyped])[:pid] = m[1].to_i return true end if (m = l.match(/^\*?\s*Listening on (.+)$/)) si = T.cast(STATE[:start_info], T::Hash[Symbol, T.untyped]) list = T.cast(si[:listening], T::Array[T.untyped]) address = T.must(m[1]) list << address unless list.include?(address) # Emit started when we see the first listening address if !STATE[:started_emitted] emit_started! STATE[:started_emitted] = true end return true end if l == "Use Ctrl-C to stop" si = T.cast(STATE[:start_info], T::Hash[Symbol, T.untyped]) # Fallback: if no listening address captured yet, infer from ARGV if T.cast(si[:listening], T::Array[T.untyped]).empty? begin port = T.let(nil, T.untyped) ARGV.each_with_index do |arg, idx| if arg == "-p" || arg == "--port" port = ARGV[idx + 1] break elsif arg.start_with?("--port=") port = arg.split("=", 2)[1] break end end if port si[:listening] << "tcp://127.0.0.1:#{port}" end rescue => e handle_integration_error(e) end end if !STATE[:started_emitted] emit_started! STATE[:started_emitted] = true end return false end if l.start_with?("- Gracefully stopping") emit_shutdown!(l) return true end if l.start_with?("=== puma shutdown:") emit_shutdown!(l) return true end if l == "- Goodbye!" # Swallow return true end if l == "Exiting" emit_shutdown!(l) return true end false end |
.setup(config) ⇒ Boolean?
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/log_struct/integrations/puma.rb', line 35 def setup(config) return nil unless config.integrations.enable_puma # No stdout wrapping here. # Ensure Puma is loaded so we can patch its classes begin require "puma" rescue LoadError # If Puma isn't available, skip setup return nil end install_patches! if ARGV.include?("server") # Emit deterministic boot/started events based on CLI args begin port = T.let(nil, T.nilable(String)) ARGV.each_with_index do |arg, idx| if arg == "-p" || arg == "--port" port = ARGV[idx + 1] break elsif arg.start_with?("--port=") port = arg.split("=", 2)[1] break end end si = T.cast(STATE[:start_info], T::Hash[Symbol, T.untyped]) si[:pid] ||= Process.pid si[:environment] ||= ((defined?(::Rails) && ::Rails.respond_to?(:env)) ? ::Rails.env : nil) si[:mode] ||= "single" if port && !T.cast(si[:listening], T::Array[T.untyped]).any? { |a| a.to_s.include?(":" + port.to_s) } si[:listening] = ["tcp://127.0.0.1:#{port}"] end emit_boot_if_needed! unless STATE[:started_emitted] emit_started! STATE[:started_emitted] = true end rescue => e handle_integration_error(e) end begin %w[TERM INT].each do |sig| Signal.trap(sig) { emit_shutdown!(sig) } end rescue => e handle_integration_error(e) end at_exit do emit_shutdown!("Exiting") rescue => e handle_integration_error(e) end # Connection-based readiness: emit started once port is accepting connections # No background threads or sockets; rely solely on parsing Puma output end true end |
.state_reset! ⇒ void
This method returns an undefined value.
No stdout interception
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/log_struct/integrations/puma.rb', line 153 def state_reset! STATE[:boot_emitted] = false STATE[:shutdown_emitted] = false STATE[:started_emitted] = false STATE[:handler_pending_started] = false STATE[:start_info] = { mode: nil, puma_version: nil, puma_codename: nil, ruby_version: nil, min_threads: nil, max_threads: nil, environment: nil, pid: nil, listening: [] } end |