lasthourcloud/app/Livewire/ActivityMonitor.php

76 lines
2.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
2023-05-08 07:16:50 +00:00
use App\Enums\ProcessStatus;
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;
2023-05-03 05:23:45 +00:00
public $activityId;
public $eventToDispatch = 'activityFinished';
2023-05-03 05:23:45 +00:00
public $isPollingActive = false;
public bool $fullHeight = false;
2024-03-22 10:34:15 +00:00
public bool $showWaiting = false;
2023-05-03 05:23:45 +00:00
protected $activity;
protected $listeners = ['activityMonitor' => 'newMonitorActivity'];
2023-05-03 05:23:45 +00:00
public function newMonitorActivity($activityId, $eventToDispatch = 'activityFinished')
2023-05-03 05:23:45 +00:00
{
$this->activityId = $activityId;
$this->eventToDispatch = $eventToDispatch;
2023-05-03 05:23:45 +00:00
$this->hydrateActivity();
$this->isPollingActive = true;
}
public function hydrateActivity()
{
2023-12-08 12:55:55 +00:00
$this->activity = Activity::find($this->activityId);
}
2023-05-03 05:23:45 +00:00
public function polling()
{
$this->hydrateActivity();
2023-12-08 12:55:55 +00:00
// $this->setStatus(ProcessStatus::IN_PROGRESS);
2023-05-08 07:16:50 +00: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 07:16:50 +00: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) {
foreach($user->teams as $team) {
$teamId = $team->id;
$this->eventToDispatch::dispatch($teamId);
}
}
return;
}
$this->dispatch($this->eventToDispatch);
}
2023-05-08 07:16:50 +00:00
}
2023-05-03 05:23:45 +00:00
}
}
2023-12-08 12:55:55 +00:00
// protected function setStatus($status)
// {
// $this->activity->properties = $this->activity->properties->merge([
// 'status' => $status,
// ]);
// $this->activity->save();
// }
}