Question

I'd like one of my classes to only run on Tuesday,Wed,and Thursday. I have the following:

class ReminderJob

  def perform

    begin
        # Days to send: Tuesdays, Wednesdays, Thursdays
        next Date.today.wday.instance_of?('2,3,4')

The idea being if it is not Tues,Wed,Thur exist the class. Suggestions on how to solve this with ruby? Thanks

Was it helpful?

Solution

If you just want to have a task run only on certain days/times, you probably want to schedule a cron job.

If you actually want a method to do nothing unless it's a certain day of the week, you could do something like this:

return unless [ :tuesday?, :wednesday?, :thursday? ].any? { |day| Date.today.send(day) }

OTHER TIPS

Don't mix scheduling into the class itself. Let the class deal strictly with the logic of performing the task.

Use cron to do the scheduling. Use the whenever gem to create the cron job. You can specify that it should only execute on certain days of the week, certain times of day, etc.

Though your question isn't clear, here's the answer for the doubt I think you really have:

def perform
  return if [2,3,4].include? Date.today.wday
  puts "Your job!!!"
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top