I made an app to help track my kids’ chores and responsibilities (yes, I am that kind of dad). We’ve got an old iPad mounted in the kitchen that stays on a dashboard screen, showing outstanding tasks and reminders.
One of my favourite features is the weekly schedule builder. My wife and I can quickly create recurring chores—but those tasks don’t get created automatically. Some pages in the app create missing tasks on page load, which was easy to implement at first. But since the dashboard uses Turbo Streams and doesn’t refresh constantly, it’s usually out of date by morning.
I always intended to generate tasks in the background. So let’s walk through setting that up using Solid Queue.
Add the gem and install it:
bundle add solid_queue
bin/rails solid_queue:install
I’m using Postgres in both development and production, so I had to add an extra database connection for Solid Queue.
Also, I needed to move the main connection under a primary key:
development:
primary:
<<: *default
database: irl_game_development
queue:
<<: *default
database: irl_game_development_queue
migrations_paths: db/queue_migrate
production:
primary:
<<: *default
url: <%= ENV["DATABASE_URL"] %>
queue:
<<: *default
# this will be based on your environment
url: <%= ENV["RED_DATABASE_URL"] %>
migrations_paths: db/queue_migrate
Solid Queue needs a separate process to run jobs. You can do this by setting SOLID_QUEUE_IN_PUMA
in your environment or your Procfile.dev
, but I just hardcoded it to run in development:
# config/puma.rb
plugin :solid_queue if ENV["SOLID_QUEUE_IN_PUMA"] || Rails.env.development?
By default, solid_queue:install
enables it only in production. I want to use it in development too:
# config/environments/development.rb
Rails.application.configure do
# ...
config.active_job.queue_adapter = :solid_queue
config.solid_queue.connects_to = {
database: { writing: :queue }
}
# ...
end
That :queue
reference matches the queue
block in database.yml
—it tells Solid Queue where to store job data.
Finally, I configured my production system (I use Hatchbox) to run Solid Queue as a separate process. I just added a process that runs:
bin/jobs
Now that background jobs are up and running, we’re ready to use them to create scheduled tasks automatically.
But that’ll have to wait for the next post—this one’s long enough already.