mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-19 21:04:44 +02:00

* Fix these * Update phpstan * Transform these into their identifiers instead * Fix custom rule * License is wrong * Update these * Pint fixes * Fix this * Consolidate these * Never supported PHP 7 * Better evaluation * Fixes * Don’t need ignore * Replace trait with service * Subusers are simply the many to many relationship between Servers and Users * Adjust to remove ignores * Use new query builder instead! * wip * Update composer * Quick fixes * Use realtime facade * Small fixes * Convert to static to avoid new * Update to statics * Don’t modify protected properties directly * Run pint * Change to correct method * Give up and use the facade * Make sure this route is available * Filament hasn’t been loaded yet * This can be readonly * Typehint * These are no longer used * Quick fixes * Need doc block help * Always true * We use caddy with docker * Pint * Fix phpstan issues * Remove unused import --------- Co-authored-by: MartinOscar <40749467+RMartinOscar@users.noreply.github.com>
74 lines
2.5 KiB
PHP
74 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Schedules;
|
|
|
|
use App\Models\Task;
|
|
use Exception;
|
|
use App\Models\Schedule;
|
|
use Illuminate\Contracts\Bus\Dispatcher;
|
|
use App\Jobs\Schedule\RunTaskJob;
|
|
use Illuminate\Database\ConnectionInterface;
|
|
use App\Exceptions\DisplayException;
|
|
use App\Repositories\Daemon\DaemonServerRepository;
|
|
|
|
class ProcessScheduleService
|
|
{
|
|
public function __construct(private ConnectionInterface $connection, private Dispatcher $dispatcher, private DaemonServerRepository $serverRepository) {}
|
|
|
|
/**
|
|
* Process a schedule and push the first task onto the queue worker.
|
|
*/
|
|
public function handle(Schedule $schedule, bool $now = false): void
|
|
{
|
|
/** @var ?Task $task */
|
|
$task = $schedule->tasks()->orderBy('sequence_id')->first();
|
|
|
|
if (!$task) {
|
|
throw new DisplayException('Cannot process schedule for task execution: no tasks are registered.');
|
|
}
|
|
|
|
$this->connection->transaction(function () use ($schedule, $task) {
|
|
$schedule->forceFill([
|
|
'is_processing' => true,
|
|
'next_run_at' => $schedule->getNextRunDate(),
|
|
])->saveOrFail();
|
|
|
|
$task->update(['is_queued' => true]);
|
|
});
|
|
|
|
$job = new RunTaskJob($task, $now);
|
|
if ($schedule->only_when_online) {
|
|
// Check that the server is currently in a starting or running state before executing
|
|
// this schedule if this option has been set.
|
|
try {
|
|
$details = $this->serverRepository->setServer($schedule->server)->getDetails();
|
|
$state = $details['state'] ?? 'offline';
|
|
// If the server is stopping or offline just do nothing with this task.
|
|
if (in_array($state, ['offline', 'stopping'])) {
|
|
$job->failed();
|
|
|
|
return;
|
|
}
|
|
} catch (Exception) {
|
|
$job->failed();
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (!$now) {
|
|
$this->dispatcher->dispatch($job->delay($task->time_offset));
|
|
} else {
|
|
// When using dispatchNow the RunTaskJob::failed() function is not called automatically
|
|
// so we need to manually trigger it and then continue with the exception throw.
|
|
try {
|
|
$this->dispatcher->dispatchNow($job);
|
|
} catch (Exception $exception) {
|
|
$job->failed();
|
|
|
|
throw $exception;
|
|
}
|
|
}
|
|
}
|
|
}
|