2017-09-12 23:45:19 -05:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2017-09-12 23:45:19 -05:00
|
|
|
namespace Pterodactyl\Models;
|
|
|
|
|
|
2020-10-14 20:38:59 -07:00
|
|
|
use Carbon\CarbonImmutable;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Cron\CronExpression;
|
|
|
|
|
use Database\Factories\ScheduleFactory;
|
|
|
|
|
use Exception;
|
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
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\HasMany;
|
|
|
|
|
use Pterodactyl\Models\Traits\HasHashid;
|
2020-02-08 15:23:08 -08:00
|
|
|
|
|
|
|
|
/**
|
2026-10-11 22:55:07 -04:00
|
|
|
* @property-read Server $server
|
2020-02-08 15:23:08 -08:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
#[Fillable([
|
|
|
|
|
'server_id',
|
|
|
|
|
'name',
|
|
|
|
|
'cron_day_of_week',
|
|
|
|
|
'cron_month',
|
|
|
|
|
'cron_day_of_month',
|
|
|
|
|
'cron_hour',
|
|
|
|
|
'cron_minute',
|
|
|
|
|
'is_active',
|
|
|
|
|
'is_processing',
|
|
|
|
|
'only_when_online',
|
|
|
|
|
'last_run_at',
|
|
|
|
|
'next_run_at',
|
|
|
|
|
])]
|
2020-04-03 23:22:35 -07:00
|
|
|
class Schedule extends Model
|
2017-09-12 23:45:19 -05:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
/** @use HasFactory<ScheduleFactory> */
|
2024-10-21 19:18:20 -06:00
|
|
|
use HasFactory;
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
use HasHashid;
|
|
|
|
|
|
|
|
|
|
public const string RESOURCE_NAME = 'server_schedule';
|
2017-09-12 23:45:19 -05:00
|
|
|
|
2020-03-22 13:57:31 -07:00
|
|
|
protected $with = ['tasks'];
|
|
|
|
|
|
2017-09-12 23:45:19 -05:00
|
|
|
protected $attributes = [
|
|
|
|
|
'name' => null,
|
|
|
|
|
'cron_day_of_week' => '*',
|
2021-01-16 22:07:39 -05:00
|
|
|
'cron_month' => '*',
|
2017-09-12 23:45:19 -05:00
|
|
|
'cron_day_of_month' => '*',
|
|
|
|
|
'cron_hour' => '*',
|
|
|
|
|
'cron_minute' => '*',
|
|
|
|
|
'is_active' => true,
|
|
|
|
|
'is_processing' => false,
|
2021-05-01 10:44:40 -07:00
|
|
|
'only_when_online' => false,
|
2017-09-12 23:45:19 -05:00
|
|
|
];
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
/**
|
|
|
|
|
* @return array<string, string>
|
|
|
|
|
*/
|
|
|
|
|
protected function casts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'id' => 'integer',
|
|
|
|
|
'server_id' => 'integer',
|
|
|
|
|
'is_active' => 'boolean',
|
|
|
|
|
'is_processing' => 'boolean',
|
|
|
|
|
'only_when_online' => 'boolean',
|
|
|
|
|
'last_run_at' => 'datetime',
|
|
|
|
|
'next_run_at' => 'datetime',
|
|
|
|
|
];
|
|
|
|
|
}
|
2017-09-12 23:45:19 -05:00
|
|
|
|
2022-05-22 14:10:01 -04:00
|
|
|
public function getRouteKeyName(): string
|
|
|
|
|
{
|
|
|
|
|
return $this->getKeyName();
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-14 20:38:59 -07:00
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* @throws Exception
|
2020-10-14 20:38:59 -07:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function getNextRunDate(): CarbonImmutable
|
2020-10-14 20:38:59 -07:00
|
|
|
{
|
2021-01-16 22:07:39 -05:00
|
|
|
$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);
|
2020-10-14 20:38:59 -07:00
|
|
|
|
2025-11-09 14:32:06 +08:00
|
|
|
return CarbonImmutable::instance((new CronExpression($formatted))->getNextRunDate());
|
2020-10-14 20:38:59 -07:00
|
|
|
}
|
|
|
|
|
|
2017-09-12 23:45:19 -05:00
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return HasMany<Task, $this>
|
2017-09-12 23:45:19 -05:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function tasks(): HasMany
|
2017-09-12 23:45:19 -05:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
return $this->hasMany(Task::class)->chaperone('schedule');
|
2017-09-12 23:45:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return BelongsTo<Server, $this>
|
2017-09-12 23:45:19 -05:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function server(): BelongsTo
|
2017-09-12 23:45:19 -05:00
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Server::class);
|
|
|
|
|
}
|
|
|
|
|
}
|