How to use Solid Queue in Rails
Enhance your Rails application’s queuing capabilities effortlessly with Solid Queue, without the need for Redis — this gem seamlessly runs on your app’s database.
Using Solid Queue in Rails is easy and efficient. This DB-based queuing backend for Active Job prioritizes simplicity and performance. With features like delayed jobs, concurrency controls, pausing queues, numeric priorities, and bulk enqueuing, Solid Queue proves to be a versatile choice.
To integrate Solid Queue into your Rails project, follow these simple steps:
Add the following line to your Gemfile:
gem "solid_queue"
Execute
$ bundle
Install the necessary migrations and configure the Active Job’s adapter with the provided generator:
$ rails generate solid_queue:install
This sets Solid Queue as the Active Job’s adapter in production and copies the required migrations to your application.
Alternatively, if you prefer, you can add only the migrations to your app:
$ bin/rails solid_queue:install:migrations
Manually set Solid Queue as your Active Job’s queue backend in your environment config:
# config/environments/production.rb
# config/environments/development.rb
config.active_job.queue_adapter = :solid_queue
Run the generated migrations:
$ rails db:migrate
Solid Queue automatically searches for your configuration in config/solid_queue.yml
. Ensure you create this file in your project directory. While we won’t be editing this file in this post, it’s a necessary component for Solid Queue to function properly.
Now, you’re all set to enqueue jobs using Solid Queue. To start processing jobs in all queues with the default configuration, run:
$ bundle exec rake solid_queue:start
If opting not to run Solid Queue as a separate process — allowing the use of a single dyno on Heroku for both your server and jobs — you can achieve this by integrating the Puma plugin. Simply add the following line to your config/puma.rb
file:
plugin :solid_queue
Solid Queue supports integration with SQL databases like MySQL, PostgreSQL, or SQLite. It optimizes performance by utilizing the FOR UPDATE SKIP LOCKED clause (when available) to prevent blocking and waiting on locks during job polling. The functionality of Solid Queue is complemented by Active Job, handling retries, discarding, error management, serialization, and delays. Additionally, it seamlessly aligns with Ruby on Rails multi-threading capabilities.
Exciting enhancements are on the horizon for Solid Queue, including upgrades to logging and instrumentation, a revamped CLI tool, and the option to operate within an existing process in “async” mode. Stay tuned for the introduction of unique job functionalities and the integration of recurring, cron-like tasks — coming your way shortly. For the latest updates on these advancements and more in the world of technology, follow me on Medium to stay in the loop!