diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fdeb97..e5966a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## vX.X.X (YYYY-MM-DD) +### Fixed +- `models.GetExpiredDeployments` queried a column that does not exist (`expired_at` instead of `expires_at`), so it always failed. It now shares `models.GetDeploymentsExpiringBy` with the expiry scheduler, and like it leaves out unique deployments, which never expire + ## v0.7.1 (2026-06-02) ### Added diff --git a/galvanize-instancer/pkg/models/deployment.go b/galvanize-instancer/pkg/models/deployment.go index 00e76e4..1e91883 100755 --- a/galvanize-instancer/pkg/models/deployment.go +++ b/galvanize-instancer/pkg/models/deployment.go @@ -47,12 +47,23 @@ func (deployment *Deployment) BeforeDelete(tx *gorm.DB) (err error) { return tx.Error } -func GetExpiredDeployments(db *gorm.DB) ([]Deployment, error) { +// GetDeploymentsExpiringBy returns the running team deployments that expire +// at t or earlier, the soonest first. Unique (shared) deployments have no +// team and are never expired, so they are left out. +func GetDeploymentsExpiringBy(db *gorm.DB, t time.Time) ([]Deployment, error) { var deployments []Deployment - result := db.Where("status = ? AND expired_at <= ?", DeploymentStatusRunning, time.Now()).Find(&deployments) + result := db.Where("status = ? AND expires_at IS NOT NULL AND expires_at <= ? AND team_id IS NOT NULL", + DeploymentStatusRunning, t). + Order("expires_at ASC"). + Find(&deployments) return deployments, result.Error } +// GetExpiredDeployments returns the running team deployments already expired. +func GetExpiredDeployments(db *gorm.DB) ([]Deployment, error) { + return GetDeploymentsExpiringBy(db, time.Now()) +} + func GetDeployment(db *gorm.DB, category, challengeName, teamID string, lock bool) (*Deployment, error) { var deployment Deployment var result *gorm.DB diff --git a/galvanize-instancer/pkg/models/deployment_test.go b/galvanize-instancer/pkg/models/deployment_test.go new file mode 100644 index 0000000..dcf9f85 --- /dev/null +++ b/galvanize-instancer/pkg/models/deployment_test.go @@ -0,0 +1,66 @@ +package models + +import ( + "testing" + "time" + + "github.com/28Pollux28/galvanize/pkg/utils" + "github.com/glebarez/sqlite" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "gorm.io/gorm" +) + +func newTestDB(t *testing.T) *gorm.DB { + t.Helper() + db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{}) + require.NoError(t, err) + require.NoError(t, db.AutoMigrate(Deployment{})) + return db +} + +// addDeployment stores a team deployment (a unique one when teamID is empty) +// with the given status, expiring at expiresAt (none when nil). +func addDeployment(t *testing.T, db *gorm.DB, name, teamID, status string, expiresAt *time.Time) { + t.Helper() + d := &Deployment{ChallengeName: name, Category: "web", Status: status, ExpiresAt: expiresAt} + if teamID != "" { + d.TeamID = &teamID + } + require.NoError(t, db.Create(d).Error) +} + +func names(deployments []Deployment) []string { + out := make([]string, 0, len(deployments)) + for _, d := range deployments { + out = append(out, d.ChallengeName) + } + return out +} + +func TestGetExpiredDeployments(t *testing.T) { + db := newTestDB(t) + now := time.Now() + addDeployment(t, db, "expired-later", "team1", DeploymentStatusRunning, utils.Ptr(now.Add(-time.Minute))) + addDeployment(t, db, "expired-first", "team2", DeploymentStatusRunning, utils.Ptr(now.Add(-time.Hour))) + addDeployment(t, db, "not-yet", "team3", DeploymentStatusRunning, utils.Ptr(now.Add(time.Hour))) + addDeployment(t, db, "starting", "team4", DeploymentStatusStarting, utils.Ptr(now.Add(-time.Hour))) + addDeployment(t, db, "no-expiry", "team5", DeploymentStatusRunning, nil) + addDeployment(t, db, "unique", "", DeploymentStatusRunning, utils.Ptr(now.Add(-time.Hour))) + + expired, err := GetExpiredDeployments(db) + require.NoError(t, err) + assert.Equal(t, []string{"expired-first", "expired-later"}, names(expired), "running team deployments past expiry, soonest first") +} + +func TestGetDeploymentsExpiringBy(t *testing.T) { + db := newTestDB(t) + now := time.Now() + addDeployment(t, db, "in-1m", "team1", DeploymentStatusRunning, utils.Ptr(now.Add(time.Minute))) + addDeployment(t, db, "in-1h", "team2", DeploymentStatusRunning, utils.Ptr(now.Add(time.Hour))) + addDeployment(t, db, "past", "team3", DeploymentStatusRunning, utils.Ptr(now.Add(-time.Minute))) + + upcoming, err := GetDeploymentsExpiringBy(db, now.Add(10*time.Minute)) + require.NoError(t, err) + assert.Equal(t, []string{"past", "in-1m"}, names(upcoming)) +} diff --git a/galvanize-instancer/pkg/scheduler/scheduler.go b/galvanize-instancer/pkg/scheduler/scheduler.go index 0b26ce2..f207a95 100644 --- a/galvanize-instancer/pkg/scheduler/scheduler.go +++ b/galvanize-instancer/pkg/scheduler/scheduler.go @@ -76,12 +76,7 @@ func (s *ExpiryScheduler) fetchNextExpiries() { window := time.Now().Add(s.lookahead) - var deployments []models.Deployment - err := s.db.Where("status = ? AND expires_at IS NOT NULL AND expires_at <= ? AND team_id IS NOT NULL", // Not null excludes unique deployments - "running", window). - Order("expires_at ASC"). - Find(&deployments).Error - + deployments, err := models.GetDeploymentsExpiringBy(s.db, window) if err != nil { s.l.Errorf("failed to fetch upcoming expirations: %v", err) return