lasthourcloud/app/Models/ApplicationDeploymentQueue.php

98 lines
3.0 KiB
PHP
Raw Permalink Normal View History

2023-05-24 12:26:50 +00:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
2023-11-23 20:02:30 +00:00
use Illuminate\Support\Carbon;
2024-07-09 08:45:10 +00:00
use OpenApi\Attributes as OA;
2023-05-24 12:26:50 +00:00
2024-07-09 08:45:10 +00:00
#[OA\Schema(
description: 'Project model',
type: 'object',
properties: [
'id' => ['type' => 'integer'],
'application_id' => ['type' => 'string'],
'deployment_uuid' => ['type' => 'string'],
'pull_request_id' => ['type' => 'integer'],
'force_rebuild' => ['type' => 'boolean'],
'commit' => ['type' => 'string'],
'status' => ['type' => 'string'],
'is_webhook' => ['type' => 'boolean'],
'is_api' => ['type' => 'boolean'],
'created_at' => ['type' => 'string'],
'updated_at' => ['type' => 'string'],
'logs' => ['type' => 'string'],
'current_process_id' => ['type' => 'string'],
'restart_only' => ['type' => 'boolean'],
'git_type' => ['type' => 'string'],
'server_id' => ['type' => 'integer'],
'application_name' => ['type' => 'string'],
'server_name' => ['type' => 'string'],
'deployment_url' => ['type' => 'string'],
'destination_id' => ['type' => 'string'],
'only_this_server' => ['type' => 'boolean'],
'rollback' => ['type' => 'boolean'],
'commit_message' => ['type' => 'string'],
],
)]
2023-05-24 12:26:50 +00:00
class ApplicationDeploymentQueue extends Model
{
2023-06-30 13:57:40 +00:00
protected $guarded = [];
2023-11-23 20:02:30 +00:00
public function setStatus(string $status)
{
2023-12-04 14:08:24 +00:00
$this->update([
'status' => $status,
]);
}
2024-06-10 20:43:34 +00:00
public function getOutput($name)
{
2024-06-10 20:43:34 +00:00
if (! $this->logs) {
2023-11-23 20:02:30 +00:00
return null;
}
2024-06-10 20:43:34 +00:00
2023-11-23 20:02:30 +00:00
return collect(json_decode($this->logs))->where('name', $name)->first()?->output ?? null;
}
2024-06-10 20:43:34 +00:00
public function commitMessage()
{
if (empty($this->commit_message) || is_null($this->commit_message)) {
return null;
}
2024-06-10 20:43:34 +00:00
return str($this->commit_message)->trim()->limit(50)->value();
}
2024-06-10 20:43:34 +00:00
2023-11-23 20:02:30 +00:00
public function addLogEntry(string $message, string $type = 'stdout', bool $hidden = false)
{
if ($type === 'error') {
$type = 'stderr';
}
$message = str($message)->trim();
if ($message->startsWith('╔')) {
2024-06-10 20:43:34 +00:00
$message = "\n".$message;
}
2023-11-23 20:02:30 +00:00
$newLogEntry = [
'command' => null,
'output' => remove_iip($message),
2023-11-23 20:02:30 +00:00
'type' => $type,
'timestamp' => Carbon::now('UTC'),
'hidden' => $hidden,
'batch' => 1,
];
if ($this->logs) {
$previousLogs = json_decode($this->logs, associative: true, flags: JSON_THROW_ON_ERROR);
$newLogEntry['order'] = count($previousLogs) + 1;
$previousLogs[] = $newLogEntry;
$this->update([
'logs' => json_encode($previousLogs, flags: JSON_THROW_ON_ERROR),
]);
} else {
$this->update([
'logs' => json_encode([$newLogEntry], flags: JSON_THROW_ON_ERROR),
]);
}
}
2023-05-24 12:26:50 +00:00
}