diff --git a/README.md b/README.md index ac408f7a6..12f399c0c 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/puma/plugin/solid_queue.rb b/lib/puma/plugin/solid_queue.rb index 65f10dc83..606311a02 100644 --- a/lib/puma/plugin/solid_queue.rb +++ b/lib/puma/plugin/solid_queue.rb @@ -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 @@ -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 @@ -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 diff --git a/test/dummy/config/puma_forked_async.rb b/test/dummy/config/puma_forked_async.rb new file mode 100644 index 000000000..89a72d9df --- /dev/null +++ b/test/dummy/config/puma_forked_async.rb @@ -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 diff --git a/test/integration/puma/plugin_forked_async_test.rb b/test/integration/puma/plugin_forked_async_test.rb new file mode 100644 index 000000000..a87c995dc --- /dev/null +++ b/test/integration/puma/plugin_forked_async_test.rb @@ -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