Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 346
luthfianto Avatar asked Mar 31 '15 03:03

luthfianto


4 Answers

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.

like image 78
joelparkerhenderson Avatar answered Oct 16 '22 10:10

joelparkerhenderson


It's working for me:

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

I'm using whenever and ActiveJob

like image 29
Fist Avatar answered Oct 16 '22 11:10

Fist


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

like image 1
Ben-Hur Batista Avatar answered Oct 16 '22 11:10

Ben-Hur Batista


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

like image 1
Haseeb A Avatar answered Oct 16 '22 09:10

Haseeb A