lasthourcloud/app/Actions/CoolifyTask/PrepareCoolifyTask.php

61 lines
1.8 KiB
PHP
Raw Normal View History

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;
use App\Enums\ActivityTypes;
2023-05-03 06:24:34 +00:00
use App\Jobs\CoolifyTask;
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-05-03 05:15:45 +00:00
public function __construct(CoolifyTaskArgs $remoteProcessArgs)
{
2023-06-07 13:39:08 +00:00
$this->remoteProcessArgs = $remoteProcessArgs;
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('[]');
} 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-20 12:04:22 +00:00
}
public function __invoke(): Activity
2023-03-20 12:04:22 +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);
}
$this->activity->refresh();
2024-06-10 20:43:34 +00:00
2023-03-20 12:04:22 +00:00
return $this->activity;
}
}