coolify/app/Livewire/Project/Shared/ScheduledTask/Add.php

141 lines
3.8 KiB
PHP
Raw Normal View History

2024-01-01 18:33:16 +00:00
<?php
namespace App\Livewire\Project\Shared\ScheduledTask;
use App\Models\ScheduledTask;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Collection;
use Livewire\Attributes\Locked;
2024-01-01 18:33:16 +00:00
use Livewire\Component;
class Add extends Component
{
use AuthorizesRequests;
2024-01-01 18:33:16 +00:00
public $parameters;
2024-06-10 20:43:34 +00:00
#[Locked]
public string $id;
#[Locked]
public string $type;
2024-06-10 20:43:34 +00:00
#[Locked]
public Collection $containerNames;
2024-06-10 20:43:34 +00:00
#[Locked]
public $resource;
2024-01-01 18:33:16 +00:00
public string $name;
2024-06-10 20:43:34 +00:00
2024-01-01 18:33:16 +00:00
public string $command;
2024-06-10 20:43:34 +00:00
2024-01-01 18:33:16 +00:00
public string $frequency;
2024-06-10 20:43:34 +00:00
2024-01-01 18:33:16 +00:00
public ?string $container = '';
public int $timeout = 300;
2024-01-01 18:33:16 +00:00
protected $rules = [
'name' => 'required|string',
'command' => 'required|string',
'frequency' => 'required|string',
'container' => 'nullable|string',
'timeout' => 'required|integer|min:60|max:36000',
2024-01-01 18:33:16 +00:00
];
2024-06-10 20:43:34 +00:00
2024-01-01 18:33:16 +00:00
protected $validationAttributes = [
'name' => 'name',
'command' => 'command',
'frequency' => 'frequency',
'container' => 'container',
'timeout' => 'timeout',
2024-01-01 18:33:16 +00:00
];
public function mount()
{
$this->parameters = get_route_parameters();
// Get the resource based on type and id
switch ($this->type) {
case 'application':
$this->resource = \App\Models\Application::findOrFail($this->id);
break;
case 'service':
$this->resource = \App\Models\Service::findOrFail($this->id);
break;
case 'standalone-postgresql':
$this->resource = \App\Models\StandalonePostgresql::findOrFail($this->id);
break;
default:
throw new \Exception('Invalid resource type');
}
if ($this->containerNames->count() > 0) {
$this->container = $this->containerNames->first();
}
2024-01-01 18:33:16 +00:00
}
public function submit()
{
2024-03-19 14:37:16 +00:00
try {
$this->authorize('update', $this->resource);
2024-03-19 14:37:16 +00:00
$this->validate();
$isValid = validate_cron_expression($this->frequency);
2024-06-10 20:43:34 +00:00
if (! $isValid) {
2024-03-19 14:37:16 +00:00
$this->dispatch('error', 'Invalid Cron / Human expression.');
2024-06-10 20:43:34 +00:00
return;
2024-03-19 14:37:16 +00:00
}
if (empty($this->container) || $this->container === 'null') {
if ($this->type === 'service') {
$this->container = $this->subServiceName;
}
}
$this->saveScheduledTask();
2024-03-19 14:37:16 +00:00
$this->clear();
} catch (\Exception $e) {
2024-03-19 14:37:16 +00:00
return handleError($e, $this);
2024-01-01 18:33:16 +00:00
}
}
public function saveScheduledTask()
{
try {
$task = new ScheduledTask;
$task->name = $this->name;
$task->command = $this->command;
$task->frequency = $this->frequency;
$task->container = $this->container;
$task->timeout = $this->timeout;
$task->team_id = currentTeam()->id;
switch ($this->type) {
case 'application':
$task->application_id = $this->id;
break;
case 'standalone-postgresql':
$task->standalone_postgresql_id = $this->id;
break;
case 'service':
$task->service_id = $this->id;
break;
}
$task->save();
$this->dispatch('refreshTasks');
$this->dispatch('success', 'Scheduled task added.');
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
2024-01-01 18:33:16 +00:00
public function clear()
{
$this->name = '';
$this->command = '';
$this->frequency = '';
$this->container = '';
$this->timeout = 300;
2024-01-01 18:33:16 +00:00
}
}