2024-02-05 13:40:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Livewire;
|
|
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
use Livewire\Component;
|
|
|
|
use Spatie\Activitylog\Models\Activity;
|
|
|
|
|
|
|
|
class NewActivityMonitor extends Component
|
|
|
|
{
|
|
|
|
public ?string $header = null;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-05 13:40:54 +00:00
|
|
|
public $activityId;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-05 13:40:54 +00:00
|
|
|
public $eventToDispatch = 'activityFinished';
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-04-16 13:42:38 +00:00
|
|
|
public $eventData = null;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-05 13:40:54 +00:00
|
|
|
public $isPollingActive = false;
|
|
|
|
|
|
|
|
protected $activity;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-05 13:40:54 +00:00
|
|
|
protected $listeners = ['newActivityMonitor' => 'newMonitorActivity'];
|
|
|
|
|
2024-04-16 13:42:38 +00:00
|
|
|
public function newMonitorActivity($activityId, $eventToDispatch = 'activityFinished', $eventData = null)
|
2024-02-05 13:40:54 +00:00
|
|
|
{
|
|
|
|
$this->activityId = $activityId;
|
|
|
|
$this->eventToDispatch = $eventToDispatch;
|
2024-04-16 13:42:38 +00:00
|
|
|
$this->eventData = $eventData;
|
2024-02-05 13:40:54 +00:00
|
|
|
|
|
|
|
$this->hydrateActivity();
|
|
|
|
|
|
|
|
$this->isPollingActive = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hydrateActivity()
|
|
|
|
{
|
|
|
|
$this->activity = Activity::find($this->activityId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function polling()
|
|
|
|
{
|
|
|
|
$this->hydrateActivity();
|
|
|
|
// $this->setStatus(ProcessStatus::IN_PROGRESS);
|
|
|
|
$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;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-05 13:40:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
if (! is_null($this->eventData)) {
|
2024-04-16 13:42:38 +00:00
|
|
|
$this->dispatch($this->eventToDispatch, $this->eventData);
|
|
|
|
} else {
|
|
|
|
$this->dispatch($this->eventToDispatch);
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
ray('Dispatched event: '.$this->eventToDispatch.' with data: '.$this->eventData);
|
2024-02-05 13:40:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|