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 ;
2023-08-08 09:51:36 +00:00
use Livewire\Component ;
2023-08-07 16:46:40 +00:00
class Heading extends Component
{
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 ()
{
$userId = auth () -> user () -> id ;
2024-06-10 20:43:34 +00:00
2023-12-11 08:02:53 +00:00
return [
2023-12-11 09:23:10 +00:00
" echo-private:user. { $userId } ,DatabaseStatusChanged " => 'activityFinished' ,
2023-12-11 08:02:53 +00:00
];
}
2023-08-08 09:51:36 +00:00
public function activityFinished ()
{
2023-08-07 20:14:21 +00:00
$this -> database -> update ([
'started_at' => now (),
]);
2023-12-07 18:06:32 +00:00
$this -> dispatch ( 'refresh' );
2023-08-07 20:14:21 +00:00
$this -> check_status ();
2024-04-12 10:44:49 +00:00
if ( is_null ( $this -> database -> config_hash ) || $this -> database -> isConfigurationChanged ()) {
$this -> database -> isConfigurationChanged ( true );
$this -> dispatch ( 'configurationChanged' );
} else {
$this -> dispatch ( 'configurationChanged' );
}
2023-08-07 20:14:21 +00:00
}
2023-08-08 09:51:36 +00:00
2023-12-11 12:43:16 +00:00
public function check_status ( $showNotification = false )
2023-08-07 20:14:21 +00:00
{
2024-05-08 07:23:36 +00:00
GetContainersStatus :: run ( $this -> database -> destination -> server );
2023-08-07 20:14:21 +00:00
$this -> database -> refresh ();
2024-06-10 20:43:34 +00:00
if ( $showNotification ) {
$this -> dispatch ( 'success' , 'Database status updated.' );
}
2023-08-07 20:14:21 +00:00
}
2023-08-08 09:51:36 +00:00
2023-08-07 16:46:40 +00:00
public function mount ()
{
2023-08-09 13:57:53 +00:00
$this -> parameters = get_route_parameters ();
2023-08-07 16:46:40 +00:00
}
2023-08-08 09:51:36 +00:00
public function stop ()
{
2024-09-04 12:59:44 +00:00
StopDatabase :: run ( $this -> database , false , $this -> docker_cleanup );
2023-09-18 10:38:11 +00:00
$this -> database -> status = 'exited' ;
2023-08-07 20:14:21 +00:00
$this -> database -> save ();
2023-09-15 13:34:25 +00:00
$this -> check_status ();
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 ()
{
$activity = RestartDatabase :: run ( $this -> database );
$this -> dispatch ( 'activityMonitor' , $activity -> id );
}
2023-08-08 09:51:36 +00:00
public function start ()
{
2024-07-02 11:39:44 +00:00
$activity = StartDatabase :: run ( $this -> database );
$this -> dispatch ( 'activityMonitor' , $activity -> id );
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' => [
[ 'id' => 'docker_cleanup' , 'label' => 'Docker cleanup will be run on the server which removes builder cache and unused images (the next deployment will take longer as the images have to be pulled again)' ],
]
]);
}
2023-08-07 20:14:21 +00:00
}