refactor scheduled task job (and related stuffs)

This commit is contained in:
Andras Bacsai
2024-11-07 11:09:38 +01:00
parent 8e3469bdff
commit 376a2341af
9 changed files with 220 additions and 87 deletions

View File

@@ -2,74 +2,124 @@
namespace App\Livewire\Project\Shared\ScheduledTask;
use App\Jobs\ScheduledTaskJob;
use App\Models\Application;
use App\Models\ScheduledTask as ModelsScheduledTask;
use App\Models\ScheduledTask;
use App\Models\Service;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Validate;
use Livewire\Component;
use Visus\Cuid2\Cuid2;
class Show extends Component
{
public $parameters;
public Application|Service $resource;
public ModelsScheduledTask $task;
public ScheduledTask $task;
public ?string $modalId = null;
#[Locked]
public array $parameters;
#[Locked]
public string $type;
public string $scheduledTaskName;
#[Validate(['boolean'])]
public bool $isEnabled = false;
protected $rules = [
'task.enabled' => 'required|boolean',
'task.name' => 'required|string',
'task.command' => 'required|string',
'task.frequency' => 'required|string',
'task.container' => 'nullable|string',
];
#[Validate(['string', 'required'])]
public string $name;
protected $validationAttributes = [
'name' => 'name',
'command' => 'command',
'frequency' => 'frequency',
'container' => 'container',
];
#[Validate(['string', 'required'])]
public string $command;
public function mount()
#[Validate(['string', 'required'])]
public string $frequency;
#[Validate(['string', 'nullable'])]
public ?string $container = null;
#[Locked]
public ?string $application_uuid;
#[Locked]
public ?string $service_uuid;
#[Locked]
public string $task_uuid;
public function mount(string $task_uuid, string $project_uuid, string $environment_name, ?string $application_uuid = null, ?string $service_uuid = null)
{
$this->parameters = get_route_parameters();
try {
$this->task_uuid = $task_uuid;
if ($application_uuid) {
$this->type = 'application';
$this->application_uuid = $application_uuid;
$this->resource = Application::ownedByCurrentTeam()->where('uuid', $application_uuid)->firstOrFail();
} elseif ($service_uuid) {
$this->type = 'service';
$this->service_uuid = $service_uuid;
$this->resource = Service::ownedByCurrentTeam()->where('uuid', $service_uuid)->firstOrFail();
}
$this->parameters = [
'environment_name' => $environment_name,
'project_uuid' => $project_uuid,
'application_uuid' => $application_uuid,
'service_uuid' => $service_uuid,
];
if (data_get($this->parameters, 'application_uuid')) {
$this->type = 'application';
$this->resource = Application::where('uuid', $this->parameters['application_uuid'])->firstOrFail();
} elseif (data_get($this->parameters, 'service_uuid')) {
$this->type = 'service';
$this->resource = Service::where('uuid', $this->parameters['service_uuid'])->firstOrFail();
$this->task = $this->resource->scheduled_tasks()->where('uuid', $task_uuid)->firstOrFail();
$this->syncData();
} catch (\Exception $e) {
return handleError($e);
}
}
$this->modalId = new Cuid2;
$this->task = ModelsScheduledTask::where('uuid', request()->route('task_uuid'))->first();
$this->scheduledTaskName = $this->task->name;
public function syncData(bool $toModel = false)
{
if ($toModel) {
$this->validate();
$this->task->enabled = $this->isEnabled;
$this->task->name = str($this->name)->trim()->value();
$this->task->command = str($this->command)->trim()->value();
$this->task->frequency = str($this->frequency)->trim()->value();
$this->task->container = str($this->container)->trim()->value();
$this->task->save();
} else {
$this->isEnabled = $this->task->enabled;
$this->name = $this->task->name;
$this->command = $this->task->command;
$this->frequency = $this->task->frequency;
$this->container = $this->task->container;
}
}
public function instantSave()
{
$this->validateOnly('task.enabled');
$this->task->save(['enabled' => $this->task->enabled]);
$this->dispatch('success', 'Scheduled task updated.');
$this->dispatch('refreshTasks');
try {
$this->syncData(true);
$this->dispatch('success', 'Scheduled task updated.');
$this->refreshTasks();
} catch (\Exception $e) {
return handleError($e);
}
}
public function submit()
{
$this->validate();
$this->task->name = str($this->task->name)->trim()->value();
$this->task->container = str($this->task->container)->trim()->value();
$this->task->save();
$this->dispatch('success', 'Scheduled task updated.');
$this->dispatch('refreshTasks');
try {
$this->syncData(true);
$this->dispatch('success', 'Scheduled task updated.');
} catch (\Exception $e) {
return handleError($e);
}
}
public function refreshTasks()
{
try {
$this->task->refresh();
} catch (\Exception $e) {
return handleError($e);
}
}
public function delete()
@@ -78,12 +128,22 @@ class Show extends Component
$this->task->delete();
if ($this->type === 'application') {
return redirect()->route('project.application.configuration', $this->parameters, $this->scheduledTaskName);
return redirect()->route('project.application.configuration', $this->parameters, $this->task->name);
} else {
return redirect()->route('project.service.configuration', $this->parameters, $this->scheduledTaskName);
return redirect()->route('project.service.configuration', $this->parameters, $this->task->name);
}
} catch (\Exception $e) {
return handleError($e);
}
}
public function executeNow()
{
try {
ScheduledTaskJob::dispatch($this->task);
$this->dispatch('success', 'Scheduled task executed.');
} catch (\Exception $e) {
return handleError($e);
}
}
}