2017-09-12 23:45:19 -05:00
|
|
|
<?php
|
|
|
|
|
|
2024-03-12 22:39:16 -04:00
|
|
|
namespace App\Models;
|
2017-09-12 23:45:19 -05:00
|
|
|
|
2025-01-30 16:39:00 -05:00
|
|
|
use App\Contracts\Validatable;
|
2025-09-15 23:28:57 +02:00
|
|
|
use App\Enums\ScheduleStatus;
|
2024-10-23 21:59:13 +02:00
|
|
|
use App\Helpers\Utilities;
|
2025-01-30 16:39:00 -05:00
|
|
|
use App\Traits\HasValidation;
|
2025-09-24 13:34:19 +02:00
|
|
|
use Exception;
|
2025-09-15 23:28:57 +02:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2026-03-17 09:09:01 +01:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2025-01-30 16:39:00 -05:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2022-10-14 10:59:20 -06:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2025-09-24 13:34:19 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2026-03-17 09:09:01 +01:00
|
|
|
use Illuminate\Support\Carbon;
|
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_day_of_month
|
|
|
|
|
* @property string $cron_hour
|
|
|
|
|
* @property string $cron_minute
|
|
|
|
|
* @property bool $is_active
|
|
|
|
|
* @property bool $is_processing
|
2025-09-08 13:12:33 -04:00
|
|
|
* @property Carbon|null $last_run_at
|
|
|
|
|
* @property Carbon|null $next_run_at
|
2026-03-17 09:09:01 +01:00
|
|
|
* @property Carbon|null $created_at
|
|
|
|
|
* @property Carbon|null $updated_at
|
|
|
|
|
* @property string $cron_month
|
|
|
|
|
* @property bool $only_when_online
|
|
|
|
|
* @property-read Server $server
|
|
|
|
|
* @property-read ScheduleStatus $status
|
|
|
|
|
* @property-read Collection<int, Task> $tasks
|
|
|
|
|
* @property-read int|null $tasks_count
|
|
|
|
|
*
|
|
|
|
|
* @method static \Database\Factories\ScheduleFactory factory($count = null, $state = [])
|
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule newModelQuery()
|
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule newQuery()
|
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule query()
|
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereCreatedAt($value)
|
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereCronDayOfMonth($value)
|
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereCronDayOfWeek($value)
|
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereCronHour($value)
|
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereCronMinute($value)
|
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereCronMonth($value)
|
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereId($value)
|
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereIsActive($value)
|
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereIsProcessing($value)
|
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereLastRunAt($value)
|
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereName($value)
|
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereNextRunAt($value)
|
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereOnlyWhenOnline($value)
|
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereServerId($value)
|
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereUpdatedAt($value)
|
2020-02-08 15:23:08 -08:00
|
|
|
*/
|
2025-01-30 16:39:00 -05:00
|
|
|
class Schedule extends Model implements Validatable
|
2017-09-12 23:45:19 -05:00
|
|
|
{
|
2025-01-30 16:39:00 -05:00
|
|
|
use HasFactory;
|
|
|
|
|
use HasValidation;
|
|
|
|
|
|
2018-01-25 21:26:06 -06:00
|
|
|
/**
|
|
|
|
|
* The resource name for this model when it is transformed into an
|
|
|
|
|
* API representation using fractal.
|
|
|
|
|
*/
|
2021-01-23 12:33:34 -08:00
|
|
|
public const RESOURCE_NAME = 'server_schedule';
|
2018-01-25 21:26:06 -06: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
|
|
|
/**
|
|
|
|
|
* Mass assignable attributes on this model.
|
|
|
|
|
*/
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'server_id',
|
|
|
|
|
'name',
|
|
|
|
|
'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',
|
|
|
|
|
'is_processing',
|
2021-05-01 10:44:40 -07:00
|
|
|
'only_when_online',
|
2017-09-12 23:45:19 -05:00
|
|
|
'last_run_at',
|
|
|
|
|
'next_run_at',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
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
|
|
|
];
|
|
|
|
|
|
2025-03-08 19:56:06 -05:00
|
|
|
/** @var array<array-key, string[]> */
|
2022-10-14 10:59:20 -06:00
|
|
|
public static array $validationRules = [
|
2025-03-08 19:56:06 -05:00
|
|
|
'server_id' => ['required', 'exists:servers,id'],
|
|
|
|
|
'name' => ['required', 'string', 'max:255'],
|
|
|
|
|
'cron_day_of_week' => ['required', 'string'],
|
|
|
|
|
'cron_month' => ['required', 'string'],
|
|
|
|
|
'cron_day_of_month' => ['required', 'string'],
|
|
|
|
|
'cron_hour' => ['required', 'string'],
|
|
|
|
|
'cron_minute' => ['required', 'string'],
|
|
|
|
|
'is_active' => ['boolean'],
|
|
|
|
|
'is_processing' => ['boolean'],
|
|
|
|
|
'only_when_online' => ['boolean'],
|
|
|
|
|
'last_run_at' => ['nullable', 'date'],
|
|
|
|
|
'next_run_at' => ['nullable', 'date'],
|
2017-09-12 23:45:19 -05:00
|
|
|
];
|
|
|
|
|
|
2024-03-19 21:08:49 -04:00
|
|
|
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',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-15 23:28:57 +02:00
|
|
|
protected function status(): Attribute
|
|
|
|
|
{
|
|
|
|
|
return Attribute::make(
|
|
|
|
|
get: fn () => !$this->is_active ? ScheduleStatus::Inactive : ($this->is_processing ? ScheduleStatus::Processing : ScheduleStatus::Active),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-14 20:38:59 -07:00
|
|
|
/**
|
|
|
|
|
* Returns the schedule's execution crontab entry as a string.
|
|
|
|
|
*
|
2025-09-08 13:12:33 -04:00
|
|
|
* @throws Exception
|
2020-10-14 20:38:59 -07:00
|
|
|
*/
|
2024-10-23 21:59:13 +02:00
|
|
|
public function getNextRunDate(): string
|
2020-10-14 20:38:59 -07:00
|
|
|
{
|
2024-10-23 21:59:13 +02:00
|
|
|
return Utilities::getScheduleNextRunDate($this->cron_minute, $this->cron_hour, $this->cron_day_of_month, $this->cron_month, $this->cron_day_of_week)->toDateTimeString();
|
2020-10-14 20:38:59 -07:00
|
|
|
}
|
|
|
|
|
|
2017-09-12 23:45:19 -05:00
|
|
|
/**
|
|
|
|
|
* Return tasks belonging to a schedule.
|
|
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function tasks(): HasMany
|
2017-09-12 23:45:19 -05:00
|
|
|
{
|
|
|
|
|
return $this->hasMany(Task::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the server model that a schedule belongs to.
|
|
|
|
|
*/
|
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);
|
|
|
|
|
}
|
2025-11-24 00:48:32 +01:00
|
|
|
|
|
|
|
|
public function firstTask(): ?Task
|
|
|
|
|
{
|
|
|
|
|
/** @var ?Task $task */
|
|
|
|
|
$task = $this->tasks()->orderBy('sequence_id')->first();
|
|
|
|
|
|
|
|
|
|
return $task;
|
|
|
|
|
}
|
2017-09-12 23:45:19 -05:00
|
|
|
}
|