How to create a daily job (cron-like) in Rails ActiveJob?
Asked Answered
K

4

16

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?

Kedah answered 31/3, 2015 at 3:12 Comment(0)
T
16

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.

Transarctic answered 31/3, 2015 at 3:26 Comment(1)
So, ActiveJob is about queued jobs?Kedah
A
11

It's working for me:

every 1.day, at: '9:36 am' do
  runner 'SomeJob.perform_later'
end

I'm using whenever and ActiveJob

Ander answered 31/3, 2015 at 7:40 Comment(0)
O
2

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

Obstruction answered 9/8, 2021 at 14:16 Comment(0)
S
1

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

Siana answered 6/6, 2019 at 15:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.