dbader / schedule

Python job scheduling for humans.
https://schedule.readthedocs.io/
MIT License
11.62k stars 954 forks source link

Weekly job fails with interval > 1 #613

Open maxnyman opened 5 months ago

maxnyman commented 5 months ago

Some assumptions are made in the code, which breaks if the interval is > 1.

Attached some unit test to expose the error: scheduler_weeks_test.txt

To fix:

In _schedule_next_run replace the lines from starting with if days_ahead <= 0: with the following

            if days_ahead < 0:  # Target day already happened this week
                days_ahead += 7
            if self.last_run is None:
                self.next_run += datetime.timedelta(days_ahead) - self.period
            elif self.last_run + self.period < datetime.datetime.now():
                self.next_run += datetime.timedelta(days_ahead) - datetime.timedelta(days=7)

And a bit further down, at the end of the function, remove the following completely

        if self.start_day is not None and self.at_time is not None:
            # Let's see if we will still make that time we specified today
            if (self.next_run - datetime.datetime.now()).days >= 7:
                self.next_run -= self.period