coolify/app/Livewire/ActivityMonitor.php

83 lines
2.3 KiB
PHP
Raw Normal View History

2023-05-03 07:23:45 +02:00
<?php
2023-12-07 19:06:32 +01:00
namespace App\Livewire;
2023-05-03 07:23:45 +02:00
2023-05-08 09:16:50 +02:00
use App\Enums\ProcessStatus;
use App\Models\User;
2023-05-03 07:23:45 +02:00
use Livewire\Component;
use Spatie\Activitylog\Models\Activity;
class ActivityMonitor extends Component
{
2023-12-08 13:55:55 +01:00
public ?string $header = null;
2024-06-10 22:43:34 +02:00
2023-05-03 07:23:45 +02:00
public $activityId;
2024-06-10 22:43:34 +02:00
public $eventToDispatch = 'activityFinished';
2024-06-10 22:43:34 +02:00
2023-05-03 07:23:45 +02:00
public $isPollingActive = false;
2024-06-10 22:43:34 +02:00
public bool $fullHeight = false;
2024-06-10 22:43:34 +02:00
2024-03-22 11:34:15 +01:00
public bool $showWaiting = false;
2023-05-03 07:23:45 +02:00
protected $activity;
2024-06-10 22:43:34 +02:00
protected $listeners = ['activityMonitor' => 'newMonitorActivity'];
2023-05-03 07:23:45 +02:00
public function newMonitorActivity($activityId, $eventToDispatch = 'activityFinished')
2023-05-03 07:23:45 +02:00
{
$this->activityId = $activityId;
$this->eventToDispatch = $eventToDispatch;
2023-05-03 07:23:45 +02:00
$this->hydrateActivity();
$this->isPollingActive = true;
}
public function hydrateActivity()
{
2023-12-08 13:55:55 +01:00
$this->activity = Activity::find($this->activityId);
}
2023-05-03 07:23:45 +02:00
public function polling()
{
$this->hydrateActivity();
2023-12-08 13:55:55 +01:00
// $this->setStatus(ProcessStatus::IN_PROGRESS);
2023-05-08 09:16:50 +02:00
$exit_code = data_get($this->activity, 'properties.exitCode');
if ($exit_code !== null) {
// if ($exit_code === 0) {
// // $this->setStatus(ProcessStatus::FINISHED);
// } else {
// // $this->setStatus(ProcessStatus::ERROR);
// }
$this->isPollingActive = false;
2023-05-08 09:16:50 +02:00
if ($exit_code === 0) {
if ($this->eventToDispatch !== null) {
if (str($this->eventToDispatch)->startsWith('App\\Events\\')) {
$causer_id = data_get($this->activity, 'causer_id');
$user = User::find($causer_id);
if ($user) {
2024-06-10 22:43:34 +02:00
foreach ($user->teams as $team) {
$teamId = $team->id;
$this->eventToDispatch::dispatch($teamId);
}
}
2024-06-10 22:43:34 +02:00
return;
}
$this->dispatch($this->eventToDispatch);
}
2023-05-08 09:16:50 +02:00
}
2023-05-03 07:23:45 +02:00
}
}
2023-12-08 13:55:55 +01:00
// protected function setStatus($status)
// {
// $this->activity->properties = $this->activity->properties->merge([
// 'status' => $status,
// ]);
// $this->activity->save();
// }
}