Scheduling tasks by reading time of execution from database instead of code

335 Views Asked by At

I am looking for a flow in ruby through which I can run a task daily on some particular times. I want to add a new task in some table, specify the time in some column and the task should be scheduled daily since the row has been added to the table. I know the gem 'whenever' is used for scheduling but it would require a code deployment whenever I need to specify the time in schedule.rb. Is it even possible what I want to do?

1

There are 1 best solutions below

0
widjajayd On

I think you can use crone gem this is purely writing in ruby, you do not need to set cron job like whenever gem. but how it work is cron in linux

installation steps:

gem 'crono'
rails generate crono:install
rake db:migrate # to install it's table

you can merge with active job

class TestJob # This is not an Active Job job, but pretty legal Crono job.
  def perform(*args)
    # put you scheduled code here
    # Comments.deleted.clean_up...
  end
end

and do the scheduling as follow (no need to set from OS / cron)

# config/cronotab.rb
Crono.perform(TestJob).every 2.days, at: {hour: 15, min: 30}
Crono.perform(TestJob).every 1.week, on: :monday, at: "15:30"