Class: Wurk::Embedded

Inherits:
Object
  • Object
show all
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.configure_embed { |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).

Constant Summary

Constants included from Component

Component::DEFAULT_THREAD_PRIORITY, Component::PROCESS_NONCE

Instance Attribute Summary collapse

Attributes included from Component

#config

Instance Method Summary collapse

Methods included from Component

#default_tag, #fire_event, #handle_exception, #hostname, #identity, #leader?, #logger, #mono_ms, #process_nonce, #real_ms, #redis, #safe_thread, #tid, #watchdog

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

#launcherObject (readonly)

Returns the value of attribute launcher.



26
27
28
# File 'lib/wurk/embedded.rb', line 26

def launcher
  @launcher
end

Instance Method Details

#quietObject

Stop fetching new work; in-flight jobs continue.



47
48
49
# File 'lib/wurk/embedded.rb', line 47

def quiet
  @launcher&.quiet
end

#runObject

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

#stopObject

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