fix: improve scheduled task adding/removing

This commit is contained in:
Andras Bacsai
2024-05-14 15:19:28 +02:00
parent f06065337c
commit 317dc10af4
12 changed files with 123 additions and 56 deletions

View File

@@ -77,8 +77,12 @@ class ScheduledTaskJob implements ShouldQueue
$this->containers[] = data_get($application, 'name') . '-' . data_get($this->resource, 'uuid');
}
});
$this->resource->databases()->get()->each(function ($database) {
if (str(data_get($database, 'status'))->contains('running')) {
$this->containers[] = data_get($database, 'name') . '-' . data_get($this->resource, 'uuid');
}
});
}
if (count($this->containers) == 0) {
throw new \Exception('ScheduledTaskJob failed: No containers running.');
}

View File

@@ -3,7 +3,6 @@
namespace App\Livewire\Project\Service;
use App\Actions\Docker\GetContainersStatus;
use App\Jobs\ContainerStatusJob;
use App\Models\Service;
use Livewire\Component;

View File

@@ -2,11 +2,14 @@
namespace App\Livewire\Project\Shared\ScheduledTask;
use Illuminate\Support\Collection;
use Livewire\Component;
class Add extends Component
{
public $parameters;
public string $type;
public Collection $containerNames;
public string $name;
public string $command;
public string $frequency;
@@ -29,6 +32,9 @@ class Add extends Component
public function mount()
{
$this->parameters = get_route_parameters();
if ($this->containerNames->count() > 0) {
$this->container = $this->containerNames->first();
}
}
public function submit()
@@ -40,6 +46,11 @@ class Add extends Component
$this->dispatch('error', 'Invalid Cron / Human expression.');
return;
}
if (empty($this->container) || $this->container == 'null') {
if ($this->type == 'service') {
$this->container = $this->subServiceName;
}
}
$this->dispatch('saveScheduledTask', [
'name' => $this->name,
'command' => $this->command,

View File

@@ -3,14 +3,13 @@
namespace App\Livewire\Project\Shared\ScheduledTask;
use App\Models\ScheduledTask;
use Illuminate\Support\Collection;
use Livewire\Component;
use Visus\Cuid2\Cuid2;
use Illuminate\Support\Str;
class All extends Component
{
public $resource;
public string|null $modalId = null;
public Collection $containerNames;
public ?string $variables = null;
public array $parameters;
protected $listeners = ['refreshTasks', 'saveScheduledTask' => 'submit'];
@@ -18,7 +17,19 @@ class All extends Component
public function mount()
{
$this->parameters = get_route_parameters();
$this->modalId = new Cuid2(7);
if ($this->resource->type() == 'service') {
$this->containerNames = $this->resource->applications()->pluck('name');
$this->containerNames = $this->containerNames->merge($this->resource->databases()->pluck('name'));
ray($this->containerNames);
} elseif ($this->resource->type() == 'application') {
if ($this->resource->build_pack === 'dockercompose') {
$parsed = $this->resource->parseCompose();
$containers = collect($parsed['services'])->keys();
$this->containerNames = $containers;
} else {
$this->containerNames = collect([]);
}
}
}
public function refreshTasks()
{

View File

@@ -17,6 +17,7 @@ class Show extends Component
public string $type;
protected $rules = [
'task.enabled' => 'required|boolean',
'task.name' => 'required|string',
'task.command' => 'required|string',
'task.frequency' => 'required|string',
@@ -45,9 +46,18 @@ class Show extends Component
$this->task = ModelsScheduledTask::where('uuid', request()->route('task_uuid'))->first();
}
public function instantSave()
{
$this->validateOnly('task.enabled');
$this->task->save(['enabled' => $this->task->enabled]);
$this->dispatch('success', 'Scheduled task updated.');
$this->dispatch('refreshTasks');
}
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');
@@ -60,11 +70,9 @@ class Show extends Component
if ($this->type == 'application') {
return redirect()->route('project.application.configuration', $this->parameters);
}
else {
} else {
return redirect()->route('project.service.configuration', $this->parameters);
}
} catch (\Exception $e) {
return handleError($e);
}

View File

@@ -677,6 +677,17 @@ class Service extends BaseModel
{
return $this->belongsTo(Server::class);
}
public function byUuid(string $uuid) {
$app = $this->applications()->whereUuid($uuid)->first();
if ($app) {
return $app;
}
$db = $this->databases()->whereUuid($uuid)->first();
if ($db) {
return $db;
}
return null;
}
public function byName(string $name)
{
$app = $this->applications()->whereName($name)->first();