Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Models/Schedule.php
7432 views
1
<?php
2
3
namespace Pterodactyl\Models;
4
5
use Cron\CronExpression;
6
use Carbon\CarbonImmutable;
7
use Illuminate\Container\Container;
8
use Illuminate\Database\Eloquent\Relations\HasMany;
9
use Illuminate\Database\Eloquent\Relations\BelongsTo;
10
use Illuminate\Database\Eloquent\Factories\HasFactory;
11
use Pterodactyl\Contracts\Extensions\HashidsInterface;
12
13
/**
14
* @property int $id
15
* @property int $server_id
16
* @property string $name
17
* @property string $cron_day_of_week
18
* @property string $cron_month
19
* @property string $cron_day_of_month
20
* @property string $cron_hour
21
* @property string $cron_minute
22
* @property bool $is_active
23
* @property bool $is_processing
24
* @property bool $only_when_online
25
* @property \Carbon\Carbon|null $last_run_at
26
* @property \Carbon\Carbon|null $next_run_at
27
* @property \Carbon\Carbon $created_at
28
* @property \Carbon\Carbon $updated_at
29
* @property string $hashid
30
* @property Server $server
31
* @property \Pterodactyl\Models\Task[]|\Illuminate\Support\Collection $tasks
32
*/
33
class Schedule extends Model
34
{
35
/** @use HasFactory<\Database\Factories\ScheduleFactory> */
36
use HasFactory;
37
38
/**
39
* The resource name for this model when it is transformed into an
40
* API representation using fractal.
41
*/
42
public const RESOURCE_NAME = 'server_schedule';
43
44
/**
45
* The table associated with the model.
46
*/
47
protected $table = 'schedules';
48
49
/**
50
* Always return the tasks associated with this schedule.
51
*/
52
protected $with = ['tasks'];
53
54
/**
55
* Mass assignable attributes on this model.
56
*/
57
protected $fillable = [
58
'server_id',
59
'name',
60
'cron_day_of_week',
61
'cron_month',
62
'cron_day_of_month',
63
'cron_hour',
64
'cron_minute',
65
'is_active',
66
'is_processing',
67
'only_when_online',
68
'last_run_at',
69
'next_run_at',
70
];
71
72
protected $casts = [
73
'id' => 'integer',
74
'server_id' => 'integer',
75
'is_active' => 'boolean',
76
'is_processing' => 'boolean',
77
'only_when_online' => 'boolean',
78
'last_run_at' => 'datetime',
79
'next_run_at' => 'datetime',
80
];
81
82
protected $attributes = [
83
'name' => null,
84
'cron_day_of_week' => '*',
85
'cron_month' => '*',
86
'cron_day_of_month' => '*',
87
'cron_hour' => '*',
88
'cron_minute' => '*',
89
'is_active' => true,
90
'is_processing' => false,
91
'only_when_online' => false,
92
];
93
94
public static array $validationRules = [
95
'server_id' => 'required|exists:servers,id',
96
'name' => 'required|string|max:191',
97
'cron_day_of_week' => 'required|string',
98
'cron_month' => 'required|string',
99
'cron_day_of_month' => 'required|string',
100
'cron_hour' => 'required|string',
101
'cron_minute' => 'required|string',
102
'is_active' => 'boolean',
103
'is_processing' => 'boolean',
104
'only_when_online' => 'boolean',
105
'last_run_at' => 'nullable|date',
106
'next_run_at' => 'nullable|date',
107
];
108
109
public function getRouteKeyName(): string
110
{
111
return $this->getKeyName();
112
}
113
114
/**
115
* Returns the schedule's execution crontab entry as a string.
116
*
117
* @throws \Exception
118
*/
119
public function getNextRunDate(): CarbonImmutable
120
{
121
$formatted = sprintf('%s %s %s %s %s', $this->cron_minute, $this->cron_hour, $this->cron_day_of_month, $this->cron_month, $this->cron_day_of_week);
122
123
return CarbonImmutable::createFromTimestamp(
124
(new CronExpression($formatted))->getNextRunDate()->getTimestamp()
125
);
126
}
127
128
/**
129
* Return a hashid encoded string to represent the ID of the schedule.
130
*/
131
public function getHashidAttribute(): string
132
{
133
return Container::getInstance()->make(HashidsInterface::class)->encode($this->id);
134
}
135
136
/**
137
* Return tasks belonging to a schedule.
138
*/
139
public function tasks(): HasMany
140
{
141
return $this->hasMany(Task::class);
142
}
143
144
/**
145
* Return the server model that a schedule belongs to.
146
*/
147
public function server(): BelongsTo
148
{
149
return $this->belongsTo(Server::class);
150
}
151
}
152
153