Files

104 lines
2.4 KiB
PHP
Raw Permalink Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Models;
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',
])]
class Schedule extends Model
{
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';
2020-03-22 13:57:31 -07:00
protected $with = ['tasks'];
protected $attributes = [
'name' => null,
'cron_day_of_week' => '*',
2021-01-16 22:07:39 -05:00
'cron_month' => '*',
'cron_day_of_month' => '*',
'cron_hour' => '*',
'cron_minute' => '*',
'is_active' => true,
'is_processing' => false,
'only_when_online' => false,
];
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',
];
}
2022-05-22 14:10:01 -04:00
public function getRouteKeyName(): string
{
return $this->getKeyName();
}
/**
2026-10-05 18:10:18 -04:00
* @throws Exception
*/
2022-10-14 10:59:20 -06:00
public function getNextRunDate(): CarbonImmutable
{
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);
return CarbonImmutable::instance((new CronExpression($formatted))->getNextRunDate());
}
/**
2026-10-05 18:10:18 -04:00
* @return HasMany<Task, $this>
*/
2022-10-14 10:59:20 -06:00
public function tasks(): HasMany
{
2026-10-05 18:10:18 -04:00
return $this->hasMany(Task::class)->chaperone('schedule');
}
/**
2026-10-05 18:10:18 -04:00
* @return BelongsTo<Server, $this>
*/
2022-10-14 10:59:20 -06:00
public function server(): BelongsTo
{
return $this->belongsTo(Server::class);
}
}