Files

81 lines
3.3 KiB
PHP
Raw Permalink Normal View History

2026-10-05 18:10:18 -04:00
<?php
declare(strict_types=1);
namespace Pterodactyl\Actions\Schedules;
use Illuminate\Support\Facades\DB;
2026-10-11 06:11:40 -04:00
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Contracts\Schedules\CreatesScheduleTasks;
2026-10-11 06:11:40 -04:00
use Pterodactyl\Exceptions\Http\HttpForbiddenException;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Exceptions\Service\ServiceLimitExceededException;
2026-10-11 06:11:40 -04:00
use Pterodactyl\Facades\Activity;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Schedule;
use Pterodactyl\Models\Task;
final readonly class CreateScheduleTask implements CreatesScheduleTasks
{
2026-10-11 06:11:40 -04:00
/**
2026-10-11 23:50:53 -04:00
* A power task's payload must name the power action to send.
2026-10-11 06:11:40 -04:00
*
* @return NormalizedValidationRules
*/
public static function rules(bool $power): array
{
$payload = ['required_unless:action,backup', 'string', 'nullable'];
if ($power) {
$payload[] = Rule::in(Task::POWER_ACTIONS);
}
return [
'action' => ['required', Rule::in([Task::ACTION_COMMAND, Task::ACTION_POWER, Task::ACTION_BACKUP])],
'payload' => $payload,
'time_offset' => ['required', 'integer', 'min:0', 'max:900'],
'sequence_id' => ['sometimes', 'required', 'integer', 'min:1'],
'continue_on_failure' => ['sometimes', 'required', 'boolean'],
];
}
public function create(Schedule $schedule, array $input): Task
2026-10-05 18:10:18 -04:00
{
2026-10-11 23:41:04 -04:00
$validated = Validator::make($input, self::rules(($input['action'] ?? null) === Task::ACTION_POWER))->safe();
throw_if($validated['action'] === Task::ACTION_BACKUP && $schedule->server->backup_limit === 0, HttpForbiddenException::class, "A backup task cannot be created when the server's backup limit is set to 0.");
$attributes = [
'action' => $validated['action'],
'payload' => $validated['payload'] ?? '',
'time_offset' => $validated['time_offset'],
'continue_on_failure' => $validated['continue_on_failure'] ?? false,
];
$requested = $validated->filled('sequence_id') ? $validated->integer('sequence_id') : null;
2026-10-11 06:11:40 -04:00
2026-10-11 23:41:04 -04:00
$task = DB::transaction(function () use ($schedule, $attributes, $requested): Task {
2026-10-05 18:10:18 -04:00
$schedule = $schedule->newQuery()->whereKey($schedule->getKey())->lockForUpdate()->firstOrFail();
2026-10-11 19:15:22 -04:00
$limit = config('pterodactyl.client_features.schedules.per_schedule_task_limit');
2026-10-05 18:10:18 -04:00
throw_if($schedule->tasks()->count() >= $limit, ServiceLimitExceededException::class, "Schedules may not have more than $limit tasks associated with them. Creating this task would put this schedule over the limit.");
$next = ($schedule->tasks()->orderByDesc('sequence_id')->first()->sequence_id ?? 0) + 1;
2026-10-11 23:41:04 -04:00
$requested ??= $next;
2026-10-05 18:10:18 -04:00
$sequenceId = $next;
if ($requested < $next) {
$schedule->tasks()
->where('sequence_id', '>=', $requested)
->increment('sequence_id');
$sequenceId = $requested;
}
2026-10-11 06:11:40 -04:00
return $schedule->tasks()->create([...$attributes, 'sequence_id' => $sequenceId]);
2026-10-05 18:10:18 -04:00
});
2026-10-11 06:11:40 -04:00
Activity::event('server:task.create')
2026-10-11 14:07:14 -04:00
->subject($schedule->server, $schedule, $task)
2026-10-11 06:11:40 -04:00
->property(['name' => $schedule->name, 'action' => $task->action, 'payload' => $task->payload])
->log();
return $task;
2026-10-05 18:10:18 -04:00
}
}