@@ -117,12 +117,12 @@ export const createDeployment = async (
117117 > ,
118118) => {
119119 const application = await findApplicationById ( deployment . applicationId ) ;
120+ await removeLastTenDeployments (
121+ deployment . applicationId ,
122+ "application" ,
123+ application . serverId ,
124+ ) ;
120125 try {
121- await removeLastTenDeployments (
122- deployment . applicationId ,
123- "application" ,
124- application . serverId ,
125- ) ;
126126 const serverId = application . buildServerId || application . serverId ;
127127
128128 const { LOGS_PATH } = paths ( ! ! serverId ) ;
@@ -200,13 +200,12 @@ export const createDeploymentPreview = async (
200200 const previewDeployment = await findPreviewDeploymentById (
201201 deployment . previewDeploymentId ,
202202 ) ;
203+ await removeLastTenDeployments (
204+ deployment . previewDeploymentId ,
205+ "previewDeployment" ,
206+ previewDeployment ?. application ?. serverId ,
207+ ) ;
203208 try {
204- await removeLastTenDeployments (
205- deployment . previewDeploymentId ,
206- "previewDeployment" ,
207- previewDeployment ?. application ?. serverId ,
208- ) ;
209-
210209 const appName = `${ previewDeployment . appName } ` ;
211210 const { LOGS_PATH } = paths ( ! ! previewDeployment ?. application ?. serverId ) ;
212211 const formattedDateTime = format ( new Date ( ) , "yyyy-MM-dd:HH:mm:ss" ) ;
@@ -281,12 +280,12 @@ export const createDeploymentCompose = async (
281280 > ,
282281) => {
283282 const compose = await findComposeById ( deployment . composeId ) ;
283+ await removeLastTenDeployments (
284+ deployment . composeId ,
285+ "compose" ,
286+ compose . serverId ,
287+ ) ;
284288 try {
285- await removeLastTenDeployments (
286- deployment . composeId ,
287- "compose" ,
288- compose . serverId ,
289- ) ;
290289 const { LOGS_PATH } = paths ( ! ! compose . serverId ) ;
291290 const formattedDateTime = format ( new Date ( ) , "yyyy-MM-dd:HH:mm:ss" ) ;
292291 const fileName = `${ compose . appName } -${ formattedDateTime } .log` ;
@@ -369,8 +368,8 @@ export const createDeploymentBackup = async (
369368 } else if ( backup . backupType === "compose" ) {
370369 serverId = backup . compose ?. serverId ;
371370 }
371+ await removeLastTenDeployments ( deployment . backupId , "backup" , serverId ) ;
372372 try {
373- await removeLastTenDeployments ( deployment . backupId , "backup" , serverId ) ;
374373 const { LOGS_PATH } = paths ( ! ! serverId ) ;
375374 const formattedDateTime = format ( new Date ( ) , "yyyy-MM-dd:HH:mm:ss" ) ;
376375 const fileName = `${ backup . appName } -${ formattedDateTime } .log` ;
@@ -439,12 +438,12 @@ export const createDeploymentSchedule = async (
439438) => {
440439 const schedule = await findScheduleById ( deployment . scheduleId ) ;
441440
441+ const serverId =
442+ schedule . application ?. serverId ||
443+ schedule . compose ?. serverId ||
444+ schedule . server ?. serverId ;
445+ await removeLastTenDeployments ( deployment . scheduleId , "schedule" , serverId ) ;
442446 try {
443- const serverId =
444- schedule . application ?. serverId ||
445- schedule . compose ?. serverId ||
446- schedule . server ?. serverId ;
447- await removeLastTenDeployments ( deployment . scheduleId , "schedule" , serverId ) ;
448447 const { SCHEDULES_PATH } = paths ( ! ! serverId ) ;
449448 const formattedDateTime = format ( new Date ( ) , "yyyy-MM-dd:HH:mm:ss" ) ;
450449 const fileName = `${ schedule . appName } -${ formattedDateTime } .log` ;
@@ -515,14 +514,14 @@ export const createDeploymentVolumeBackup = async (
515514) => {
516515 const volumeBackup = await findVolumeBackupById ( deployment . volumeBackupId ) ;
517516
517+ const serverId =
518+ volumeBackup . application ?. serverId || volumeBackup . compose ?. serverId ;
519+ await removeLastTenDeployments (
520+ deployment . volumeBackupId ,
521+ "volumeBackup" ,
522+ serverId ,
523+ ) ;
518524 try {
519- const serverId =
520- volumeBackup . application ?. serverId || volumeBackup . compose ?. serverId ;
521- await removeLastTenDeployments (
522- deployment . volumeBackupId ,
523- "volumeBackup" ,
524- serverId ,
525- ) ;
526525 const { VOLUME_BACKUPS_PATH } = paths ( ! ! serverId ) ;
527526 const formattedDateTime = format ( new Date ( ) , "yyyy-MM-dd:HH:mm:ss" ) ;
528527 const fileName = `${ volumeBackup . appName } -${ formattedDateTime } .log` ;
@@ -601,24 +600,23 @@ export const removeDeployment = async (deploymentId: string) => {
601600 . then ( ( result ) => result [ 0 ] ) ;
602601
603602 if ( ! deployment ) {
604- throw new TRPCError ( {
605- code : "BAD_REQUEST" ,
606- message : "Deployment not found" ,
607- } ) ;
603+ return null ;
608604 }
609- const command = `
610- rm -f ${ deployment . logPath } ;
611- ` ;
612- if ( deployment . serverId ) {
613- await execAsyncRemote ( deployment . serverId , command ) ;
614- } else {
615- await execAsync ( command ) ;
605+
606+ const logPath = path . join ( deployment . logPath ) ;
607+ if ( logPath && logPath !== "." ) {
608+ const command = `rm -f ${ logPath } ;` ;
609+ if ( deployment . serverId ) {
610+ await execAsyncRemote ( deployment . serverId , command ) ;
611+ } else {
612+ await execAsync ( command ) ;
613+ }
616614 }
617615
618616 return deployment ;
619617 } catch ( error ) {
620618 const message =
621- error instanceof Error ? error . message : "Error creating the deployment" ;
619+ error instanceof Error ? error . message : "Error removing the deployment" ;
622620 throw new TRPCError ( {
623621 code : "BAD_REQUEST" ,
624622 message,
@@ -686,34 +684,49 @@ const removeLastTenDeployments = async (
686684 if ( serverId ) {
687685 let command = "" ;
688686 for ( const oldDeployment of deploymentsToDelete ) {
689- const logPath = path . join ( oldDeployment . logPath ) ;
690- if ( oldDeployment . rollbackId ) {
691- await removeRollbackById ( oldDeployment . rollbackId ) ;
692- }
693-
694- if ( logPath !== "." ) {
695- command += `
696- rm -rf ${ logPath } ;
697- ` ;
687+ try {
688+ const logPath = path . join ( oldDeployment . logPath ) ;
689+ if ( oldDeployment . rollbackId ) {
690+ await removeRollbackById ( oldDeployment . rollbackId ) ;
691+ }
692+
693+ if ( logPath && logPath !== "." ) {
694+ command += `rm -rf ${ logPath } ;` ;
695+ }
696+ await removeDeployment ( oldDeployment . deploymentId ) ;
697+ } catch ( err ) {
698+ console . error (
699+ `Failed to remove deployment ${ oldDeployment . deploymentId } during cleanup:` ,
700+ err ,
701+ ) ;
698702 }
699- await removeDeployment ( oldDeployment . deploymentId ) ;
700703 }
701704
702- await execAsyncRemote ( serverId , command ) ;
705+ if ( command ) {
706+ await execAsyncRemote ( serverId , command ) ;
707+ }
703708 } else {
704709 for ( const oldDeployment of deploymentsToDelete ) {
705- if ( oldDeployment . rollbackId ) {
706- await removeRollbackById ( oldDeployment . rollbackId ) ;
707- }
708- const logPath = path . join ( oldDeployment . logPath ) ;
709- if (
710- existsSync ( logPath ) &&
711- ! oldDeployment . errorMessage &&
712- logPath !== "."
713- ) {
714- await fsPromises . unlink ( logPath ) ;
710+ try {
711+ if ( oldDeployment . rollbackId ) {
712+ await removeRollbackById ( oldDeployment . rollbackId ) ;
713+ }
714+ const logPath = path . join ( oldDeployment . logPath ) ;
715+ if (
716+ logPath &&
717+ logPath !== "." &&
718+ existsSync ( logPath ) &&
719+ ! oldDeployment . errorMessage
720+ ) {
721+ await fsPromises . unlink ( logPath ) ;
722+ }
723+ await removeDeployment ( oldDeployment . deploymentId ) ;
724+ } catch ( err ) {
725+ console . error (
726+ `Failed to remove deployment ${ oldDeployment . deploymentId } during cleanup:` ,
727+ err ,
728+ ) ;
715729 }
716- await removeDeployment ( oldDeployment . deploymentId ) ;
717730 }
718731 }
719732 }
0 commit comments