Files
pterodactyl-panel/app/Models/Task.php
T

139 lines
3.7 KiB
PHP
Raw Normal View History

<?php
2016-12-07 22:46:38 +00:00
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Models;
2026-10-05 18:10:18 -04:00
use Carbon\Carbon;
use Database\Factories\TaskFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Touches;
2024-10-21 19:18:20 -06:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2026-10-05 18:10:18 -04:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Pterodactyl\Enum\Permissions;
use Pterodactyl\Models\Traits\HasHashid;
2020-02-08 15:23:08 -08:00
/**
* @property int $id
* @property int $schedule_id
* @property int $sequence_id
* @property string $action
* @property string $payload
* @property int $time_offset
* @property bool $is_queued
* @property bool $continue_on_failure
2026-10-05 18:10:18 -04:00
* @property Carbon $created_at
* @property Carbon $updated_at
2024-10-21 19:18:20 -06:00
* @property Schedule $schedule
* @property Server $server
2020-02-08 15:23:08 -08:00
*/
2026-10-05 18:10:18 -04:00
#[Fillable([
'schedule_id',
'sequence_id',
'action',
'payload',
'time_offset',
'is_queued',
'continue_on_failure',
])]
#[Touches(['schedule'])]
class Task extends Model
{
2026-10-05 18:10:18 -04:00
/** @use HasFactory<TaskFactory> */
2024-10-21 19:18:20 -06:00
use HasFactory;
2026-10-05 18:10:18 -04:00
use HasHashid;
2017-09-09 23:55:21 -05:00
/**
* The resource name for this model when it is transformed into an
* API representation using fractal.
*/
2026-10-05 18:10:18 -04:00
public const string RESOURCE_NAME = 'schedule_task';
/**
* The default actions that can exist for a task in Pterodactyl.
*/
2026-10-05 18:10:18 -04:00
public const string ACTION_POWER = 'power';
2026-10-05 18:10:18 -04:00
public const string ACTION_COMMAND = 'command';
2026-10-05 18:10:18 -04:00
public const string ACTION_BACKUP = 'backup';
/**
2026-10-05 18:10:18 -04:00
* The signals a power task may send.
*
* @var list<string>
*/
2026-10-05 18:10:18 -04:00
public const array POWER_ACTIONS = ['start', 'stop', 'restart', 'kill'];
2017-09-09 23:55:21 -05:00
/**
* Default attributes when creating a new model.
*/
protected $attributes = [
'time_offset' => 0,
'is_queued' => false,
'continue_on_failure' => false,
2017-09-09 23:55:21 -05:00
];
2026-10-05 18:10:18 -04:00
/**
* @return array<string, string>
*/
protected function casts(): array
2022-05-22 14:10:01 -04:00
{
2026-10-05 18:10:18 -04:00
return [
'id' => 'integer',
'schedule_id' => 'integer',
'sequence_id' => 'integer',
'time_offset' => 'integer',
'is_queued' => 'boolean',
'continue_on_failure' => 'boolean',
];
2022-05-22 14:10:01 -04:00
}
2017-09-09 23:55:21 -05:00
/**
2026-10-05 18:10:18 -04:00
* The permission a user needs to run a task with the given action and payload, or
* null when the action or power signal is not one a task can perform.
2017-09-09 23:55:21 -05:00
*/
2026-10-05 18:10:18 -04:00
public static function permissionForAction(string $action, ?string $payload = null): ?Permissions
{
return match ($action) {
self::ACTION_COMMAND => Permissions::ControlConsole,
self::ACTION_BACKUP => Permissions::BackupCreate,
self::ACTION_POWER => match (mb_trim((string) $payload)) {
'start' => Permissions::ControlStart,
'stop', 'kill' => Permissions::ControlStop,
'restart' => Permissions::ControlRestart,
default => null,
},
default => null,
};
}
public function getRouteKeyName(): string
2017-09-09 23:55:21 -05:00
{
2026-10-05 18:10:18 -04:00
return $this->getKeyName();
2017-09-09 23:55:21 -05:00
}
2017-04-15 13:52:43 -04:00
/**
* Return the schedule that a task belongs to.
*
2026-10-05 18:10:18 -04:00
* @return BelongsTo<Schedule, $this>
2017-04-15 13:52:43 -04:00
*/
2022-10-14 10:59:20 -06:00
public function schedule(): BelongsTo
2017-04-15 13:52:43 -04:00
{
return $this->belongsTo(Schedule::class);
2017-04-15 13:52:43 -04:00
}
/**
2026-10-05 18:10:18 -04:00
* The server a task runs against is whatever its schedule belongs to, so this
* resolves through the schedule with local keys.
*
2026-10-05 18:10:18 -04:00
* @return HasOneThrough<Server, Schedule, $this>
2017-04-15 13:52:43 -04:00
*/
2026-10-05 18:10:18 -04:00
public function server(): HasOneThrough
2017-09-09 23:55:21 -05:00
{
2026-10-05 18:10:18 -04:00
return $this->hasOneThrough(Server::class, Schedule::class, 'id', 'id', 'schedule_id', 'server_id');
2017-09-09 23:55:21 -05:00
}
}