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;
|
|
|
|
use Pterodactyl\Contracts\Extensions\HashidsInterface;
|
|
|
|
|
|
|
|
/**
|
2021-01-23 20:33:34 +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
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $table = 'schedules';
|
|
|
|
|
2020-03-22 20:57:31 +00:00
|
|
|
/**
|
|
|
|
* Always return the tasks associated with this schedule.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $with = ['tasks'];
|
|
|
|
|
2017-09-13 04:45:19 +00:00
|
|
|
/**
|
|
|
|
* Mass assignable attributes on this model.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
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',
|
|
|
|
'last_run_at',
|
|
|
|
'next_run_at',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
|
|
|
'id' => 'integer',
|
|
|
|
'server_id' => 'integer',
|
|
|
|
'is_active' => 'boolean',
|
|
|
|
'is_processing' => 'boolean',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Columns to mutate to a date.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $dates = [
|
|
|
|
'last_run_at',
|
|
|
|
'next_run_at',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
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,
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-09-05 05:19:57 +00:00
|
|
|
public static $validationRules = [
|
|
|
|
'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',
|
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
|
|
|
];
|
|
|
|
|
2020-10-15 03:38:59 +00:00
|
|
|
/**
|
|
|
|
* Returns the schedule's execution crontab entry as a string.
|
|
|
|
*
|
|
|
|
* @return \Carbon\CarbonImmutable
|
|
|
|
*/
|
|
|
|
public function getNextRunDate()
|
|
|
|
{
|
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(
|
|
|
|
CronExpression::factory($formatted)->getNextRunDate()->getTimestamp()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-09-13 04:45:19 +00:00
|
|
|
/**
|
|
|
|
* Return a hashid encoded string to represent the ID of the schedule.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getHashidAttribute()
|
|
|
|
{
|
2020-02-08 23:23:08 +00:00
|
|
|
return Container::getInstance()->make(HashidsInterface::class)->encode($this->id);
|
2017-09-13 04:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return tasks belonging to a schedule.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function tasks()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Task::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the server model that a schedule belongs to.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function server()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Server::class);
|
|
|
|
}
|
|
|
|
}
|