Rails - Whenever Gem - Dynamic Values

Rails - Whenever gem - Dynamic values

I've never tried, but you should be able to do this by loading up the Rails environment in whenever so that you can use your model classes. Just use require File.dirname(__FILE__) + "./environment" (or "./application" for Rails 3) in your schedule.rb, assuming your schedule.rb is in the config dir.

However, since all whenever does is generate lines in the crontab, any changes made to any Constant would require running whenever --update-crontab again.

whenever gem schedule.rb file: doesn't recognize RAILS_ROOT variable

Whenever doesn't require or depend on Rails at all, so when it runs, RAILS_ROOT is not defined, however because whenever's schedule.rb is generally kept in /config/schedule.rb, we can make an assumption that it is in a rails project, and set our own RAILS_ROOT like this:

# in schedule.rb
RAILS_ROOT = File.dirname(__FILE__) + '/..'

Edit: in the case that you actually need Rails loaded, do this:

# in schedule.rb
# this will require config/environment and load your entire rails environment
require File.expand_path(File.dirname(__FILE__) + "/environment")

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

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

Whenever gem controller method not working

That's because Reset.reset trys to call the reset method on the Reset class. That is, Reset.reset trys to call a class method.

Your reset method is an instance method. To define a Reset.reset class method, use:

class Reset
def self.reset
logger.debug("This is the cron job")
end
end

Also, you'll need to make sure that you have a self.logger class method as well, or that code will just die.

Lastly, for your own edification: Reset as you have it written isn't a controller. It's just a plain old ruby object.



Related Topics



Leave a reply



Submit