2016-02-27 18:35:12 -05:00
|
|
|
<?php
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2016-02-27 18:35:12 -05:00
|
|
|
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;
|
2016-02-27 18:35:12 -05:00
|
|
|
|
2020-02-08 15:23:08 -08:00
|
|
|
/**
|
2021-01-27 20:52:11 -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
|
2021-05-01 10:44:40 -07:00
|
|
|
* @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'])]
|
2020-04-03 23:22:35 -07:00
|
|
|
class Task extends Model
|
2016-02-27 18:35:12 -05:00
|
|
|
{
|
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
|
|
|
|
2018-01-25 21:26:06 -06: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';
|
2018-01-25 21:26:06 -06:00
|
|
|
|
2021-05-01 10:44:40 -07:00
|
|
|
/**
|
|
|
|
|
* 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';
|
2021-05-01 10:44:40 -07:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
public const string ACTION_COMMAND = 'command';
|
2016-02-27 18:35:12 -05:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
public const string ACTION_BACKUP = 'backup';
|
2017-09-12 23:45:19 -05:00
|
|
|
|
|
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* The signals a power task may send.
|
|
|
|
|
*
|
|
|
|
|
* @var list<string>
|
2017-09-12 23:45:19 -05:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public const array POWER_ACTIONS = ['start', 'stop', 'restart', 'kill'];
|
2016-02-27 18:35:12 -05:00
|
|
|
|
2017-09-09 23:55:21 -05:00
|
|
|
/**
|
|
|
|
|
* Default attributes when creating a new model.
|
|
|
|
|
*/
|
|
|
|
|
protected $attributes = [
|
2020-03-18 22:28:32 -07:00
|
|
|
'time_offset' => 0,
|
2017-09-12 23:45:19 -05:00
|
|
|
'is_queued' => false,
|
2021-05-01 10:44:40 -07:00
|
|
|
'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
|
|
|
/**
|
2017-09-12 23:45:19 -05:00
|
|
|
* Return the schedule that a task belongs to.
|
2025-12-20 15:55:13 -08:00
|
|
|
*
|
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
|
|
|
{
|
2017-09-12 23:45:19 -05: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.
|
2025-12-20 15:55:13 -08:00
|
|
|
*
|
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
|
|
|
}
|
2016-02-27 18:35:12 -05:00
|
|
|
}
|