2016-02-27 18:35:12 -05:00
|
|
|
<?php
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2024-03-12 22:39:16 -04:00
|
|
|
namespace App\Models;
|
2016-02-27 18:35:12 -05:00
|
|
|
|
2024-03-23 10:58:21 -04:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
|
2022-10-14 10:59:20 -06:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2016-02-27 18:35:12 -05:00
|
|
|
|
2020-02-08 15:23:08 -08:00
|
|
|
/**
|
2021-01-27 20:52:11 -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
|
2021-05-01 10:44:40 -07:00
|
|
|
* @property bool $continue_on_failure
|
2021-01-27 20:52:11 -08:00
|
|
|
* @property \Carbon\Carbon $created_at
|
|
|
|
|
* @property \Carbon\Carbon $updated_at
|
2024-03-12 22:39:16 -04:00
|
|
|
* @property \App\Models\Schedule $schedule
|
|
|
|
|
* @property \App\Models\Server $server
|
2020-02-08 15:23:08 -08:00
|
|
|
*/
|
2020-04-03 23:22:35 -07:00
|
|
|
class Task extends Model
|
2016-02-27 18:35:12 -05:00
|
|
|
{
|
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 = 'schedule_task';
|
2018-01-25 21:26:06 -06:00
|
|
|
|
2021-05-01 10:44:40 -07:00
|
|
|
/**
|
2024-03-12 22:39:16 -04:00
|
|
|
* The default actions that can exist for a task
|
2021-05-01 10:44:40 -07:00
|
|
|
*/
|
|
|
|
|
public const ACTION_POWER = 'power';
|
|
|
|
|
public const ACTION_COMMAND = 'command';
|
|
|
|
|
public const ACTION_BACKUP = 'backup';
|
2024-07-10 09:25:15 +02:00
|
|
|
public const ACTION_DELETE_FILES = 'delete_files';
|
2021-05-01 10:44:40 -07:00
|
|
|
|
2016-02-27 18:35:12 -05:00
|
|
|
/**
|
|
|
|
|
* The table associated with the model.
|
|
|
|
|
*/
|
|
|
|
|
protected $table = 'tasks';
|
|
|
|
|
|
|
|
|
|
/**
|
2017-09-12 23:45:19 -05:00
|
|
|
* Relationships to be updated when this model is updated.
|
2016-02-27 18:35:12 -05:00
|
|
|
*/
|
2017-09-12 23:45:19 -05:00
|
|
|
protected $touches = ['schedule'];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fields that are mass assignable.
|
|
|
|
|
*/
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'schedule_id',
|
2017-09-13 21:46:43 -05:00
|
|
|
'sequence_id',
|
2017-09-12 23:45:19 -05:00
|
|
|
'action',
|
|
|
|
|
'payload',
|
|
|
|
|
'time_offset',
|
|
|
|
|
'is_queued',
|
2021-05-01 10:44:40 -07:00
|
|
|
'continue_on_failure',
|
2017-09-12 23:45:19 -05:00
|
|
|
];
|
2016-02-27 18:35:12 -05:00
|
|
|
|
2017-09-09 23:55:21 -05:00
|
|
|
/**
|
|
|
|
|
* Default attributes when creating a new model.
|
|
|
|
|
*/
|
|
|
|
|
protected $attributes = [
|
2020-03-18 22:28:32 -07:00
|
|
|
'time_offset' => 0,
|
2017-09-12 23:45:19 -05:00
|
|
|
'is_queued' => false,
|
2021-05-01 10:44:40 -07:00
|
|
|
'continue_on_failure' => false,
|
2017-09-09 23:55:21 -05:00
|
|
|
];
|
|
|
|
|
|
2022-10-14 10:59:20 -06:00
|
|
|
public static array $validationRules = [
|
2019-09-04 22:19:57 -07:00
|
|
|
'schedule_id' => 'required|numeric|exists:schedules,id',
|
|
|
|
|
'sequence_id' => 'required|numeric|min:1',
|
|
|
|
|
'action' => 'required|string',
|
2020-06-18 21:00:04 -07:00
|
|
|
'payload' => 'required_unless:action,backup|string',
|
2019-09-04 22:19:57 -07:00
|
|
|
'time_offset' => 'required|numeric|between:0,900',
|
2017-09-12 23:45:19 -05:00
|
|
|
'is_queued' => 'boolean',
|
2021-05-01 10:44:40 -07:00
|
|
|
'continue_on_failure' => 'boolean',
|
2017-09-09 23:55:21 -05:00
|
|
|
];
|
|
|
|
|
|
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',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-22 14:10:01 -04:00
|
|
|
public function getRouteKeyName(): string
|
|
|
|
|
{
|
|
|
|
|
return $this->getKeyName();
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-15 13:52:43 -04:00
|
|
|
/**
|
2017-09-12 23:45:19 -05:00
|
|
|
* Return the schedule that a task belongs to.
|
2017-04-15 13:52:43 -04:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function schedule(): BelongsTo
|
2017-04-15 13:52:43 -04:00
|
|
|
{
|
2017-09-12 23:45:19 -05:00
|
|
|
return $this->belongsTo(Schedule::class);
|
2017-04-15 13:52:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-09-12 23:45:19 -05: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
|
2017-09-09 23:55:21 -05:00
|
|
|
{
|
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
|
|
|
|
|
);
|
2017-09-09 23:55:21 -05:00
|
|
|
}
|
2016-02-27 18:35:12 -05:00
|
|
|
}
|