Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Models/Task.php
7432 views
1
<?php
2
3
namespace Pterodactyl\Models;
4
5
use Illuminate\Container\Container;
6
use Znck\Eloquent\Traits\BelongsToThrough;
7
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8
use Illuminate\Database\Eloquent\Factories\HasFactory;
9
use Pterodactyl\Contracts\Extensions\HashidsInterface;
10
11
/**
12
* @property int $id
13
* @property int $schedule_id
14
* @property int $sequence_id
15
* @property string $action
16
* @property string $payload
17
* @property int $time_offset
18
* @property bool $is_queued
19
* @property bool $continue_on_failure
20
* @property \Carbon\Carbon $created_at
21
* @property \Carbon\Carbon $updated_at
22
* @property string $hashid
23
* @property Schedule $schedule
24
* @property Server $server
25
*/
26
class Task extends Model
27
{
28
/** @use HasFactory<\Database\Factories\TaskFactory> */
29
use HasFactory;
30
use BelongsToThrough;
31
32
/**
33
* The resource name for this model when it is transformed into an
34
* API representation using fractal.
35
*/
36
public const RESOURCE_NAME = 'schedule_task';
37
38
/**
39
* The default actions that can exist for a task in Pterodactyl.
40
*/
41
public const ACTION_POWER = 'power';
42
public const ACTION_COMMAND = 'command';
43
public const ACTION_BACKUP = 'backup';
44
45
/**
46
* The table associated with the model.
47
*/
48
protected $table = 'tasks';
49
50
/**
51
* Relationships to be updated when this model is updated.
52
*/
53
protected $touches = ['schedule'];
54
55
/**
56
* Fields that are mass assignable.
57
*/
58
protected $fillable = [
59
'schedule_id',
60
'sequence_id',
61
'action',
62
'payload',
63
'time_offset',
64
'is_queued',
65
'continue_on_failure',
66
];
67
68
/**
69
* Cast values to correct type.
70
*/
71
protected $casts = [
72
'id' => 'integer',
73
'schedule_id' => 'integer',
74
'sequence_id' => 'integer',
75
'time_offset' => 'integer',
76
'is_queued' => 'boolean',
77
'continue_on_failure' => 'boolean',
78
];
79
80
/**
81
* Default attributes when creating a new model.
82
*/
83
protected $attributes = [
84
'time_offset' => 0,
85
'is_queued' => false,
86
'continue_on_failure' => false,
87
];
88
89
public static array $validationRules = [
90
'schedule_id' => 'required|numeric|exists:schedules,id',
91
'sequence_id' => 'required|numeric|min:1',
92
'action' => 'required|string',
93
'payload' => 'required_unless:action,backup|string',
94
'time_offset' => 'required|numeric|between:0,900',
95
'is_queued' => 'boolean',
96
'continue_on_failure' => 'boolean',
97
];
98
99
public function getRouteKeyName(): string
100
{
101
return $this->getKeyName();
102
}
103
104
/**
105
* Return a hashid encoded string to represent the ID of the task.
106
*/
107
public function getHashidAttribute(): string
108
{
109
return Container::getInstance()->make(HashidsInterface::class)->encode($this->id);
110
}
111
112
/**
113
* Return the schedule that a task belongs to.
114
*/
115
public function schedule(): BelongsTo
116
{
117
return $this->belongsTo(Schedule::class);
118
}
119
120
/**
121
* Return the server a task is assigned to, acts as a belongsToThrough.
122
*/
123
public function server(): \Znck\Eloquent\Relations\BelongsToThrough
124
{
125
return $this->belongsToThrough(Server::class, Schedule::class);
126
}
127
}
128
129