Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 20 additions & 3 deletions src/ScheduleEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
78 changes: 78 additions & 0 deletions tests/Unit/FlushFailureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

use Brain\Monkey\Functions;
use Pollora\Schedule\Drivers\ActionSchedulerDriver;
use Pollora\Schedule\Event;
use Pollora\Schedule\Exceptions\ScheduleException;
use Pollora\Schedule\ScheduleEngine;

require_once __DIR__.'/../Support/ActionSchedulerFake.php';

beforeEach(function (): void {
Functions\when('add_action')->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();
});
Loading