coolify/app/Jobs/ContainerStatusJob.php

55 lines
1.8 KiB
PHP
Raw Normal View History

2023-03-30 15:52:19 +02:00
<?php
namespace App\Jobs;
2023-05-30 15:52:17 +02:00
use App\Models\ApplicationPreview;
2023-07-28 10:55:26 +02:00
use App\Notifications\Application\StatusChanged;
2023-03-30 15:52:19 +02:00
use Illuminate\Bus\Queueable;
2023-05-23 09:53:24 +02:00
use Illuminate\Contracts\Queue\ShouldBeUnique;
2023-03-30 15:52:19 +02:00
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
2023-08-07 22:14:21 +02:00
class ContainerStatusJob implements ShouldQueue, ShouldBeUnique
2023-03-30 15:52:19 +02:00
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2023-05-30 15:52:17 +02:00
public string $container_name;
public string|null $pull_request_id;
2023-08-07 22:14:21 +02:00
public $resource;
2023-05-30 15:52:17 +02:00
2023-08-07 22:14:21 +02:00
public function __construct($resource, string $container_name, string|null $pull_request_id = null)
2023-05-30 15:52:17 +02:00
{
2023-08-07 22:14:21 +02:00
$this->resource = $resource;
2023-05-30 15:52:17 +02:00
$this->container_name = $container_name;
$this->pull_request_id = $pull_request_id;
2023-03-30 15:52:19 +02:00
}
2023-05-23 09:53:24 +02:00
public function uniqueId(): string
{
2023-05-30 15:52:17 +02:00
return $this->container_name;
2023-05-23 09:53:24 +02:00
}
2023-04-14 13:18:55 +02:00
public function handle(): void
{
2023-04-28 10:38:22 +02:00
try {
2023-08-07 22:14:21 +02:00
$status = get_container_status(server: $this->resource->destination->server, container_id: $this->container_name, throwError: false);
2023-08-16 17:18:50 +02:00
if ($this->resource->status === 'running' && $status !== 'running') {
2023-08-07 22:14:21 +02:00
$this->resource->environment->project->team->notify(new StatusChanged($this->resource));
}
2023-05-30 15:52:17 +02:00
if ($this->pull_request_id) {
2023-08-07 22:14:21 +02:00
$preview = ApplicationPreview::findPreviewByApplicationAndPullId($this->resource->id, $this->pull_request_id);
2023-05-30 15:52:17 +02:00
$preview->status = $status;
$preview->save();
2023-04-28 10:38:22 +02:00
} else {
2023-08-07 22:14:21 +02:00
$this->resource->status = $status;
$this->resource->save();
2023-04-28 10:38:22 +02:00
}
} catch (\Exception $e) {
2023-07-26 13:33:48 +02:00
ray($e->getMessage());
2023-04-14 13:18:55 +02:00
}
}
2023-08-07 22:14:21 +02:00
}