2023-03-20 12:04:22 +00:00
|
|
|
<?php
|
|
|
|
|
2023-05-03 05:15:45 +00:00
|
|
|
namespace App\Actions\CoolifyTask;
|
2023-03-20 12:04:22 +00:00
|
|
|
|
2023-05-03 05:15:45 +00:00
|
|
|
use App\Data\CoolifyTaskArgs;
|
2024-06-25 08:51:32 +00:00
|
|
|
use App\Enums\ActivityTypes;
|
2023-05-03 06:24:34 +00:00
|
|
|
use App\Jobs\CoolifyTask;
|
2023-03-21 10:32:38 +00:00
|
|
|
use Spatie\Activitylog\Models\Activity;
|
2023-03-20 12:04:22 +00:00
|
|
|
|
2023-05-03 06:24:34 +00:00
|
|
|
/**
|
|
|
|
* The initial step to run a `CoolifyTask`: a remote SSH process
|
|
|
|
* with monitoring/tracking/trace feature. Such thing is made
|
|
|
|
* possible using an Activity model and some attributes.
|
|
|
|
*/
|
2023-05-03 05:15:45 +00:00
|
|
|
class PrepareCoolifyTask
|
2023-03-20 12:04:22 +00:00
|
|
|
{
|
|
|
|
protected Activity $activity;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-06-07 13:39:08 +00:00
|
|
|
protected CoolifyTaskArgs $remoteProcessArgs;
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-05-03 05:15:45 +00:00
|
|
|
public function __construct(CoolifyTaskArgs $remoteProcessArgs)
|
2023-03-29 10:27:02 +00:00
|
|
|
{
|
2023-06-07 13:39:08 +00:00
|
|
|
$this->remoteProcessArgs = $remoteProcessArgs;
|
|
|
|
|
2023-03-29 10:27:02 +00:00
|
|
|
if ($remoteProcessArgs->model) {
|
|
|
|
$properties = $remoteProcessArgs->toArray();
|
|
|
|
unset($properties['model']);
|
|
|
|
|
|
|
|
$this->activity = activity()
|
|
|
|
->withProperties($properties)
|
|
|
|
->performedOn($remoteProcessArgs->model)
|
2023-03-30 07:47:04 +00:00
|
|
|
->event($remoteProcessArgs->type)
|
2024-06-10 20:43:34 +00:00
|
|
|
->log('[]');
|
2023-03-29 10:27:02 +00:00
|
|
|
} else {
|
|
|
|
$this->activity = activity()
|
|
|
|
->withProperties($remoteProcessArgs->toArray())
|
2023-03-30 07:47:04 +00:00
|
|
|
->event($remoteProcessArgs->type)
|
2024-06-10 20:43:34 +00:00
|
|
|
->log('[]');
|
2023-03-29 10:27:02 +00:00
|
|
|
}
|
2023-03-20 12:04:22 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 09:08:36 +00:00
|
|
|
public function __invoke(): Activity
|
2023-03-20 12:04:22 +00:00
|
|
|
{
|
2024-06-25 08:51:32 +00:00
|
|
|
$job = new CoolifyTask(
|
|
|
|
activity: $this->activity,
|
|
|
|
ignore_errors: $this->remoteProcessArgs->ignore_errors,
|
|
|
|
call_event_on_finish: $this->remoteProcessArgs->call_event_on_finish,
|
|
|
|
call_event_data: $this->remoteProcessArgs->call_event_data,
|
|
|
|
);
|
|
|
|
if ($this->remoteProcessArgs->type === ActivityTypes::COMMAND->value) {
|
|
|
|
ray('Dispatching a high priority job');
|
|
|
|
dispatch($job)->onQueue('high');
|
|
|
|
} else {
|
|
|
|
dispatch($job);
|
|
|
|
}
|
2023-03-21 09:31:16 +00:00
|
|
|
$this->activity->refresh();
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-03-20 12:04:22 +00:00
|
|
|
return $this->activity;
|
|
|
|
}
|
|
|
|
}
|