coolify/app/Livewire/ActivityMonitor.php

110 lines
3.3 KiB
PHP
Raw Normal View History

2023-05-03 05:23:45 +00:00
<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire;
2023-05-03 05:23:45 +00:00
use App\Models\User;
2023-05-03 05:23:45 +00:00
use Livewire\Component;
use Spatie\Activitylog\Models\Activity;
class ActivityMonitor extends Component
{
2023-12-08 12:55:55 +00:00
public ?string $header = null;
2024-06-10 20:43:34 +00:00
public $activityId = null;
2024-06-10 20:43:34 +00:00
public $eventToDispatch = 'activityFinished';
2024-06-10 20:43:34 +00:00
public $eventData = null;
2023-05-03 05:23:45 +00:00
public $isPollingActive = false;
2024-06-10 20:43:34 +00:00
public bool $fullHeight = false;
2024-06-10 20:43:34 +00:00
public $activity;
2024-06-10 20:43:34 +00:00
public bool $showWaiting = true;
public static $eventDispatched = false;
protected $listeners = ['activityMonitor' => 'newMonitorActivity'];
2023-05-03 05:23:45 +00:00
public function newMonitorActivity($activityId, $eventToDispatch = 'activityFinished', $eventData = null, $header = null)
2023-05-03 05:23:45 +00:00
{
// Reset event dispatched flag for new activity
self::$eventDispatched = false;
2023-05-03 05:23:45 +00:00
$this->activityId = $activityId;
$this->eventToDispatch = $eventToDispatch;
$this->eventData = $eventData;
2023-05-03 05:23:45 +00:00
// Update header if provided
if ($header !== null) {
$this->header = $header;
}
2023-05-03 05:23:45 +00:00
$this->hydrateActivity();
$this->isPollingActive = true;
}
public function hydrateActivity()
{
if ($this->activityId === null) {
$this->activity = null;
return;
}
$this->activity = Activity::find($this->activityId);
}
public function updatedActivityId($value)
{
if ($value) {
$this->hydrateActivity();
$this->isPollingActive = true;
self::$eventDispatched = false;
}
}
2023-05-03 05:23:45 +00:00
public function polling()
{
$this->hydrateActivity();
2023-05-08 07:16:50 +00:00
$exit_code = data_get($this->activity, 'properties.exitCode');
if ($exit_code !== null) {
$this->isPollingActive = false;
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) {
$teamId = data_get($this->activity, 'properties.team_id')
?? $user->currentTeam()?->id
?? $user->teams->first()?->id;
if ($teamId && ! self::$eventDispatched) {
if (filled($this->eventData)) {
$this->eventToDispatch::dispatch($teamId, $this->eventData);
} else {
$this->eventToDispatch::dispatch($teamId);
}
self::$eventDispatched = true;
}
}
2025-01-07 13:52:08 +00:00
return;
}
if (! self::$eventDispatched) {
if (filled($this->eventData)) {
$this->dispatch($this->eventToDispatch, $this->eventData);
} else {
$this->dispatch($this->eventToDispatch);
}
self::$eventDispatched = true;
}
}
2023-05-08 07:16:50 +00:00
}
2023-05-03 05:23:45 +00:00
}
}
}