Fix app status not updated when deployment fails or service restarts

- Update app status to error when health check fails with an error
- Update app status to error in cleanupStuckDeployments for apps stuck in building state
- This fixes the inconsistency where app shows "building" but deployment is "failed"
This commit is contained in:
Jeffrey Paul 2026-01-01 06:59:03 -08:00
parent a71bf07dbf
commit aaa55fd153

View File

@ -386,7 +386,8 @@ func (svc *Service) deployContainerWithTimeout(
// cleanupStuckDeployments marks any deployments stuck in building/deploying as failed. // cleanupStuckDeployments marks any deployments stuck in building/deploying as failed.
func (svc *Service) cleanupStuckDeployments(ctx context.Context) error { func (svc *Service) cleanupStuckDeployments(ctx context.Context) error {
query := ` // First, update the deployments
deployQuery := `
UPDATE deployments UPDATE deployments
SET status = ?, finished_at = ?, logs = COALESCE(logs, '') || ? SET status = ?, finished_at = ?, logs = COALESCE(logs, '') || ?
WHERE status IN (?, ?) WHERE status IN (?, ?)
@ -395,7 +396,7 @@ func (svc *Service) cleanupStuckDeployments(ctx context.Context) error {
_, err := svc.db.DB().ExecContext( _, err := svc.db.DB().ExecContext(
ctx, ctx,
query, deployQuery,
models.DeploymentStatusFailed, models.DeploymentStatusFailed,
time.Now(), time.Now(),
msg, msg,
@ -408,7 +409,27 @@ func (svc *Service) cleanupStuckDeployments(ctx context.Context) error {
return fmt.Errorf("failed to cleanup stuck deployments: %w", err) return fmt.Errorf("failed to cleanup stuck deployments: %w", err)
} }
svc.log.Info("cleaned up stuck deployments") // Also update app status for apps that were stuck in building
appQuery := `
UPDATE apps
SET status = ?, updated_at = ?
WHERE status = ?
`
_, err = svc.db.DB().ExecContext(
ctx,
appQuery,
models.AppStatusError,
time.Now(),
models.AppStatusBuilding,
)
if err != nil {
svc.log.Error("failed to cleanup stuck app statuses", "error", err)
return fmt.Errorf("failed to cleanup stuck app statuses: %w", err)
}
svc.log.Info("cleaned up stuck deployments and app statuses")
return nil return nil
} }
@ -893,6 +914,9 @@ func (svc *Service) checkHealthAfterDelay(
svc.log.Error("failed to check container health", "error", err) svc.log.Error("failed to check container health", "error", err)
svc.notify.NotifyDeployFailed(ctx, reloadedApp, deployment, err) svc.notify.NotifyDeployFailed(ctx, reloadedApp, deployment, err)
_ = deployment.MarkFinished(ctx, models.DeploymentStatusFailed) _ = deployment.MarkFinished(ctx, models.DeploymentStatusFailed)
svc.writeLogsToFile(reloadedApp, deployment)
reloadedApp.Status = models.AppStatusError
_ = reloadedApp.Save(ctx)
return return
} }