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;
|
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
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 Illuminate\Support\Carbon;
|
|
|
|
|
use Pterodactyl\Models\Traits\HasHashid;
|
2020-02-08 15:23:08 -08:00
|
|
|
|
|
|
|
|
/**
|
2021-01-27 20:52:11 -08:00
|
|
|
* @property int $id
|
|
|
|
|
* @property int $server_id
|
|
|
|
|
* @property string $name
|
|
|
|
|
* @property string $cron_day_of_week
|
|
|
|
|
* @property string $cron_month
|
|
|
|
|
* @property string $cron_day_of_month
|
|
|
|
|
* @property string $cron_hour
|
|
|
|
|
* @property string $cron_minute
|
|
|
|
|
* @property bool $is_active
|
|
|
|
|
* @property bool $is_processing
|
2021-05-01 10:44:40 -07:00
|
|
|
* @property bool $only_when_online
|
2026-10-05 18:10:18 -04:00
|
|
|
* @property Carbon|null $last_run_at
|
|
|
|
|
* @property Carbon|null $next_run_at
|
|
|
|
|
* @property Carbon $created_at
|
|
|
|
|
* @property Carbon $updated_at
|
2024-10-21 19:18:20 -06:00
|
|
|
* @property Server $server
|
2026-10-05 18:10:18 -04:00
|
|
|
* @property Collection<int, Task> $tasks
|
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;
|
|
|
|
|
|
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 = 'server_schedule';
|
2017-09-12 23:45:19 -05:00
|
|
|
|
2020-03-22 13:57:31 -07:00
|
|
|
/**
|
|
|
|
|
* Always return the tasks associated with this schedule.
|
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
|
* Returns the schedule's execution crontab entry as a string.
|
|
|
|
|
*
|
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
|
|
|
/**
|
|
|
|
|
* Return tasks belonging to a schedule.
|
2025-12-20 15:55:13 -08: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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the server model that a schedule belongs to.
|
2025-12-20 15:55:13 -08: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);
|
|
|
|
|
}
|
|
|
|
|
}
|