2023-09-22 09:23:49 +00:00
< ? php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Project\Service ;
2023-09-22 09:23:49 +00:00
use App\Actions\Service\StartService ;
use App\Actions\Service\StopService ;
2024-06-10 20:43:34 +00:00
use App\Actions\Shared\PullImage ;
2023-12-08 12:07:42 +00:00
use App\Events\ServiceStatusChanged ;
2023-09-22 09:23:49 +00:00
use App\Models\Service ;
use Livewire\Component ;
2023-12-08 11:12:44 +00:00
use Spatie\Activitylog\Models\Activity ;
2023-09-22 09:23:49 +00:00
class Navbar extends Component
{
public Service $service ;
2024-06-10 20:43:34 +00:00
2023-09-22 09:23:49 +00:00
public array $parameters ;
2024-06-10 20:43:34 +00:00
2023-09-22 09:23:49 +00:00
public array $query ;
2024-06-10 20:43:34 +00:00
2023-12-08 11:12:44 +00:00
public $isDeploymentProgress = false ;
2024-03-21 11:44:32 +00:00
2024-09-04 12:34:46 +00:00
public $docker_cleanup = true ;
2024-04-12 10:44:49 +00:00
public function mount ()
{
if ( str ( $this -> service -> status ()) -> contains ( 'running' ) && is_null ( $this -> service -> config_hash )) {
ray ( 'isConfigurationChanged init' );
$this -> service -> isConfigurationChanged ( true );
$this -> dispatch ( 'configurationChanged' );
}
}
2024-06-10 20:43:34 +00:00
2023-12-08 11:12:44 +00:00
public function getListeners ()
{
2024-03-21 11:44:32 +00:00
$userId = auth () -> user () -> id ;
2024-06-10 20:43:34 +00:00
2023-12-08 11:12:44 +00:00
return [
2024-03-21 11:44:32 +00:00
" echo-private:user. { $userId } ,ServiceStatusChanged " => 'serviceStarted' ,
2023-12-08 11:12:44 +00:00
];
}
2024-06-10 20:43:34 +00:00
2024-04-12 10:44:49 +00:00
public function serviceStarted ()
{
2024-03-22 10:34:15 +00:00
$this -> dispatch ( 'success' , 'Service status changed.' );
2024-04-12 11:15:24 +00:00
if ( is_null ( $this -> service -> config_hash ) || $this -> service -> isConfigurationChanged ()) {
$this -> service -> isConfigurationChanged ( true );
$this -> dispatch ( 'configurationChanged' );
} else {
$this -> dispatch ( 'configurationChanged' );
}
2024-03-21 11:44:32 +00:00
}
2024-04-12 11:15:24 +00:00
2024-08-06 12:02:24 +00:00
public function check_status_without_notification ()
{
$this -> dispatch ( 'check_status' );
}
2024-03-21 11:44:32 +00:00
public function check_status ()
{
2024-03-04 07:57:18 +00:00
$this -> dispatch ( 'check_status' );
$this -> dispatch ( 'success' , 'Service status updated.' );
}
2024-06-10 20:43:34 +00:00
2024-03-01 09:36:32 +00:00
public function checkDeployments ()
2023-12-08 11:12:44 +00:00
{
2024-07-25 19:51:53 +00:00
try {
// TODO: This is a temporary solution. We need to refactor this.
// We need to delete null bytes somehow.
$activity = Activity :: where ( 'properties->type_uuid' , $this -> service -> uuid ) -> latest () -> first ();
$status = data_get ( $activity , 'properties.status' );
if ( $status === 'queued' || $status === 'in_progress' ) {
$this -> isDeploymentProgress = true ;
} else {
$this -> isDeploymentProgress = false ;
}
} catch ( \Throwable $e ) {
2024-03-01 09:36:32 +00:00
$this -> isDeploymentProgress = false ;
}
2023-11-05 08:49:23 +00:00
}
2024-06-10 20:43:34 +00:00
2024-03-21 11:44:32 +00:00
public function start ()
2023-09-22 09:23:49 +00:00
{
2023-12-08 11:12:44 +00:00
$this -> checkDeployments ();
if ( $this -> isDeploymentProgress ) {
$this -> dispatch ( 'error' , 'There is a deployment in progress.' );
2024-06-10 20:43:34 +00:00
2023-12-08 11:12:44 +00:00
return ;
}
2023-09-22 09:23:49 +00:00
$this -> service -> parse ();
$activity = StartService :: run ( $this -> service );
2024-02-05 13:40:54 +00:00
$this -> dispatch ( 'activityMonitor' , $activity -> id );
2023-09-22 09:23:49 +00:00
}
2024-06-10 20:43:34 +00:00
2024-09-04 12:34:46 +00:00
public function stop ()
2023-09-22 09:23:49 +00:00
{
2024-09-04 12:34:46 +00:00
StopService :: run ( $this -> service , false , $this -> docker_cleanup );
2023-12-08 17:32:08 +00:00
ServiceStatusChanged :: dispatch ();
2023-09-22 09:23:49 +00:00
}
2024-06-10 20:43:34 +00:00
2023-12-14 13:50:38 +00:00
public function restart ()
{
$this -> checkDeployments ();
if ( $this -> isDeploymentProgress ) {
$this -> dispatch ( 'error' , 'There is a deployment in progress.' );
2024-06-10 20:43:34 +00:00
2023-12-14 13:50:38 +00:00
return ;
}
PullImage :: run ( $this -> service );
StopService :: run ( $this -> service );
$this -> service -> parse ();
2024-03-21 11:44:32 +00:00
$this -> dispatch ( 'imagePulled' );
2023-12-14 13:50:38 +00:00
$activity = StartService :: run ( $this -> service );
2024-02-05 13:40:54 +00:00
$this -> dispatch ( 'activityMonitor' , $activity -> id );
2023-12-14 13:50:38 +00:00
}
2024-09-04 12:34:46 +00:00
public function render ()
{
return view ( 'livewire.project.service.navbar' , [
'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-09-22 09:23:49 +00:00
}