Module: Wurk::Context
- Defined in:
- lib/wurk/context.rb
Overview
Thread-local logging context. Job state (jid, bid, tags, elapsed, …) is attached here so loggers, middleware, and error handlers can pick it up without threading the hash through every call. Storage lives in ‘Thread.current` — never share across threads or forks.
Spec: docs/target/sidekiq-free.md §29.
Constant Summary collapse
- KEY =
:wurk_context
Class Method Summary collapse
-
.add(key, value) ⇒ Object
Add a single key to the current thread’s context.
-
.current ⇒ Object
Read the current thread’s context (or an empty hash if unset).
-
.with(hash) ⇒ Object
Run ‘block` with `hash` merged onto the thread-local context.
Class Method Details
.add(key, value) ⇒ Object
Add a single key to the current thread’s context. Creates the hash if no enclosing ‘with` block has been opened yet.
25 26 27 28 |
# File 'lib/wurk/context.rb', line 25 def self.add(key, value) Thread.current[KEY] ||= {} Thread.current[KEY][key] = value end |
.current ⇒ Object
Read the current thread’s context (or an empty hash if unset).
31 32 33 |
# File 'lib/wurk/context.rb', line 31 def self.current Thread.current[KEY] || {} end |
.with(hash) ⇒ Object
Run ‘block` with `hash` merged onto the thread-local context. The prior context is restored on exit, even if the block raises — safe to nest.
15 16 17 18 19 20 21 |
# File 'lib/wurk/context.rb', line 15 def self.with(hash) prior = Thread.current[KEY] Thread.current[KEY] = (prior || {}).merge(hash) yield ensure Thread.current[KEY] = prior end |