Class: Wurk::Embedded
- Inherits:
-
Object
- Object
- Wurk::Embedded
- Includes:
- Component
- Defined in:
- lib/wurk/embedded.rb
Overview
Embeds Wurk inside an arbitrary Ruby process (Puma, rake task, custom daemon) without forking a swarm. Same Launcher/Manager/Processor stack the CLI uses — only the parent-process supervision is skipped.
Typical use:
instance = Wurk. { |c| c.queues = %w[critical default] }
instance.run
# ... host process serves traffic ...
instance.stop
Concurrency defaults to 2 (set in Wurk.configure_embed) because the GIL
makes contention worse in a host process that already has its own thread
pool. The heartbeat marks embedded: true so the dashboard distinguishes
embedded workers from swarm-forked workers.
Spec: docs/target/sidekiq-free.md §22 (Sidekiq::Embedded).
Instance Attribute Summary collapse
-
#config ⇒ Object
included
from Component
readonly
Returns the value of attribute config.
-
#launcher ⇒ Object
readonly
Returns the value of attribute launcher.
Instance Method Summary collapse
- #default_tag(dir = Dir.pwd) ⇒ Object included from Component
-
#fire_event(event, oneshot: true, reverse: false, reraise: false) ⇒ Object
included
from Component
Invokes lifecycle hooks for
event. - #handle_exception(ex, ctx = {}) ⇒ Object included from Component
- #hostname ⇒ Object included from Component
- #identity ⇒ Object included from Component
-
#initialize(config) ⇒ Embedded
constructor
A new instance of Embedded.
-
#leader? ⇒ Boolean
included
from Component
True iff this process currently holds the cluster
dear-leaderlock. -
#logger ⇒ Object
included
from Component
--- delegated to config -------------------------------------------.
- #mono_ms ⇒ Object included from Component
- #process_nonce ⇒ Object included from Component
-
#quiet ⇒ Object
Stop fetching new work; in-flight jobs continue.
-
#real_ms ⇒ Object
included
from Component
--- clocks ---------------------------------------------------------.
- #redis ⇒ Object included from Component
-
#run ⇒ Object
Validates Redis, fires :startup, boots the launcher, sleeps 0.2 so the worker threads have time to spin up before the host goes back to serving traffic.
-
#safe_thread(name, priority: nil, &block) ⇒ Object
included
from Component
Spawns a named thread that runs
blockunderwatchdog(name). -
#stop ⇒ Object
Graceful drain inside config; cancels in-flight jobs that exceed the deadline.
-
#tid ⇒ Object
included
from Component
--- identity -------------------------------------------------------.
-
#watchdog(last_words) ⇒ Object
included
from Component
Wraps a block at a thread boundary: any unhandled exception is reported via handle_exception (so it lands in error_handlers / the log) and then re-raised.
Constructor Details
#initialize(config) ⇒ Embedded
Returns a new instance of Embedded.
28 29 30 31 |
# File 'lib/wurk/embedded.rb', line 28 def initialize(config) @config = config @launcher = nil end |
Instance Attribute Details
#config ⇒ Object (readonly) Originally defined in module Component
Returns the value of attribute config.
#launcher ⇒ Object (readonly)
Returns the value of attribute launcher.
26 27 28 |
# File 'lib/wurk/embedded.rb', line 26 def launcher @launcher end |
Instance Method Details
#default_tag(dir = Dir.pwd) ⇒ Object Originally defined in module Component
#fire_event(event, oneshot: true, reverse: false, reraise: false) ⇒ Object Originally defined in module Component
Invokes lifecycle hooks for event. Hooks run in registration order
(or LIFO when reverse: true, used for teardown). A raise in one hook
is reported via handle_exception and does NOT stop the next hook unless
reraise: true (used in tests / fail-fast boot). oneshot: true
clears the bucket after dispatch so the event can't fire twice.
#handle_exception(ex, ctx = {}) ⇒ Object Originally defined in module Component
#hostname ⇒ Object Originally defined in module Component
#identity ⇒ Object Originally defined in module Component
#leader? ⇒ Boolean Originally defined in module Component
True iff this process currently holds the cluster dear-leader lock.
Per spec, the check is performed at call time (Wurk does not cache);
callers must not poll faster than the 60s follower cadence. Returns
false unconditionally when WURK_LEADER=false (or SIDEKIQ_LEADER=false)
is set on the process (opt-out hot-standby). Any Redis error is swallowed →
false, so a transient partition can't propagate as an exception into user
code.
Spec: docs/target/sidekiq-ent.md §6.1.
#logger ⇒ Object Originally defined in module Component
--- delegated to config -------------------------------------------
#mono_ms ⇒ Object Originally defined in module Component
#process_nonce ⇒ Object Originally defined in module Component
#quiet ⇒ Object
Stop fetching new work; in-flight jobs continue.
47 48 49 |
# File 'lib/wurk/embedded.rb', line 47 def quiet @launcher&.quiet end |
#real_ms ⇒ Object Originally defined in module Component
--- clocks ---------------------------------------------------------
#redis ⇒ Object Originally defined in module Component
#run ⇒ Object
Validates Redis, fires :startup, boots the launcher, sleeps 0.2 so the worker threads have time to spin up before the host goes back to serving traffic. Mirrors Sidekiq::Embedded#run.
36 37 38 39 40 41 42 43 44 |
# File 'lib/wurk/embedded.rb', line 36 def run housekeeping fire_event(:startup, reverse: false, reraise: true) @launcher = build_launcher @launcher.run sleep 0.2 logger.info { "Wurk running embedded, total process thread count: #{Thread.list.size}" } logger.debug { Thread.list.map(&:name).to_s } end |
#safe_thread(name, priority: nil, &block) ⇒ Object Originally defined in module Component
Spawns a named thread that runs block under watchdog(name). The
parent must retain the returned Thread; otherwise GC may not, but
report_on_exception is disabled so we don't double-log on death.
#stop ⇒ Object
Graceful drain inside config; cancels in-flight jobs that exceed the deadline.
53 54 55 |
# File 'lib/wurk/embedded.rb', line 53 def stop @launcher&.stop end |
#tid ⇒ Object Originally defined in module Component
--- identity -------------------------------------------------------
#watchdog(last_words) ⇒ Object Originally defined in module Component
Wraps a block at a thread boundary: any unhandled exception is reported
via handle_exception (so it lands in error_handlers / the log) and then
re-raised. last_words is the component label included in the context.