2017-09-13 06:45:19 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Models;
|
|
|
|
|
2020-10-15 05:38:59 +02:00
|
|
|
use Cron\CronExpression;
|
|
|
|
use Carbon\CarbonImmutable;
|
2020-02-09 00:23:08 +01:00
|
|
|
use Illuminate\Container\Container;
|
2022-10-14 18:59:20 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2020-02-09 00:23:08 +01:00
|
|
|
use Pterodactyl\Contracts\Extensions\HashidsInterface;
|
|
|
|
|
|
|
|
/**
|
2021-01-28 05:52:11 +01: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 19:44:40 +02:00
|
|
|
* @property bool $only_when_online
|
2021-01-28 05:52:11 +01:00
|
|
|
* @property \Carbon\Carbon|null $last_run_at
|
|
|
|
* @property \Carbon\Carbon|null $next_run_at
|
|
|
|
* @property \Carbon\Carbon $created_at
|
|
|
|
* @property \Carbon\Carbon $updated_at
|
|
|
|
* @property string $hashid
|
|
|
|
* @property \Pterodactyl\Models\Server $server
|
2020-02-09 00:23:08 +01:00
|
|
|
* @property \Pterodactyl\Models\Task[]|\Illuminate\Support\Collection $tasks
|
|
|
|
*/
|
2020-04-04 08:22:35 +02:00
|
|
|
class Schedule extends Model
|
2017-09-13 06:45:19 +02:00
|
|
|
{
|
2018-01-26 04:26:06 +01:00
|
|
|
/**
|
|
|
|
* The resource name for this model when it is transformed into an
|
|
|
|
* API representation using fractal.
|
|
|
|
*/
|
2021-01-23 21:33:34 +01:00
|
|
|
public const RESOURCE_NAME = 'server_schedule';
|
2018-01-26 04:26:06 +01:00
|
|
|
|
2017-09-13 06:45:19 +02:00
|
|
|
/**
|
|
|
|
* The table associated with the model.
|
|
|
|
*/
|
|
|
|
protected $table = 'schedules';
|
|
|
|
|
2020-03-22 21:57:31 +01:00
|
|
|
/**
|
|
|
|
* Always return the tasks associated with this schedule.
|
|
|
|
*/
|
|
|
|
protected $with = ['tasks'];
|
|
|
|
|
2017-09-13 06:45:19 +02:00
|
|
|
/**
|
|
|
|
* Mass assignable attributes on this model.
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
|
|
|
'server_id',
|
|
|
|
'name',
|
|
|
|
'cron_day_of_week',
|
2021-01-17 04:07:39 +01:00
|
|
|
'cron_month',
|
2017-09-13 06:45:19 +02:00
|
|
|
'cron_day_of_month',
|
|
|
|
'cron_hour',
|
|
|
|
'cron_minute',
|
|
|
|
'is_active',
|
|
|
|
'is_processing',
|
2021-05-01 19:44:40 +02:00
|
|
|
'only_when_online',
|
2017-09-13 06:45:19 +02:00
|
|
|
'last_run_at',
|
|
|
|
'next_run_at',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
'id' => 'integer',
|
|
|
|
'server_id' => 'integer',
|
|
|
|
'is_active' => 'boolean',
|
|
|
|
'is_processing' => 'boolean',
|
2021-05-01 19:44:40 +02:00
|
|
|
'only_when_online' => 'boolean',
|
2023-02-23 20:30:16 +01:00
|
|
|
'last_run_at' => 'datetime',
|
|
|
|
'next_run_at' => 'datetime',
|
2017-09-13 06:45:19 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
protected $attributes = [
|
|
|
|
'name' => null,
|
|
|
|
'cron_day_of_week' => '*',
|
2021-01-17 04:07:39 +01:00
|
|
|
'cron_month' => '*',
|
2017-09-13 06:45:19 +02:00
|
|
|
'cron_day_of_month' => '*',
|
|
|
|
'cron_hour' => '*',
|
|
|
|
'cron_minute' => '*',
|
|
|
|
'is_active' => true,
|
|
|
|
'is_processing' => false,
|
2021-05-01 19:44:40 +02:00
|
|
|
'only_when_online' => false,
|
2017-09-13 06:45:19 +02:00
|
|
|
];
|
|
|
|
|
2022-10-14 18:59:20 +02:00
|
|
|
public static array $validationRules = [
|
2019-09-05 07:19:57 +02:00
|
|
|
'server_id' => 'required|exists:servers,id',
|
2020-09-27 01:29:26 +02:00
|
|
|
'name' => 'required|string|max:191',
|
2019-09-05 07:19:57 +02:00
|
|
|
'cron_day_of_week' => 'required|string',
|
2021-01-17 04:07:39 +01:00
|
|
|
'cron_month' => 'required|string',
|
2019-09-05 07:19:57 +02:00
|
|
|
'cron_day_of_month' => 'required|string',
|
|
|
|
'cron_hour' => 'required|string',
|
|
|
|
'cron_minute' => 'required|string',
|
2017-09-13 06:45:19 +02:00
|
|
|
'is_active' => 'boolean',
|
|
|
|
'is_processing' => 'boolean',
|
2021-05-01 19:44:40 +02:00
|
|
|
'only_when_online' => 'boolean',
|
2017-09-17 06:10:00 +02:00
|
|
|
'last_run_at' => 'nullable|date',
|
|
|
|
'next_run_at' => 'nullable|date',
|
2017-09-13 06:45:19 +02:00
|
|
|
];
|
|
|
|
|
2022-05-22 20:10:01 +02:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getRouteKeyName(): string
|
|
|
|
{
|
|
|
|
return $this->getKeyName();
|
|
|
|
}
|
|
|
|
|
2020-10-15 05:38:59 +02:00
|
|
|
/**
|
|
|
|
* Returns the schedule's execution crontab entry as a string.
|
|
|
|
*
|
2021-03-21 18:49:23 +01:00
|
|
|
* @throws \Exception
|
2020-10-15 05:38:59 +02:00
|
|
|
*/
|
2022-10-14 18:59:20 +02:00
|
|
|
public function getNextRunDate(): CarbonImmutable
|
2020-10-15 05:38:59 +02:00
|
|
|
{
|
2021-01-17 04:07:39 +01: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-15 05:38:59 +02:00
|
|
|
|
|
|
|
return CarbonImmutable::createFromTimestamp(
|
2021-03-21 18:49:23 +01:00
|
|
|
(new CronExpression($formatted))->getNextRunDate()->getTimestamp()
|
2020-10-15 05:38:59 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-09-13 06:45:19 +02:00
|
|
|
/**
|
|
|
|
* Return a hashid encoded string to represent the ID of the schedule.
|
|
|
|
*/
|
2022-10-14 18:59:20 +02:00
|
|
|
public function getHashidAttribute(): string
|
2017-09-13 06:45:19 +02:00
|
|
|
{
|
2020-02-09 00:23:08 +01:00
|
|
|
return Container::getInstance()->make(HashidsInterface::class)->encode($this->id);
|
2017-09-13 06:45:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return tasks belonging to a schedule.
|
|
|
|
*/
|
2022-10-14 18:59:20 +02:00
|
|
|
public function tasks(): HasMany
|
2017-09-13 06:45:19 +02:00
|
|
|
{
|
|
|
|
return $this->hasMany(Task::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the server model that a schedule belongs to.
|
|
|
|
*/
|
2022-10-14 18:59:20 +02:00
|
|
|
public function server(): BelongsTo
|
2017-09-13 06:45:19 +02:00
|
|
|
{
|
|
|
|
return $this->belongsTo(Server::class);
|
|
|
|
}
|
|
|
|
}
|