lasthourcloud/app/Jobs/ApplicationContainerStatusJob.php

50 lines
1.6 KiB
PHP
Raw Normal View History

2023-03-30 13:52:19 +00:00
<?php
namespace App\Jobs;
use App\Models\Application;
2023-05-30 13:52:17 +00:00
use App\Models\ApplicationPreview;
2023-03-30 13:52:19 +00:00
use Illuminate\Bus\Queueable;
2023-05-23 07:53:24 +00:00
use Illuminate\Contracts\Queue\ShouldBeUnique;
2023-03-30 13:52:19 +00:00
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
2023-06-02 13:15:12 +00:00
class ApplicationContainerStatusJob implements ShouldQueue, ShouldBeUnique
2023-03-30 13:52:19 +00:00
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2023-05-30 13:52:17 +00:00
public string $container_name;
public string|null $pull_request_id;
public Application $application;
public function __construct($application, string $container_name, string|null $pull_request_id = null)
{
$this->application = $application;
$this->container_name = $container_name;
$this->pull_request_id = $pull_request_id;
2023-03-30 13:52:19 +00:00
}
2023-05-23 07:53:24 +00:00
public function uniqueId(): string
{
2023-05-30 13:52:17 +00:00
return $this->container_name;
2023-05-23 07:53:24 +00:00
}
2023-04-14 11:18:55 +00:00
public function handle(): void
{
2023-04-28 08:38:22 +00:00
try {
2023-05-30 13:52:17 +00:00
$status = get_container_status(server: $this->application->destination->server, container_id: $this->container_name, throwError: false);
if ($this->pull_request_id) {
$preview = ApplicationPreview::findPreviewByApplicationAndPullId($this->application->id, $this->pull_request_id);
$preview->status = $status;
$preview->save();
2023-04-28 08:38:22 +00:00
} else {
2023-05-30 13:52:17 +00:00
$this->application->status = $status;
$this->application->save();
2023-04-28 08:38:22 +00:00
}
} catch (\Exception $e) {
Log::error($e->getMessage());
2023-04-14 11:18:55 +00:00
}
}
2023-03-30 13:52:19 +00:00
}