coolify/bootstrap/helpers/applications.php

40 lines
1.9 KiB
PHP
Raw Normal View History

2023-05-24 14:26:50 +02:00
<?php
use App\Jobs\ApplicationDeploymentJob;
use App\Models\ApplicationDeploymentQueue;
2023-05-31 11:24:02 +02:00
function queue_application_deployment(int $application_id, string $deployment_uuid, int|null $pull_request_id = 0, string $commit = 'HEAD', bool $force_rebuild = false, bool $is_webhook = false)
2023-05-24 14:26:50 +02:00
{
2023-05-30 15:52:17 +02:00
ray('Queuing deployment: ' . $deployment_uuid . ' of applicationID: ' . $application_id . ' pull request: ' . $pull_request_id . ' with commit: ' . $commit . ' and is it forced: ' . $force_rebuild);
2023-05-24 14:26:50 +02:00
$deployment = ApplicationDeploymentQueue::create([
2023-05-30 15:52:17 +02:00
'application_id' => $application_id,
'deployment_uuid' => $deployment_uuid,
'pull_request_id' => $pull_request_id,
'force_rebuild' => $force_rebuild,
2023-05-31 11:24:02 +02:00
'is_webhook' => $is_webhook,
2023-05-30 15:52:17 +02:00
'commit' => $commit,
2023-05-24 14:26:50 +02:00
]);
2023-05-30 15:52:17 +02:00
$queued_deployments = ApplicationDeploymentQueue::where('application_id', $application_id)->where('status', 'queued')->get()->sortByDesc('created_at');
$running_deployments = ApplicationDeploymentQueue::where('application_id', $application_id)->where('status', 'in_progress')->get()->sortByDesc('created_at');
ray('Queued deployments: ' . $queued_deployments->count());
ray('Running deployments: ' . $running_deployments->count());
2023-05-24 14:26:50 +02:00
if ($queued_deployments->count() > 1) {
$queued_deployments = $queued_deployments->skip(1);
$queued_deployments->each(function ($queued_deployment, $key) {
$queued_deployment->status = 'cancelled by system';
$queued_deployment->save();
});
}
if ($running_deployments->count() > 0) {
return;
}
dispatch(new ApplicationDeploymentJob(
application_deployment_queue_id: $deployment->id,
2023-05-30 15:52:17 +02:00
application_id: $application_id,
deployment_uuid: $deployment_uuid,
force_rebuild: $force_rebuild,
rollback_commit: $commit,
pull_request_id: $pull_request_id,
2023-05-24 14:26:50 +02:00
));
}