lasthourcloud/app/Jobs/ScheduledTaskJob.php

127 lines
4.6 KiB
PHP
Raw Permalink Normal View History

2024-01-02 02:23:58 +00:00
<?php
namespace App\Jobs;
2024-06-10 20:43:34 +00:00
use App\Models\Application;
2024-01-02 02:23:58 +00:00
use App\Models\ScheduledTask;
use App\Models\ScheduledTaskExecution;
2024-01-02 02:23:58 +00:00
use App\Models\Server;
use App\Models\Service;
use App\Models\Team;
use App\Notifications\ScheduledTask\TaskFailed;
2024-01-02 02:23:58 +00:00
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Queue\SerializesModels;
class ScheduledTaskJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public ?Team $team = null;
2024-06-10 20:43:34 +00:00
2024-01-02 02:23:58 +00:00
public Server $server;
2024-06-10 20:43:34 +00:00
2024-01-02 02:23:58 +00:00
public ScheduledTask $task;
2024-06-10 20:43:34 +00:00
2024-01-02 02:23:58 +00:00
public Application|Service $resource;
public ?ScheduledTaskExecution $task_log = null;
2024-06-10 20:43:34 +00:00
2024-01-02 02:23:58 +00:00
public string $task_status = 'failed';
2024-06-10 20:43:34 +00:00
public ?string $task_output = null;
2024-06-10 20:43:34 +00:00
public array $containers = [];
2024-01-02 02:23:58 +00:00
public function __construct($task)
{
$this->task = $task;
if ($service = $task->service()->first()) {
$this->resource = $service;
2024-06-10 20:43:34 +00:00
} elseif ($application = $task->application()->first()) {
2024-01-02 02:23:58 +00:00
$this->resource = $application;
2024-01-24 10:57:51 +00:00
} else {
throw new \RuntimeException('ScheduledTaskJob failed: No resource found.');
2024-01-02 02:23:58 +00:00
}
$this->team = Team::find($task->team_id);
2024-01-02 02:23:58 +00:00
}
public function middleware(): array
{
return [new WithoutOverlapping($this->task->id)];
}
public function uniqueId(): int
{
return $this->task->id;
}
public function handle(): void
{
try {
$this->task_log = ScheduledTaskExecution::create([
'scheduled_task_id' => $this->task->id,
]);
$this->server = $this->resource->destination->server;
if ($this->resource->type() == 'application') {
$containers = getCurrentApplicationContainerStatus($this->server, $this->resource->id, 0);
if ($containers->count() > 0) {
$containers->each(function ($container) {
$this->containers[] = str_replace('/', '', $container['Names']);
});
}
2024-06-10 20:43:34 +00:00
} elseif ($this->resource->type() == 'service') {
$this->resource->applications()->get()->each(function ($application) {
if (str(data_get($application, 'status'))->contains('running')) {
2024-06-10 20:43:34 +00:00
$this->containers[] = data_get($application, 'name').'-'.data_get($this->resource, 'uuid');
}
});
$this->resource->databases()->get()->each(function ($database) {
if (str(data_get($database, 'status'))->contains('running')) {
2024-06-10 20:43:34 +00:00
$this->containers[] = data_get($database, 'name').'-'.data_get($this->resource, 'uuid');
}
});
}
if (count($this->containers) == 0) {
throw new \Exception('ScheduledTaskJob failed: No containers running.');
}
if (count($this->containers) > 1 && empty($this->task->container)) {
throw new \Exception('ScheduledTaskJob failed: More than one container exists but no container name was provided.');
}
foreach ($this->containers as $containerName) {
2024-06-10 20:43:34 +00:00
if (count($this->containers) == 1 || str_starts_with($containerName, $this->task->container.'-'.$this->resource->uuid)) {
$cmd = "sh -c '".str_replace("'", "'\''", $this->task->command)."'";
$exec = "docker exec {$containerName} {$cmd}";
$this->task_output = instant_remote_process([$exec], $this->server, true);
$this->task_log->update([
'status' => 'success',
'message' => $this->task_output,
]);
2024-06-10 20:43:34 +00:00
return;
}
}
// No valid container was found.
throw new \Exception('ScheduledTaskJob failed: No valid container was found. Is the container name correct?');
2024-01-02 02:23:58 +00:00
} catch (\Throwable $e) {
if ($this->task_log) {
$this->task_log->update([
'status' => 'failed',
'message' => $this->task_output ?? $e->getMessage(),
]);
}
$this->team?->notify(new TaskFailed($this->task, $e->getMessage()));
// send_internal_notification('ScheduledTaskJob failed with: ' . $e->getMessage());
2024-01-02 02:23:58 +00:00
throw $e;
}
}
}