2023-05-03 05:23:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Livewire;
|
|
|
|
|
2023-05-08 07:16:50 +00:00
|
|
|
use App\Enums\ProcessStatus;
|
2023-05-03 05:23:45 +00:00
|
|
|
use Livewire\Component;
|
|
|
|
use Spatie\Activitylog\Models\Activity;
|
|
|
|
|
|
|
|
class ActivityMonitor extends Component
|
|
|
|
{
|
2023-06-02 13:15:12 +00:00
|
|
|
public bool $header = false;
|
2023-05-03 05:23:45 +00:00
|
|
|
public $activityId;
|
|
|
|
public $isPollingActive = false;
|
|
|
|
|
|
|
|
protected $activity;
|
|
|
|
protected $listeners = ['newMonitorActivity'];
|
|
|
|
|
|
|
|
public function newMonitorActivity($activityId)
|
|
|
|
{
|
|
|
|
$this->activityId = $activityId;
|
|
|
|
|
|
|
|
$this->hydrateActivity();
|
|
|
|
|
|
|
|
$this->isPollingActive = true;
|
|
|
|
}
|
|
|
|
|
2023-08-08 09:51:36 +00:00
|
|
|
public function hydrateActivity()
|
|
|
|
{
|
|
|
|
$this->activity = Activity::query()
|
|
|
|
->find($this->activityId);
|
|
|
|
}
|
|
|
|
|
2023-05-03 05:23:45 +00:00
|
|
|
public function polling()
|
|
|
|
{
|
|
|
|
$this->hydrateActivity();
|
2023-05-08 07:16:50 +00:00
|
|
|
$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);
|
|
|
|
}
|
2023-05-03 05:23:45 +00:00
|
|
|
$this->isPollingActive = false;
|
2023-08-07 20:14:21 +00:00
|
|
|
$this->emit('activityFinished');
|
2023-05-03 05:23:45 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-05-08 07:16:50 +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
|
|
|
}
|