From c5997263b97c595c271bac13d626ee377873d621 Mon Sep 17 00:00:00 2001 From: Jeremy Daer Date: Sat, 12 Sep 2026 12:40:04 -0700 Subject: [PATCH] Boot the Rails environment in the Resque worker so background jobs run The Procfile runs the worker with `resque-pool`, whose CLI requires `resque` before the Rakefile loads `config/application`. At that point `Rails::Railtie` isn't defined yet, so resque's own railtie -- the code that would wire `resque:setup => :environment` -- is skipped, and by the time `rails/all` loads, `require "resque"` is already a no-op. The pool master therefore never boots the app: `resque:preload` eager-loads nothing (eager_load is only true once the environment is loaded), so no ActiveJob class is ever defined and every enqueued job dies with "Failed to instantiate job, class ... doesn't exist." Concretely, ActiveStorage::PurgeJob never runs, so destroying a book never purges its uploaded files -- they stay on disk and remain retrievable via the public Disk URL for the life of the instance -- and Turbo::Streams::BroadcastJob never runs either. Wire `resque:setup => :environment` in lib/tasks/resque.rake so the pool master boots the full Rails environment and eager-loads every job class before forking workers, and reconnect each forked worker to its own Redis and database connections. --- lib/tasks/resque.rake | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 lib/tasks/resque.rake diff --git a/lib/tasks/resque.rake b/lib/tasks/resque.rake new file mode 100644 index 00000000..a84f7966 --- /dev/null +++ b/lib/tasks/resque.rake @@ -0,0 +1,14 @@ +require "resque/pool/tasks" + +task "resque:setup" => :environment + +task "resque:pool:setup" do + ActiveRecord::Base.connection_handler.clear_all_connections! + + # The pool master boots Rails and then forks each worker, so hand every child + # its own Redis and database connections instead of sharing the master's. + Resque::Pool.after_prefork do + Resque.redis.reconnect + ActiveRecord::Base.establish_connection + end +end