2023-08-07 16:46:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
2023-12-07 18:06:32 +00:00
|
|
|
namespace App\Livewire\Project\Database;
|
2023-08-07 16:46:40 +00:00
|
|
|
|
2024-07-02 11:39:44 +00:00
|
|
|
use App\Actions\Database\RestartDatabase;
|
|
|
|
|
use App\Actions\Database\StartDatabase;
|
2023-10-14 12:22:07 +00:00
|
|
|
use App\Actions\Database\StopDatabase;
|
2024-05-07 13:41:50 +00:00
|
|
|
use App\Actions\Docker\GetContainersStatus;
|
2025-05-19 19:50:32 +00:00
|
|
|
use App\Events\ServiceStatusChanged;
|
2025-08-23 16:50:35 +00:00
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
2023-08-08 09:51:36 +00:00
|
|
|
use Livewire\Component;
|
2023-08-07 16:46:40 +00:00
|
|
|
|
|
|
|
|
class Heading extends Component
|
|
|
|
|
{
|
2025-08-23 16:50:35 +00:00
|
|
|
use AuthorizesRequests;
|
|
|
|
|
|
2023-08-07 16:46:40 +00:00
|
|
|
public $database;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-07 16:46:40 +00:00
|
|
|
public array $parameters;
|
|
|
|
|
|
2024-09-04 12:59:44 +00:00
|
|
|
public $docker_cleanup = true;
|
|
|
|
|
|
2023-12-11 08:02:53 +00:00
|
|
|
public function getListeners()
|
|
|
|
|
{
|
2025-05-19 19:50:32 +00:00
|
|
|
$teamId = auth()->user()->currentTeam()->id;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-12-11 08:02:53 +00:00
|
|
|
return [
|
2025-05-19 19:50:32 +00:00
|
|
|
"echo-private:team.{$teamId},ServiceStatusChanged" => 'checkStatus',
|
|
|
|
|
"echo-private:team.{$teamId},ServiceChecked" => 'activityFinished',
|
|
|
|
|
'refresh' => '$refresh',
|
|
|
|
|
'compose_loaded' => '$refresh',
|
|
|
|
|
'update_links' => '$refresh',
|
2023-12-11 08:02:53 +00:00
|
|
|
];
|
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
|
|
|
|
public function activityFinished()
|
|
|
|
|
{
|
2025-05-19 19:50:32 +00:00
|
|
|
try {
|
2025-08-18 10:09:58 +00:00
|
|
|
// Only set started_at if database is actually running
|
|
|
|
|
if ($this->database->isRunning()) {
|
|
|
|
|
$this->database->started_at ??= now();
|
|
|
|
|
}
|
2025-05-20 08:33:55 +00:00
|
|
|
$this->database->save();
|
2025-05-19 19:50:32 +00:00
|
|
|
|
|
|
|
|
if (is_null($this->database->config_hash) || $this->database->isConfigurationChanged()) {
|
|
|
|
|
$this->database->isConfigurationChanged(true);
|
|
|
|
|
}
|
2025-05-20 08:33:55 +00:00
|
|
|
$this->dispatch('configurationChanged');
|
2025-05-19 19:50:32 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return handleError($e, $this);
|
|
|
|
|
} finally {
|
|
|
|
|
$this->dispatch('refresh');
|
2024-04-12 10:44:49 +00:00
|
|
|
}
|
2023-08-07 20:14:21 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2025-05-19 19:50:32 +00:00
|
|
|
public function checkStatus()
|
2023-08-07 20:14:21 +00:00
|
|
|
{
|
2025-02-22 11:51:22 +00:00
|
|
|
if ($this->database->destination->server->isFunctional()) {
|
2025-05-19 19:50:32 +00:00
|
|
|
GetContainersStatus::dispatch($this->database->destination->server);
|
2025-05-20 13:22:13 +00:00
|
|
|
} else {
|
|
|
|
|
$this->dispatch('error', 'Server is not functional.');
|
2024-06-10 20:43:34 +00:00
|
|
|
}
|
2023-08-07 20:14:21 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2025-10-24 10:58:52 +00:00
|
|
|
public function manualCheckStatus()
|
|
|
|
|
{
|
|
|
|
|
$this->checkStatus();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 16:46:40 +00:00
|
|
|
public function mount()
|
|
|
|
|
{
|
2026-02-28 17:37:51 +00:00
|
|
|
$this->parameters = [
|
|
|
|
|
'project_uuid' => $this->database->environment->project->uuid,
|
|
|
|
|
'environment_uuid' => $this->database->environment->uuid,
|
|
|
|
|
'database_uuid' => $this->database->uuid,
|
|
|
|
|
];
|
2023-08-07 16:46:40 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
|
|
|
|
public function stop()
|
|
|
|
|
{
|
2025-05-19 19:50:32 +00:00
|
|
|
try {
|
2025-08-23 16:50:35 +00:00
|
|
|
$this->authorize('manage', $this->database);
|
|
|
|
|
|
2025-06-05 11:33:02 +00:00
|
|
|
$this->dispatch('info', 'Gracefully stopping database.');
|
2025-05-19 19:50:32 +00:00
|
|
|
StopDatabase::dispatch($this->database, false, $this->docker_cleanup);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$this->dispatch('error', $e->getMessage());
|
|
|
|
|
}
|
2023-08-07 20:14:21 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2024-07-02 11:39:44 +00:00
|
|
|
public function restart()
|
|
|
|
|
{
|
2025-08-23 16:50:35 +00:00
|
|
|
$this->authorize('manage', $this->database);
|
|
|
|
|
|
2024-07-02 11:39:44 +00:00
|
|
|
$activity = RestartDatabase::run($this->database);
|
2025-05-19 19:50:32 +00:00
|
|
|
$this->dispatch('activityMonitor', $activity->id, ServiceStatusChanged::class);
|
2024-07-02 11:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-08 09:51:36 +00:00
|
|
|
public function start()
|
|
|
|
|
{
|
2025-08-23 16:50:35 +00:00
|
|
|
$this->authorize('manage', $this->database);
|
|
|
|
|
|
2024-07-02 11:39:44 +00:00
|
|
|
$activity = StartDatabase::run($this->database);
|
2025-05-19 19:50:32 +00:00
|
|
|
$this->dispatch('activityMonitor', $activity->id, ServiceStatusChanged::class);
|
2023-08-07 16:46:40 +00:00
|
|
|
}
|
2024-09-04 12:59:44 +00:00
|
|
|
|
|
|
|
|
public function render()
|
|
|
|
|
{
|
|
|
|
|
return view('livewire.project.database.heading', [
|
|
|
|
|
'checkboxes' => [
|
2024-09-27 15:29:36 +00:00
|
|
|
['id' => 'docker_cleanup', 'label' => __('resource.docker_cleanup')],
|
2024-09-18 19:24:42 +00:00
|
|
|
],
|
2024-09-04 12:59:44 +00:00
|
|
|
]);
|
|
|
|
|
}
|
2023-08-07 20:14:21 +00:00
|
|
|
}
|