This week I made a refactor to remove a job (ActiveJob::Base) that was used only in recurring.yml, and call a class method directly instead.
Sometimes it is ok to have a job, but sometimes using the method can help you write a little less code. And these days it is just a prompt away.
I was working on a follow-up flow for users who had not updated their profile in over a year. The recurring task only needed to fan out work: find eligible users and enqueue one job per user.
Instead of scheduling a job in recurring.yml, like this…
follow_up_main_info_review:
class: "User::ScheduleMainInfoReviewFollowUpsJob"
schedule: "every day at 09:00"
With a thin wrapper job like this…
class User::ScheduleMainInfoReviewFollowUpsJob < ApplicationJob
def perform
User::MainInfoReview::FollowUp.schedule
end
end
You can call a class method directly, like this…
follow_up_main_info_review:
command: "User::MainInfoReview::FollowUp.schedule"
schedule: "every day at 09:00"
And keep the logic in a plain Ruby object:
class User::MainInfoReview::FollowUp
def self.schedule
User.needing_main_info_review.find_each do |user|
User::MainInfoReview::FollowUpJob.perform_later(user)
end
end
# ...
end
Solid Queue runs the command on a recurring job. No custom scheduler job needed.
By default, that recurring job goes to the solid_queue_recurring queue, so you need a worker for it in queue.yml:
workers:
- queues: [ solid_queue_recurring ]
You can also set a different queue per task in recurring.yml. For example, if you want it on my_special_queue:
follow_up_main_info_review:
command: "User::MainInfoReview::FollowUp.schedule"
schedule: "every day at 09:00"
queue: my_special_queue
If a job’s only job is to call one method from recurring.yml, try command: first.
For more details you can read the Solid Queue recurring tasks docs.
Here I try to share knowledge and fixes to common problems and struggles for ruby on rails developers, like How to fetch the latest-N-of-each record or How to test that an specific mail was sent or a Capybara cheatsheet. You can see more examples on Most recent posts or all posts.