Skip to content

Commit

Permalink
Merge pull request #109 from mathiasmoser/fix-task-last-run-finished-…
Browse files Browse the repository at this point in the history
…too-late-logic

Fix wrong lastRunFinishedTooLate behaviour when lastStartedAt and lastFinishedAt are within a second because of a very fast task
  • Loading branch information
freekmurze authored Mar 28, 2024
2 parents 46b95ba + 4ab5606 commit 51e9f00
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/Support/ScheduledTasks/Tasks/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,9 @@ public function lastRunFinishedTooLate(): bool

$lastFinishedAt = $this->lastRunFinishedAt()
? $this->lastRunFinishedAt()
: $this->monitoredScheduledTask->created_at;

$expectedNextRunStart = $this->nextRunAt($lastFinishedAt->subSecond());
: $this->monitoredScheduledTask->created_at->subSecond();

$expectedNextRunStart = $this->nextRunAt($lastFinishedAt);
$shouldHaveFinishedAt = $expectedNextRunStart->addMinutes($this->graceTimeInMinutes());

return $shouldHaveFinishedAt->isPast();
Expand Down
8 changes: 8 additions & 0 deletions tests/Support/Tasks/LastRunFinishedTooLateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@
expect(createTask()->lastRunFinishedTooLate())->toBeTrue();
});

test('a task will be not consider too late if the last start and finished date are the same because the task executes pretty fast', function () {
expect(createTask()->lastRunFinishedTooLate())->toBeFalse();

$this->monitoredScheduledTask->update(['last_started_at' => now(), 'last_finished_at' => now()]);
TestTime::addMinutes(6);
expect(createTask()->lastRunFinishedTooLate())->toBeFalse();
});

it('will reset the period', function () {
expect(createTask()->lastRunFinishedTooLate())->toBeFalse();

Expand Down

0 comments on commit 51e9f00

Please sign in to comment.