Correct logic for recurring alert rules that span UTC days (#14145)

* Correct logic for recurring alert rules that span UTC days. Evaluate the day of week in local time.

* Enable previously broken test cases

* Update TestScheduledMaintenance.php

Co-authored-by: Tony Murray <murraytony@gmail.com>
This commit is contained in:
Nash Kaminski
2022-08-18 20:20:25 -05:00
committed by GitHub
parent 7d3811a2b0
commit 2741612eb6
2 changed files with 9 additions and 8 deletions

View File

@@ -173,11 +173,12 @@ class AlertSchedule extends Model
{ {
return $query->where(function ($query) { return $query->where(function ($query) {
$now = CarbonImmutable::now('UTC'); $now = CarbonImmutable::now('UTC');
$localDayNum = CarbonImmutable::now()->format('N');
$query->where('start', '<=', $now) $query->where('start', '<=', $now)
->where('end', '>=', $now) ->where('end', '>=', $now)
->where(function ($query) use ($now) { ->where(function ($query) use ($now, $localDayNum) {
$query->where('recurring', 0) // Non recurring simply between start and end $query->where('recurring', 0) // Non recurring simply between start and end
->orWhere(function ($query) use ($now) { ->orWhere(function ($query) use ($now, $localDayNum) {
$query->where('recurring', 1) $query->where('recurring', 1)
// Check the time is after the start date and before the end date, or end date is not set // Check the time is after the start date and before the end date, or end date is not set
->where(function ($query) use ($now) { ->where(function ($query) use ($now) {
@@ -190,14 +191,14 @@ class AlertSchedule extends Model
// outside, spans days // outside, spans days
$query->whereTime('start', '>', DB::raw('time(`end`)')) $query->whereTime('start', '>', DB::raw('time(`end`)'))
->where(function ($query) use ($now) { ->where(function ($query) use ($now) {
$query->whereTime('end', '<=', $now->toTimeString()) $query->whereTime('start', '<=', $now->toTimeString())
->orWhereTime('start', '>', $now->toTimeString()); ->orWhereTime('end', '>', $now->toTimeString());
}); });
}); });
}) })
// Check we are on the correct day of the week // Check we are on the correct day of the week
->where(function ($query) use ($now) { ->where(function ($query) use ($localDayNum) {
$query->where('recurring_day', 'like', $now->format('%N%')) $query->where('recurring_day', 'like', "%${localDayNum}%")
->orWhereNull('recurring_day'); ->orWhereNull('recurring_day');
}); });
}); });

View File

@@ -48,8 +48,8 @@ class TestScheduledMaintenance extends DBTestCase
$this->assertScheduleActive(Carbon::parse('2020-09-10 2:00'), $schedule); $this->assertScheduleActive(Carbon::parse('2020-09-10 2:00'), $schedule);
$this->assertScheduleSet(Carbon::parse('2020-09-10 1:59'), $schedule); $this->assertScheduleSet(Carbon::parse('2020-09-10 1:59'), $schedule);
$this->assertScheduleActive(Carbon::parse('2020-09-10 19:59'), $schedule); $this->assertScheduleActive(Carbon::parse('2020-09-10 19:59'), $schedule);
// $this->assertScheduleSet(Carbon::parse('2020-09-10 20:01'), $schedule); // FIXME broken since end is 1am UTC $this->assertScheduleSet(Carbon::parse('2020-09-10 20:01'), $schedule);
// $this->assertScheduleSet(Carbon::parse('2020-09-11 01:00'), $schedule); $this->assertScheduleSet(Carbon::parse('2020-09-11 01:00'), $schedule);
$this->assertScheduleActive(Carbon::parse('2020-09-11 11:00'), $schedule); $this->assertScheduleActive(Carbon::parse('2020-09-11 11:00'), $schedule);
$this->assertScheduleSet(Carbon::parse('2020-09-12 11:00'), $schedule); $this->assertScheduleSet(Carbon::parse('2020-09-12 11:00'), $schedule);
$this->assertScheduleActive(Carbon::parse('2020-09-14 10:00'), $schedule); $this->assertScheduleActive(Carbon::parse('2020-09-14 10:00'), $schedule);