Files
panel-pelican-dev/app/Models/Task.php

143 lines
4.6 KiB
PHP
Raw Normal View History

<?php
2016-12-07 22:46:38 +00:00
2024-03-12 22:39:16 -04:00
namespace App\Models;
use App\Contracts\Validatable;
2025-11-24 14:42:47 +01:00
use App\Extensions\Tasks\TaskSchemaInterface;
use App\Extensions\Tasks\TaskService;
use App\Traits\HasValidation;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2025-09-24 13:34:19 +02:00
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Illuminate\Support\Carbon;
2020-02-08 15:23:08 -08:00
/**
* @property int $id
* @property int $schedule_id
* @property int $sequence_id
* @property string $action
* @property string $payload
* @property int $time_offset
* @property bool $is_queued
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property bool $continue_on_failure
* @property-read Schedule $schedule
* @property-read Server|null $server
*
* @method static \Database\Factories\TaskFactory factory($count = null, $state = [])
* @method static \Illuminate\Database\Eloquent\Builder<static>|Task newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Task newQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Task query()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Task whereAction($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Task whereContinueOnFailure($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Task whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Task whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Task whereIsQueued($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Task wherePayload($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Task whereScheduleId($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Task whereSequenceId($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Task whereTimeOffset($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Task whereUpdatedAt($value)
2020-02-08 15:23:08 -08:00
*/
class Task extends Model implements Validatable
{
use HasFactory;
use HasValidation;
/**
* 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 = 'schedule_task';
/**
* Relationships to be updated when this model is updated.
*
* @var string[]
*/
protected $touches = ['schedule'];
/**
* Fields that are mass assignable.
*/
protected $fillable = [
'schedule_id',
2017-09-13 21:46:43 -05:00
'sequence_id',
'action',
'payload',
'time_offset',
'is_queued',
'continue_on_failure',
];
/**
* Default attributes when creating a new model.
*/
protected $attributes = [
'time_offset' => 0,
'is_queued' => false,
'continue_on_failure' => false,
];
/** @var array<array-key, string[]> */
public static array $validationRules = [
'schedule_id' => ['required', 'numeric', 'exists:schedules,id'],
'sequence_id' => ['required', 'numeric', 'min:1'],
'action' => ['required', 'string'],
'payload' => ['required_unless:action,backup', 'string'],
'time_offset' => ['required', 'numeric', 'between:0,900'],
'is_queued' => ['boolean'],
'continue_on_failure' => ['boolean'],
];
2024-03-19 21:08:49 -04:00
protected function casts(): array
{
return [
'id' => 'integer',
'schedule_id' => 'integer',
'sequence_id' => 'integer',
'time_offset' => 'integer',
'is_queued' => 'boolean',
'continue_on_failure' => 'boolean',
];
}
2017-04-15 13:52:43 -04:00
/**
* Return the schedule that a task belongs to.
2017-04-15 13:52:43 -04:00
*/
public function schedule(): BelongsTo
2017-04-15 13:52:43 -04:00
{
return $this->belongsTo(Schedule::class);
2017-04-15 13:52:43 -04:00
}
/**
* Return the server a task is assigned to, acts as a belongsToThrough.
2017-04-15 13:52:43 -04:00
*/
2024-03-23 10:58:21 -04:00
public function server(): HasOneThrough
{
2024-03-23 10:58:21 -04:00
return $this->hasOneThrough(
Server::class,
Schedule::class,
'id', // schedules.id
'id', // servers.id
'schedule_id', // tasks.schedule_id
'server_id' // schedules.server_id
);
}
2025-11-24 00:48:32 +01:00
public function isFirst(): bool
{
return $this->schedule->firstTask()?->id === $this->id;
}
2025-11-24 14:42:47 +01:00
public function getSchema(): ?TaskSchemaInterface
{
/** @var TaskService $taskService */
$taskService = app(TaskService::class); // @phpstan-ignore myCustomRules.forbiddenGlobalFunctions
return $taskService->get($this->action);
}
}