2024-01-01 18:33:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Livewire\Project\Shared\ScheduledTask;
|
|
|
|
|
2024-05-14 13:19:28 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2024-01-01 18:33:16 +00:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
class Add extends Component
|
|
|
|
{
|
|
|
|
public $parameters;
|
2024-05-14 13:19:28 +00:00
|
|
|
public string $type;
|
|
|
|
public Collection $containerNames;
|
2024-01-01 18:33:16 +00:00
|
|
|
public string $name;
|
|
|
|
public string $command;
|
|
|
|
public string $frequency;
|
|
|
|
public ?string $container = '';
|
|
|
|
|
|
|
|
protected $listeners = ['clearScheduledTask' => 'clear'];
|
|
|
|
protected $rules = [
|
|
|
|
'name' => 'required|string',
|
|
|
|
'command' => 'required|string',
|
|
|
|
'frequency' => 'required|string',
|
|
|
|
'container' => 'nullable|string',
|
|
|
|
];
|
|
|
|
protected $validationAttributes = [
|
|
|
|
'name' => 'name',
|
|
|
|
'command' => 'command',
|
|
|
|
'frequency' => 'frequency',
|
|
|
|
'container' => 'container',
|
|
|
|
];
|
|
|
|
|
|
|
|
public function mount()
|
|
|
|
{
|
|
|
|
$this->parameters = get_route_parameters();
|
2024-05-14 13:19:28 +00:00
|
|
|
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->validate();
|
|
|
|
$isValid = validate_cron_expression($this->frequency);
|
|
|
|
if (!$isValid) {
|
|
|
|
$this->dispatch('error', 'Invalid Cron / Human expression.');
|
|
|
|
return;
|
|
|
|
}
|
2024-05-14 13:19:28 +00:00
|
|
|
if (empty($this->container) || $this->container == 'null') {
|
|
|
|
if ($this->type == 'service') {
|
|
|
|
$this->container = $this->subServiceName;
|
|
|
|
}
|
|
|
|
}
|
2024-03-19 14:37:16 +00:00
|
|
|
$this->dispatch('saveScheduledTask', [
|
|
|
|
'name' => $this->name,
|
|
|
|
'command' => $this->command,
|
|
|
|
'frequency' => $this->frequency,
|
|
|
|
'container' => $this->container,
|
|
|
|
]);
|
|
|
|
$this->clear();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return handleError($e, $this);
|
2024-01-01 18:33:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function clear()
|
|
|
|
{
|
|
|
|
$this->name = '';
|
|
|
|
$this->command = '';
|
|
|
|
$this->frequency = '';
|
|
|
|
$this->container = '';
|
|
|
|
}
|
|
|
|
}
|