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;
|
2024-01-16 14:19:14 +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;
|
2023-05-03 05:23:45 +00:00
|
|
|
public $activityId;
|
2024-01-16 14:19:14 +00:00
|
|
|
public $eventToDispatch = 'activityFinished';
|
2023-05-03 05:23:45 +00:00
|
|
|
public $isPollingActive = false;
|
|
|
|
|
|
|
|
protected $activity;
|
2024-02-05 13:40:54 +00:00
|
|
|
protected $listeners = ['activityMonitor' => 'newMonitorActivity'];
|
2023-05-03 05:23:45 +00:00
|
|
|
|
2024-01-16 14:19:14 +00:00
|
|
|
public function newMonitorActivity($activityId, $eventToDispatch = 'activityFinished')
|
2023-05-03 05:23:45 +00:00
|
|
|
{
|
|
|
|
$this->activityId = $activityId;
|
2024-01-16 14:19:14 +00:00
|
|
|
$this->eventToDispatch = $eventToDispatch;
|
2023-05-03 05:23:45 +00:00
|
|
|
|
|
|
|
$this->hydrateActivity();
|
|
|
|
|
|
|
|
$this->isPollingActive = true;
|
|
|
|
}
|
|
|
|
|
2023-08-08 09:51:36 +00:00
|
|
|
public function hydrateActivity()
|
|
|
|
{
|
2023-12-08 12:55:55 +00:00
|
|
|
$this->activity = Activity::find($this->activityId);
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2024-01-16 14:19:14 +00:00
|
|
|
// 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) {
|
2024-01-16 14:19:14 +00:00
|
|
|
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-08-08 09:51:36 +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();
|
|
|
|
// }
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|