Files
projectsend/app/Modules/Platform/Scheduling/RecordsScheduledTaskRuns.php
T
ignacionelson 6e47d76ba6 ProjectSend 2.0.0
Client file sharing, rebuilt from the ground up: a private area per
client, resumable uploads, folders, groups and categories, sharing with
expiry dates and download limits, comments, file versions, an activity
log, a REST API, and sixteen languages.

This repository begins here. ProjectSend 2 was developed privately, and
that development history is not published — the previous generation
remains available, with its own history, at projectsend/legacy.

Free software under the GNU General Public License v2, or (at your
option) any later version.
2026-08-14 01:38:12 -03:00

52 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Platform\Scheduling;
use Illuminate\Console\Events\ScheduledTaskFailed;
use Illuminate\Console\Events\ScheduledTaskFinished;
use Illuminate\Console\Scheduling\Event as ScheduledEvent;
/**
* Records the outcome of every projectsend:* scheduled command into
* ScheduledTaskRun, purely from Laravel's own scheduler lifecycle events —
* no changes to any command class needed.
*
* ScheduleRunCommand dispatches ScheduledTaskFinished unconditionally right
* after a task runs (even one that's about to be judged a failure), and
* only afterwards — if the exit code was non-zero — throws and dispatches
* ScheduledTaskFailed. Two independent upserts, in that guaranteed order,
* naturally leave a failing run's row as "failed": Finished's "success"
* write lands first, and Failed's write overwrites it a moment later.
*/
class RecordsScheduledTaskRuns
{
public function onFinished(ScheduledTaskFinished $event): void
{
$this->record($event->task, TaskRunStatus::Success, null, (int) round($event->runtime * 1000));
}
public function onFailed(ScheduledTaskFailed $event): void
{
$this->record($event->task, TaskRunStatus::Failed, $event->exception->getMessage(), null);
}
private function record(ScheduledEvent $task, TaskRunStatus $status, ?string $message, ?int $durationMs): void
{
// $task->command is the full shelled-out string (php binary,
// artisan path, output redirection) — pull out just the artisan
// signature. Anything that isn't one of our own commands (there
// are none today, but a future package could schedule its own) is
// simply not tracked here.
if (! preg_match('/projectsend:[\w-]+/', (string) $task->command, $matches)) {
return;
}
ScheduledTaskRun::query()->updateOrCreate(
['command' => $matches[0]],
['status' => $status, 'message' => $message, 'duration_ms' => $durationMs, 'ran_at' => now()],
);
}
}