Search code examples
ruby-on-railsrubydatetimeruby-on-rails-4delayed-job

Rails - Specifying days when not to run a command


I have a method that is run via delayed_job. There is a field where the user inputs the days they do not want the task to run on e.g

days_off = ["Wednesday","Thursday"]

whenever a the method is run I want to check if todays day is in the 'days_off' array and if so then add 24hrs to the delayed job

if days_off.include? Time.now.strftime("%A")
  Call.delay(run_at: (DateTime.now + 24.hours )).my_method(c)
else
  Call.my_method(c)
end

however I'm not sure how to check the days_off array for consecutive days, i.e in the example above the method should have a delay of 48hrs as it shouldn't be run on a wednesday (today) or a thursday


Solution

  • I would start with something like this:

    days_off = ["Wednesday","Thursday"]
    
    if days_off.include? Time.now.strftime("%A")
      one_day = 60 * 60 * 24
      next_run = Time.now
      while days_off.include? next_run.strftime("%A")
        next_run = next_run + one_day
      end
      puts "call.delay() with next_run goes here"
    else
      puts "call.my_method() goes here"
    end