coolify/app/Models/ScheduledTask.php

87 lines
3.1 KiB
PHP
Raw Normal View History

2024-01-01 18:33:16 +00:00
<?php
namespace App\Models;
use App\Traits\HasSafeStringAttribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
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
{
use HasFactory;
use HasSafeStringAttribute;
2024-01-01 18:33:16 +00:00
protected $guarded = [];
public static function ownedByCurrentTeamAPI(int $teamId)
{
return static::where('team_id', $teamId)->orderBy('created_at', 'desc');
}
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
public function application()
2024-01-01 18:33:16 +00:00
{
return $this->belongsTo(Application::class);
2024-01-01 18:33:16 +00:00
}
2024-06-10 20:43:34 +00:00
public function latest_log(): HasOne
2024-01-01 18:33:16 +00:00
{
return $this->hasOne(ScheduledTaskExecution::class)->latest();
}
2024-06-10 20:43:34 +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) {
if ($this->application->destination && $this->application->destination->server) {
2024-10-31 17:20:11 +00:00
return $this->application->destination->server;
}
2024-08-16 14:01:41 +00:00
} elseif ($this->service) {
if ($this->service->destination && $this->service->destination->server) {
2024-10-31 17:20:11 +00:00
return $this->service->destination->server;
}
} 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 14:01:41 +00:00
}
2024-09-23 17:51:31 +00:00
2024-08-16 14:01:41 +00:00
return null;
}
}