Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,15 @@ Note that in this case, the `processes` configuration option will be ignored. Se

**The recommended and default mode is `fork`. Only use `async` if you know what you're doing and have strong reasons to**

In between, the plugin can fork a single process and run the supervisor there in `async` mode, with all workers, dispatchers and the scheduler as threads of that one process:

```ruby
plugin :solid_queue
solid_queue_mode :forked_async
```

This uses less memory than `fork`, since there's one process for all of them instead of one each, while keeping jobs out of Puma's process, so that jobs using a lot of CPU don't slow down requests as they can with `async`. Jobs are less isolated from each other than with `fork`: a job that crashes or blocks its process affects all of them. As with `fork`, the plugin stops Puma if that process exits, and the `processes` configuration option is ignored, as with `async`.


## Jobs and transactional integrity
:warning: Having your jobs in the same ACID-compliant database as your application data enables a powerful yet sharp tool: taking advantage of transactional integrity to ensure some action in your app is not committed unless your job is also committed and vice versa, and ensuring that your job won't be enqueued until the transaction within which you're enqueuing it is committed. This can be very powerful and useful, but it can also backfire if you base some of your logic on this behaviour, and in the future, you move to another active job backend, or if you simply move Solid Queue to its own database, and suddenly the behaviour changes under you. Because this can be quite tricky and many people shouldn't need to worry about it, by default Solid Queue is configured in a different database as the main app.
Expand Down
11 changes: 7 additions & 4 deletions lib/puma/plugin/solid_queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ def start(launcher)
@log_writer = launcher.log_writer
@puma_pid = $$

if launcher.options[:solid_queue_mode] == :async
case launcher.options[:solid_queue_mode]
when :async
start_async(launcher)
when :forked_async
start_forked(launcher, supervisor_mode: :async)
else
start_forked(launcher)
end
end

private
def start_forked(launcher)
def start_forked(launcher, supervisor_mode: :fork)
in_background do
monitor_solid_queue
end
Expand All @@ -32,7 +35,7 @@ def start_forked(launcher)
launcher.events.on_booted do
@solid_queue_pid = fork do
Thread.new { monitor_puma }
SolidQueue::Supervisor.start(mode: :fork)
SolidQueue::Supervisor.start(mode: supervisor_mode)

# Same as Processes::Supervised#create_fork: skip at-exit
# finalization, which can deadlock on database handles when a
Expand All @@ -47,7 +50,7 @@ def start_forked(launcher)
launcher.events.after_booted do
@solid_queue_pid = fork do
Thread.new { monitor_puma }
start_solid_queue(mode: :fork)
start_solid_queue(mode: supervisor_mode)

# Same as Processes::Supervised#create_fork: skip at-exit
# finalization, which can deadlock on database handles when a
Expand Down
42 changes: 42 additions & 0 deletions test/dummy/config/puma_forked_async.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers: a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum; this matches the default thread size of Active Record.
#
max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
threads min_threads_count, max_threads_count

# Specifies the `worker_timeout` threshold that Puma will use to wait before
# terminating a worker in development environments.
#
worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development"

# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "development" }

# Specifies the `pidfile` that Puma will use.
pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }

# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked web server processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
# workers ENV.fetch("WEB_CONCURRENCY") { 2 }

# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory.
#
# preload_app!

# Allow puma to be restarted by `bin/rails restart` command.
plugin :tmp_restart
plugin :solid_queue

solid_queue_mode :forked_async
46 changes: 46 additions & 0 deletions test/integration/puma/plugin_forked_async_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

require "test_helper"
require_relative "plugin_testing"

class PluginForkedAsyncTest < ActiveSupport::TestCase
include PluginTesting

test "run the supervisor and its threads in a single process forked from puma" do
supervisor = find_processes_registered_as("Supervisor(async)").first

assert_not_equal @pid, supervisor.pid
assert_equal [ supervisor.pid ], SolidQueue::Process.pluck(:pid).uniq
end

test "stop puma when solid queue's supervisor dies" do
supervisor = find_processes_registered_as("Supervisor(async)").first

signal_process(supervisor.pid, :KILL)
wait_for_process_termination_with_timeout(@pid)

assert_not process_exists?(@pid)

# The killed supervisor couldn't deregister itself and its threads
SolidQueue::Process.destroy_all
end

test "finish in-flight jobs when puma stops" do
active_job = StoreResultJob.perform_later(:in_flight, pause: 1.second)
job = SolidQueue::Job.find_by!(active_job_id: active_job.job_id)
wait_while_with_timeout(5.seconds) { skip_active_record_query_cache { job.reload.claimed_execution.nil? } }

terminate_process(@pid, signal: :TERM)

skip_active_record_query_cache do
assert_equal 1, JobResult.where(value: :in_flight, status: "completed").count
assert job.reload.finished?
assert_nil job.ready_execution
end
end

private
def solid_queue_mode
:forked_async
end
end
Loading