Skip to content

Commit 5907948

Browse files
rosabensheldon
andcommitted
Port changes by @bensheldon to run Solid Queue in the same process as Puma
See #115 Co-authored-by: Ben Sheldon <[email protected]>
1 parent 057977b commit 5907948

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

lib/puma/plugin/solid_queue.rb

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,50 @@
11
require "puma/plugin"
22

3+
module Puma
4+
class DSL
5+
def solid_queue_mode(mode = :fork)
6+
@options[:solid_queue_mode] = mode.to_sym
7+
end
8+
end
9+
end
10+
311
Puma::Plugin.create do
4-
attr_reader :puma_pid, :solid_queue_pid, :log_writer
12+
attr_reader :puma_pid, :solid_queue_pid, :log_writer, :solid_queue_supervisor
513

614
def start(launcher)
715
@log_writer = launcher.log_writer
816
@puma_pid = $$
917

10-
in_background do
11-
monitor_solid_queue
18+
if launcher.options[:solid_queue_mode] == :async
19+
start_async(launcher)
20+
else
21+
start_forked(launcher)
1222
end
23+
end
1324

14-
launcher.events.on_booted do
15-
@solid_queue_pid = fork do
16-
Thread.new { monitor_puma }
17-
SolidQueue::Supervisor.start
25+
private
26+
def start_forked(launcher)
27+
in_background do
28+
monitor_solid_queue
1829
end
30+
31+
launcher.events.on_booted do
32+
@solid_queue_pid = fork do
33+
Thread.new { monitor_puma }
34+
SolidQueue::Supervisor.start(mode: :fork)
35+
end
36+
end
37+
38+
launcher.events.on_stopped { stop_solid_queue }
39+
launcher.events.on_restart { stop_solid_queue }
1940
end
2041

21-
launcher.events.on_stopped { stop_solid_queue }
22-
launcher.events.on_restart { stop_solid_queue }
23-
end
42+
def start_async(launcher)
43+
launcher.events.on_booted { @solid_queue_supervisor = SolidQueue::Supervisor.start(mode: :async) }
44+
launcher.events.on_stopped { solid_queue_supervisor.stop }
45+
launcher.events.on_restart { solid_queue_supervisor.stop; solid_queue_supervisor.start }
46+
end
2447

25-
private
2648
def stop_solid_queue
2749
Process.waitpid(solid_queue_pid, Process::WNOHANG)
2850
log "Stopping Solid Queue..."

test/dummy/config/puma.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,5 @@
4242
# Allow puma to be restarted by `bin/rails restart` command.
4343
plugin :tmp_restart
4444
plugin :solid_queue
45+
46+
solid_queue_mode :fork

0 commit comments

Comments
 (0)