I'm aware of this thread: A cron job for rails: best practices?, but there's no mention of ActiveJob. My motivation to do it with ActiveJob is because it's built-in in Rails and here's an excerpt from its docs:
"These jobs can be everything from regularly scheduled clean-ups, to billing charges, to mailings."
How do I create a daily job (cron-like) in Rails ActiveJob? Since I don't see the example to run a regularly scheduled job in its docs.
Or should I stick with the whenever
gem?
Stick with the whenever
gem or similar gem e.g. chrono
, clockwork
, rufus-scheduler
.
What you're reading in the ActiveJob documentation is a bit confusing, because it could seem as if ActiveJob may be able handle the responsibility of regular scheduling. What the documentation should say IMHO is that the jobs are regularly scheduled by some other system or tool.
So, ActiveJob is about queued jobs?
Yes, it's about Rails providing a standard interface for adding a job to a queue, and calling a perform method. ActiveJob provides the method interfaces that enable adapters for many job-processing queues, backends, immediate runners, etc.
It's working for me:
every 1.day, at: '9:36 am' do
runner 'SomeJob.perform_later'
end
I'm using whenever
and ActiveJob
If you dont want to add a cron job, can put in the end of job a call to repeat the same job 1 day after the first execution
class SomeJob < ApplicationJob
def perform(*args)
#execution here...
SomeJob.set(wait_until: Time.now + 1.day).perform_later(...)
end
end
I know that is not a good pratice
If you are using sidekiq
adapter for active job. sidekiq-cron
has built in support for activejob. So you can use it directly. https://github.com/ondrejbartas/sidekiq-cron
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With