Intro
Welcome to this series of blog posts where I’ll guide you through getting comfortable with Solid Queue. The goal is to cover the essential aspects, providing you with a comprehensive reference you can revisit whenever you have questions.
Let’s get started!
Let’s get started!
Initial setup
Lets add Solid Queue to our Gemfile
gem "solid_queue", "~> 0.4.1"
Then, let's install the gem with bundler
bundle install
Good, now we are going to setup the migrations and configurations
rails generate solid_queue:install
Now, run the migrations
rails db:migrate
The configuration step, will update your config/environments/production.rb file changing the queue adapter to :solid_queue. Let's also update the development file to use it.
Open your config/environments/development.rb file and add the following line
config.active_job.queue_adapter = :solid_queue
Congratulations, you have the initial setup finished. Now, you are able to enqueue jobs with Solid Queue, however, you need to start Solid Queue's supervisor to execute these jobs.
bundle exec rake solid_queue:start
Optionally, you can choose specific jobs to use Solid Queue.
# app/jobs/my_job.rb class MyJob < ApplicationJob self.queue_adapter = :solid_queue # ... end
Important things to know
- Integration with Active Job: it seamlessly integrates with Active Job, utilising its capabilities for job retries, discarding, error handling, serialisation, and delays.
- Job Scheduling: it allows for scheduling jobs to run at specific times or after certain intervals using the set method with wait or wait_until options. For complex scheduling, integration with gems like "whenever" is also possible.
- Mission Control - Jobs: you can monitor and manage your background jobs using the Mission Control - Jobs tool, which provides a user-friendly interface for tracking job status and performance.
- Independent Operation: unlike many other queue systems, Solid Queue does not depend on external services such as Redis. This independence simplifies the setup and maintenance of your job queue system.
Basic differences between Active Job and Sidekiq
Enqueuing Jobs
Active Job:
Active Job:
MyJob.perform_later(args)
Sidekiq:
MySidekiqWorker.perform_async(args)
Specifying Queues
Active Job:
class MyJob < ApplicationJob queue_as :high_priority end
Sidekiq:
class MySidekiqWorker include Sidekiq::Worker sidekiq_options queue: 'high_priority' end
Customizing Job Execution Time
Active Job (scheduling a job to run after 10 minutes):
MyJob.set(wait: 10.minutes).perform_later(args)
Sidekiq (scheduling a job to run after 10 minutes):
MySidekiqWorker.perform_in(10.minutes, args)
Send welcome emails asynchronously upon user registration
Generate a mailer for sending welcome emails
rails generate mailer UserMailer welcome_email
This will create a user_mailer.rb in app/mailers and a view for the email in app/views/user_mailer.
Update the app/views/user_mailer/welcome_email.html.erb with your welcome message
Let's now generate a new job for that
Update the app/views/user_mailer/welcome_email.html.erb with your welcome message
Let's now generate a new job for that
rails generate job WelcomeEmail
Modify welcome_email_job.rb to use Solid Queue and send the email:
class WelcomeEmailJob < ApplicationJob queue_as :default def perform(user_id) user = User.find(user_id) UserMailer.welcome_email(user).deliver_now end end
In the part of your application where users are registered (e.g., in a controller after a successful sign-up), enqueue the welcome email job:
WelcomeEmailJob.perform_later(user.id)
Ensure Solid Queue's supervisor is running to execute the enqueued jobs:
bundle exec rake solid_queue:start
Conclusion
In conclusion, I wanted to share how you can embark on your journey with Solid Queue, highlighting the key steps and essential insights needed to get started. I hope you found this guide helpful and engaging. Stay tuned for more posts where I'll dive deeper into Solid Queue and its capabilities.