2023-05-24 12:26:50 +00:00
< ? php
use App\Jobs\ApplicationDeploymentJob ;
2023-05-31 10:38:36 +00:00
use App\Models\Application ;
2023-05-24 12:26:50 +00:00
use App\Models\ApplicationDeploymentQueue ;
2023-05-31 09:24:02 +00: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 12:26:50 +00:00
{
$deployment = ApplicationDeploymentQueue :: create ([
2023-05-30 13:52:17 +00:00
'application_id' => $application_id ,
'deployment_uuid' => $deployment_uuid ,
'pull_request_id' => $pull_request_id ,
'force_rebuild' => $force_rebuild ,
2023-05-31 09:24:02 +00:00
'is_webhook' => $is_webhook ,
2023-05-30 13:52:17 +00:00
'commit' => $commit ,
2023-05-24 12:26:50 +00:00
]);
2023-05-30 13:52:17 +00: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' );
2023-05-31 12:57:42 +00:00
ray ( 'Q:' . $queued_deployments -> count () . 'R:' . $running_deployments -> count () . '| 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 12:26:50 +00: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 ;
}
2023-06-30 20:24:39 +00:00
dispatch ( new ApplicationDeploymentJob (
2023-05-24 12:26:50 +00:00
application_deployment_queue_id : $deployment -> id ,
2023-06-28 16:20:01 +00:00
)) -> onConnection ( 'long-running' ) -> onQueue ( 'long-running' );
2023-05-24 12:26:50 +00:00
}
2023-05-31 10:38:36 +00:00
function queue_next_deployment ( Application $application )
{
$next_found = ApplicationDeploymentQueue :: where ( 'application_id' , $application -> id ) -> where ( 'status' , 'queued' ) -> first ();
if ( $next_found ) {
dispatch ( new ApplicationDeploymentJob (
application_deployment_queue_id : $next_found -> id ,
2023-06-28 16:20:01 +00:00
)) -> onConnection ( 'long-running' ) -> onQueue ( 'long-running' );
2023-05-31 10:38:36 +00:00
}
}