Compare commits

..

2 Commits

Author SHA1 Message Date
clawbot
25cd02e1d7 fix: use strings.HasPrefix and delegate export to real cleanup method
- Replace manual string prefix check with strings.HasPrefix (idiomatic Go)
- Refactor CleanupCancelledDeploy export to call the real cleanupCancelledDeploy
  method instead of re-implementing the logic, so tests catch regressions
- Add nil-db guard in Deployment.Save and App.Save to prevent panics when
  models are used without a database connection (e.g. in test exports)
- Fix wsl_v5 lint issue (missing blank line before assignment)
2026-02-19 20:17:06 -08:00
clawbot
729425132b fix: clean up orphan resources on deploy cancellation (closes #89) 2026-02-19 20:12:08 -08:00
4 changed files with 16 additions and 22 deletions

View File

@ -62,6 +62,10 @@ func NewApp(db *database.Database) *App {
// Save inserts or updates the app in the database.
func (a *App) Save(ctx context.Context) error {
if a.db == nil {
return fmt.Errorf("no database connection")
}
if a.exists(ctx) {
return a.update(ctx)
}

View File

@ -57,6 +57,10 @@ func NewDeployment(db *database.Database) *Deployment {
// Save inserts or updates the deployment in the database.
func (d *Deployment) Save(ctx context.Context) error {
if d.db == nil {
return fmt.Errorf("no database connection")
}
if d.ID == 0 {
return d.insert(ctx)
}

View File

@ -726,6 +726,7 @@ func (svc *Service) cleanupCancelledDeploy(
} else {
svc.log.Info("cleaned up build dir from cancelled deploy",
"app", app.Name, "path", dirPath)
_ = deployment.AppendLog(ctx, "Cleaned up build directory")
}
}

View File

@ -2,14 +2,11 @@ package deploy
import (
"context"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"git.eeqj.de/sneak/upaas/internal/config"
"git.eeqj.de/sneak/upaas/internal/docker"
"git.eeqj.de/sneak/upaas/internal/models"
)
// NewTestService creates a Service with minimal dependencies for testing.
@ -48,32 +45,20 @@ func NewTestServiceWithConfig(log *slog.Logger, cfg *config.Config, dockerClient
}
}
// CleanupCancelledDeploy exposes the build directory cleanup portion of
// cleanupCancelledDeploy for testing. It removes build directories matching
// the deployment ID prefix.
// CleanupCancelledDeploy exposes cleanupCancelledDeploy for testing.
func (svc *Service) CleanupCancelledDeploy(
ctx context.Context,
appName string,
deploymentID int64,
imageID string,
) {
// We can't create real models.App/Deployment in tests easily,
// so we test the build dir cleanup portion directly.
buildDir := svc.GetBuildDir(appName)
app := models.NewApp(nil)
app.Name = appName
entries, err := os.ReadDir(buildDir)
if err != nil {
return
}
deployment := models.NewDeployment(nil)
deployment.ID = deploymentID
prefix := fmt.Sprintf("%d-", deploymentID)
for _, entry := range entries {
if entry.IsDir() && strings.HasPrefix(entry.Name(), prefix) {
dirPath := filepath.Join(buildDir, entry.Name())
_ = os.RemoveAll(dirPath)
}
}
svc.cleanupCancelledDeploy(ctx, app, deployment, imageID)
}
// GetBuildDirExported exposes GetBuildDir for testing.