Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Job runs daily rather following the days of the week provided #19

Closed
ACPK opened this issue Apr 1, 2017 · 5 comments
Closed

Comments

@ACPK
Copy link

ACPK commented Apr 1, 2017

I have the following setup in config/simple_scheduler.rb. We'd like the job to only be run M-F, and tried to configure it using the code below. Do you have any suggestions as it ran today (i.e. Saturday)?

# Global configuration options. These can also be set on each task.
queue_ahead: 360 # Number of minutes to queue jobs into the future

tz: "America/New_York" # The application time zone will be used by default if not set

# Runs once every day at 4:15 PM. The job will expire after 18 hours, which means the
# job will not run if 18 hours passes (server downtime) before the job is actually run
overnight_task:
  class: "DailyJob"
  every: "1.day"
  at: "[Mon|Tue|Wed|Thu|Fri] 20:00"
  expires_after: "23.hours"
@bmedenwald
Copy link
Contributor

This is similar to #17 in that we should probably change the documentation to reflect this use case better. There are two issues at play, one documentation and one a bug it would seem.

The at time dictates when this Daily job should start queuing, not only when it should run. For a daily job, adding a day of the week would only postpone the first run, not the subsequent runs. Adding a day of the week works great for weekly jobs. Your example job would run every day at 20:00. If you pick one day of the week, that will run your daily job starting on that day, but after that it would run every single day per the every option.

The second issue is with [Mon|Tue|Wed|Thu|Fri]. I can see how the documentation can read that this is valid but in fact it should read that these are the days that are acceptable options, but you should pick one day. The bug is that this string doesn't raise an InvalidTime error, which it should. Of course, we should also update the docs so that this doesn't appear possible right now.

To do what you're after, you'll want to create 5 tasks (name them differently as the reset_cache key is used to look up existing queued jobs) that run every 1.week starting on Mon, Tue, Wed, Thu, and Fri specifically. Like so:

overnight_mon_task:
  class: "DailyJob"
  every: "1.week"
  at: "Mon 20:00"
  expires_after: "23.hours"

overnight_tue_task:
  class: "DailyJob"
  every: "1.week"
  at: "Tue 20:00"
  expires_after: "23.hours"

overnight_wed_task:
  class: "DailyJob"
  every: "1.week"
  at: "Wed 20:00"
  expires_after: "23.hours"

overnight_thu_task:
  class: "DailyJob"
  every: "1.week"
  at: "Thu 20:00"
  expires_after: "23.hours"

overnight_fri_task:
  class: "DailyJob"
  every: "1.week"
  at: "Fri 20:00"
  expires_after: "23.hours"

@bmedenwald bmedenwald added the bug label Apr 1, 2017
@ACPK
Copy link
Author

ACPK commented Apr 1, 2017

Thanks for the awesome reply, as it's very much appreciated!

Almost all of our jobs should run only Monday-Friday. Hence, would you suggest using a check after the job runs to see what day of the week it is in order to reduce duplicate code?

@bmedenwald
Copy link
Contributor

That is 100% an option. If you remove the day of the week entirely, the job can run every day. Checking when it was scheduled is a breeze since your job can accept scheduled_time that is passed in. A one line guard clause to return if Sat or Sun would do the trick.

@ACPK
Copy link
Author

ACPK commented Apr 1, 2017

@bmedenwald - That solves that so problem so thank you! Since I'm using Rails 5, I used:

if scheduled_time.on_weekday?

@ACPK ACPK closed this as completed Apr 1, 2017
@bmedenwald
Copy link
Contributor

Awesome.

I'm gong to reopen this issue though to track a change I'm planning to better detect an invalid date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants