Search code examples
ruby-on-railsrubycronwhenever

How to pass dynamic value in schedule.rb file ( Ruby on rails )


how I can pass dynamic value in regular expression

new_date_value = query_value
# minute(0-59), hours(0-23), day of month(1-31), month of year(1-12), day of week(0-6) 0=sunday

every '0 4 #{new_date_value} * *' do
   rake "db:get_free_tag_latest_post_batch", :environment => "production"
end

Solution

  • to avoid this kind of thing, what I usually do is check the date on the task. For example, I have a task that has to run every last day of the month. I can't use 30 or 31 on the day because Feb. has 28 days, for example.

    This is how I set-up the task

    # Apply interest accrued for all loans
    # every day at: '22:00'
    trigger_interest_accrued_application:
      cron: "0 22 * * *"
      class: "Jobs::TriggerInterestAccruedApplication"
      queue: admin
    

    and on the task definition

    class TriggerInterestAccruedApplication < ::ApplicationJob
      queue_as :admin
    
      def perform
        return unless Date.current.end_of_month.today?
        
        perform_task
      end
    end
    

    do you think something like this would work for you? IMO it is better because now what you want to do is put some logic on the scheduler file and this could probably bite you later