refactor(scheduled-tasks): Improve scheduled task creation and management
This commit is contained in:
@@ -2,15 +2,22 @@
|
||||
|
||||
namespace App\Livewire\Project\Shared\ScheduledTask;
|
||||
|
||||
use App\Models\ScheduledTask;
|
||||
use Illuminate\Support\Collection;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Component;
|
||||
|
||||
class Add extends Component
|
||||
{
|
||||
public $parameters;
|
||||
|
||||
#[Locked]
|
||||
public string $id;
|
||||
|
||||
#[Locked]
|
||||
public string $type;
|
||||
|
||||
#[Locked]
|
||||
public Collection $containerNames;
|
||||
|
||||
public string $name;
|
||||
@@ -21,8 +28,6 @@ class Add extends Component
|
||||
|
||||
public ?string $container = '';
|
||||
|
||||
protected $listeners = ['clearScheduledTask' => 'clear'];
|
||||
|
||||
protected $rules = [
|
||||
'name' => 'required|string',
|
||||
'command' => 'required|string',
|
||||
@@ -60,18 +65,42 @@ class Add extends Component
|
||||
$this->container = $this->subServiceName;
|
||||
}
|
||||
}
|
||||
$this->dispatch('saveScheduledTask', [
|
||||
'name' => $this->name,
|
||||
'command' => $this->command,
|
||||
'frequency' => $this->frequency,
|
||||
'container' => $this->container,
|
||||
]);
|
||||
$this->saveScheduledTask();
|
||||
$this->clear();
|
||||
} catch (\Exception $e) {
|
||||
return handleError($e, $this);
|
||||
}
|
||||
}
|
||||
|
||||
public function saveScheduledTask()
|
||||
{
|
||||
try {
|
||||
$task = new ScheduledTask();
|
||||
$task->name = $this->name;
|
||||
$task->command = $this->command;
|
||||
$task->frequency = $this->frequency;
|
||||
$task->container = $this->container;
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
public function clear()
|
||||
{
|
||||
$this->name = '';
|
||||
|
||||
@@ -4,20 +4,22 @@ namespace App\Livewire\Project\Shared\ScheduledTask;
|
||||
|
||||
use App\Models\ScheduledTask;
|
||||
use Illuminate\Support\Collection;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\Component;
|
||||
|
||||
class All extends Component
|
||||
{
|
||||
#[Locked]
|
||||
public $resource;
|
||||
|
||||
#[Locked]
|
||||
public array $parameters;
|
||||
|
||||
public Collection $containerNames;
|
||||
|
||||
public ?string $variables = null;
|
||||
|
||||
public array $parameters;
|
||||
|
||||
protected $listeners = ['refreshTasks', 'saveScheduledTask' => 'submit'];
|
||||
|
||||
public function mount()
|
||||
{
|
||||
$this->parameters = get_route_parameters();
|
||||
@@ -35,37 +37,10 @@ class All extends Component
|
||||
}
|
||||
}
|
||||
|
||||
#[On('refreshTasks')]
|
||||
public function refreshTasks()
|
||||
{
|
||||
$this->resource->refresh();
|
||||
}
|
||||
|
||||
public function submit($data)
|
||||
{
|
||||
try {
|
||||
$task = new ScheduledTask;
|
||||
$task->name = $data['name'];
|
||||
$task->command = $data['command'];
|
||||
$task->frequency = $data['frequency'];
|
||||
$task->container = $data['container'];
|
||||
$task->team_id = currentTeam()->id;
|
||||
|
||||
switch ($this->resource->type()) {
|
||||
case 'application':
|
||||
$task->application_id = $this->resource->id;
|
||||
break;
|
||||
case 'standalone-postgresql':
|
||||
$task->standalone_postgresql_id = $this->resource->id;
|
||||
break;
|
||||
case 'service':
|
||||
$task->service_id = $this->resource->id;
|
||||
break;
|
||||
}
|
||||
$task->save();
|
||||
$this->refreshTasks();
|
||||
$this->dispatch('success', 'Scheduled task added.');
|
||||
} catch (\Throwable $e) {
|
||||
return handleError($e, $this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
<h2>Scheduled Tasks</h2>
|
||||
<x-modal-input buttonTitle="+ Add" title="New Scheduled Task" :closeOutside="false">
|
||||
@if ($resource->type() == 'application')
|
||||
<livewire:project.shared.scheduled-task.add :type="$resource->type()" :containerNames="$containerNames" />
|
||||
<livewire:project.shared.scheduled-task.add :type="$resource->type()" :id="$resource->id" :containerNames="$containerNames" />
|
||||
@elseif ($resource->type() == 'service')
|
||||
<livewire:project.shared.scheduled-task.add :type="$resource->type()" :containerNames="$containerNames" />
|
||||
<livewire:project.shared.scheduled-task.add :type="$resource->type()" :id="$resource->id" :containerNames="$containerNames" />
|
||||
@endif
|
||||
</x-modal-input>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user