2024-01-01 18:33:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2025-08-19 10:14:48 +00:00
|
|
|
use App\Traits\HasSafeStringAttribute;
|
2026-03-03 08:50:05 +00:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2024-01-02 02:23:29 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
2026-02-16 19:26:58 +00:00
|
|
|
use OpenApi\Attributes as OA;
|
2024-01-01 18:33:16 +00:00
|
|
|
|
2026-02-16 19:26:58 +00:00
|
|
|
#[OA\Schema(
|
|
|
|
|
description: 'Scheduled Task model',
|
|
|
|
|
type: 'object',
|
|
|
|
|
properties: [
|
|
|
|
|
'id' => ['type' => 'integer', 'description' => 'The unique identifier of the scheduled task in the database.'],
|
|
|
|
|
'uuid' => ['type' => 'string', 'description' => 'The unique identifier of the scheduled task.'],
|
|
|
|
|
'enabled' => ['type' => 'boolean', 'description' => 'The flag to indicate if the scheduled task is enabled.'],
|
|
|
|
|
'name' => ['type' => 'string', 'description' => 'The name of the scheduled task.'],
|
|
|
|
|
'command' => ['type' => 'string', 'description' => 'The command to execute.'],
|
|
|
|
|
'frequency' => ['type' => 'string', 'description' => 'The frequency of the scheduled task.'],
|
|
|
|
|
'container' => ['type' => 'string', 'nullable' => true, 'description' => 'The container where the command should be executed.'],
|
|
|
|
|
'timeout' => ['type' => 'integer', 'description' => 'The timeout of the scheduled task in seconds.'],
|
|
|
|
|
'created_at' => ['type' => 'string', 'format' => 'date-time', 'description' => 'The date and time when the scheduled task was created.'],
|
|
|
|
|
'updated_at' => ['type' => 'string', 'format' => 'date-time', 'description' => 'The date and time when the scheduled task was last updated.'],
|
|
|
|
|
],
|
|
|
|
|
)]
|
2024-01-01 18:33:16 +00:00
|
|
|
class ScheduledTask extends BaseModel
|
|
|
|
|
{
|
2026-03-03 08:50:05 +00:00
|
|
|
use HasFactory;
|
2025-08-19 10:14:48 +00:00
|
|
|
use HasSafeStringAttribute;
|
2025-08-19 09:04:23 +00:00
|
|
|
|
2024-01-01 18:33:16 +00:00
|
|
|
protected $guarded = [];
|
|
|
|
|
|
2026-02-18 10:53:58 +00:00
|
|
|
public static function ownedByCurrentTeamAPI(int $teamId)
|
|
|
|
|
{
|
|
|
|
|
return static::where('team_id', $teamId)->orderBy('created_at', 'desc');
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-10 10:11:18 +00:00
|
|
|
protected function casts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'enabled' => 'boolean',
|
|
|
|
|
'timeout' => 'integer',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-01 18:33:16 +00:00
|
|
|
public function service()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Service::class);
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-01-02 02:23:29 +00:00
|
|
|
public function application()
|
2024-01-01 18:33:16 +00:00
|
|
|
{
|
2024-01-02 02:23:29 +00:00
|
|
|
return $this->belongsTo(Application::class);
|
2024-01-01 18:33:16 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-01-02 02:23:29 +00:00
|
|
|
public function latest_log(): HasOne
|
2024-01-01 18:33:16 +00:00
|
|
|
{
|
2024-01-02 02:23:29 +00:00
|
|
|
return $this->hasOne(ScheduledTaskExecution::class)->latest();
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-01-02 02:23:29 +00:00
|
|
|
public function executions(): HasMany
|
|
|
|
|
{
|
2024-09-03 15:35:18 +00:00
|
|
|
// Last execution first
|
2024-08-21 14:53:53 +00:00
|
|
|
return $this->hasMany(ScheduledTaskExecution::class)->orderBy('created_at', 'desc');
|
2024-01-01 18:33:16 +00:00
|
|
|
}
|
2024-08-16 14:01:41 +00:00
|
|
|
|
|
|
|
|
public function server()
|
|
|
|
|
{
|
|
|
|
|
if ($this->application) {
|
2024-08-16 20:05:38 +00:00
|
|
|
if ($this->application->destination && $this->application->destination->server) {
|
2024-10-31 17:20:11 +00:00
|
|
|
return $this->application->destination->server;
|
2024-08-16 20:05:38 +00:00
|
|
|
}
|
2024-08-16 14:01:41 +00:00
|
|
|
} elseif ($this->service) {
|
2024-08-16 20:05:38 +00:00
|
|
|
if ($this->service->destination && $this->service->destination->server) {
|
2024-10-31 17:20:11 +00:00
|
|
|
return $this->service->destination->server;
|
2024-08-16 20:05:38 +00:00
|
|
|
}
|
|
|
|
|
} elseif ($this->database) {
|
|
|
|
|
if ($this->database->destination && $this->database->destination->server) {
|
2024-10-31 17:20:11 +00:00
|
|
|
return $this->database->destination->server;
|
2024-08-16 20:05:38 +00:00
|
|
|
}
|
2024-08-16 14:01:41 +00:00
|
|
|
}
|
2024-09-23 17:51:31 +00:00
|
|
|
|
2024-08-16 14:01:41 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2024-08-16 20:05:38 +00:00
|
|
|
}
|