0

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

Mladen Jablanović
  • 43,461
  • 10
  • 90
  • 113
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012

3 Answers3

2

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) }
pje
  • 21,801
  • 10
  • 54
  • 70
1

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.

Tom L
  • 3,389
  • 1
  • 16
  • 14
0

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
Alcides Queiroz
  • 9,456
  • 3
  • 28
  • 43