From 6691b44bdce59e08e1021c950cba05711c73b88f Mon Sep 17 00:00:00 2001 From: Pollora maintainer Date: Thu, 3 Dec 2026 17:13:49 +0100 Subject: [PATCH] fix(engine): keep flushing buffered events after one fails ScheduleEngine::flush() emptied its buffer, then stopped at the first event that threw: every event after it was dropped, on each request. Each event is now tried, and the failures are thrown together as a ScheduleException. --- CHANGELOG.md | 4 ++ src/ScheduleEngine.php | 23 ++++++++-- tests/Unit/FlushFailureTest.php | 78 +++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 tests/Unit/FlushFailureTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 584bde3..e6a9492 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- `ScheduleEngine::flush()`, which hands over what was scheduled before the driver was ready (on `action_scheduler_init` with Action Scheduler), stopped at the first event that failed: the ones after it were dropped, and as events are declared again in the same order on every request, they were never scheduled while the failing one stayed. Every event is now tried; the failures are then thrown together as one `ScheduleException` that names the hooks and keeps the first failure as its previous exception. An exception of another class raised by a driver now comes out wrapped in it. + ### Changed - Development only, nothing to do on a project: require the Patchwork commit that fixes directory iteration (antecedent/patchwork#235) until it is released, so a mutation score is not computed from broken test runs. diff --git a/src/ScheduleEngine.php b/src/ScheduleEngine.php index 98da2b6..641fa34 100644 --- a/src/ScheduleEngine.php +++ b/src/ScheduleEngine.php @@ -704,19 +704,36 @@ public function index(): array * An event already there is not scheduled again: before the driver is ready, * a plugin that checks wp_next_scheduled() before it schedules sees nothing * and schedules on every request. + * + * An event that fails does not stop the others: each is tried, then the + * failures are reported together. A failed event is not kept: declaring it + * again, on the next request, is what retries it. + * + * @throws ScheduleException When one or more events could not be scheduled; its previous exception is the first failure */ public function flush(): void { $events = $this->buffer; $this->buffer = []; + $failures = []; + $first = null; foreach ($events as $event) { - $driver = $this->driverFor($event->hook, $event->args); + try { + $driver = $this->driverFor($event->hook, $event->args); - if ($driver->next($event->hook, $event->args) === null) { - $this->give($driver, $event); + if ($driver->next($event->hook, $event->args) === null) { + $this->give($driver, $event); + } + } catch (\Throwable $exception) { + $failures[] = sprintf("'%s' (%s)", $event->hook, $exception->getMessage()); + $first ??= $exception; } } + + if ($first instanceof \Throwable) { + throw new ScheduleException('Buffered events could not be scheduled: '.implode('; ', $failures), previous: $first); + } } private function give(ScheduleDriver $driver, Event $event): bool diff --git a/tests/Unit/FlushFailureTest.php b/tests/Unit/FlushFailureTest.php new file mode 100644 index 0000000..d545f2f --- /dev/null +++ b/tests/Unit/FlushFailureTest.php @@ -0,0 +1,78 @@ +justReturn(true); + Functions\when('_get_cron_array')->justReturn([]); + Functions\when('wp_get_schedules')->justReturn(['daily' => ['interval' => 86400, 'display' => 'Once Daily']]); + + $this->engine = new ScheduleEngine(new ActionSchedulerDriver); + $this->time = time() + 3600; + + // Scheduled before 'action_scheduler_init': the engine keeps them + ActionScheduler::$initialized = false; +}); + +function storedHooks(): array +{ + return array_values(array_map(static fn (FakeAction $action): string => $action->hook, ActionScheduler_FakeStore::$actions)); +} + +it('schedules the buffered events that follow one the driver refuses, then says which failed', function (): void { + $this->engine->schedule(new Event('first', $this->time)); + $this->engine->schedule(new Event('broken', $this->time, 'fortnightly')); + $this->engine->schedule(new Event('last', $this->time)); + + ActionScheduler::$initialized = true; + $thrown = null; + + try { + $this->engine->flush(); + } catch (ScheduleException $exception) { + $thrown = $exception; + } + + expect(storedHooks())->toBe(['first', 'last']) + ->and($thrown?->getMessage())->toBe("Buffered events could not be scheduled: 'broken' (Action Scheduler: the event 'broken' has no known recurrence 'fortnightly'.)") + ->and($thrown?->getPrevious()?->getMessage())->toBe("Action Scheduler: the event 'broken' has no known recurrence 'fortnightly'."); +}); + +it('names every failed event, with the first failure as the previous exception', function (): void { + $this->engine->schedule(new Event('broken', $this->time, 'fortnightly')); + $this->engine->schedule(new Event('fine', $this->time)); + $this->engine->schedule(new Event('worse', $this->time, 'never')); + + ActionScheduler::$initialized = true; + $thrown = null; + + try { + $this->engine->flush(); + } catch (ScheduleException $exception) { + $thrown = $exception; + } + + expect(storedHooks())->toBe(['fine']) + ->and($thrown?->getMessage())->toBe("Buffered events could not be scheduled: 'broken' (Action Scheduler: the event 'broken' has no known recurrence 'fortnightly'.); 'worse' (Action Scheduler: the event 'worse' has no known recurrence 'never'.)") + ->and($thrown?->getPrevious()?->getMessage())->toContain("'broken'"); +}); + +it('does not keep a failed event as pending, nor try it again', function (): void { + $this->engine->schedule(new Event('broken', $this->time, 'fortnightly')); + + ActionScheduler::$initialized = true; + + expect(fn () => $this->engine->flush())->toThrow(ScheduleException::class) + ->and($this->engine->next('broken'))->toBeNull(); + + // Throws again if the event was kept + $this->engine->flush(); +});