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