Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions galvanize-instancer/pkg/models/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 66 additions & 0 deletions galvanize-instancer/pkg/models/deployment_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
7 changes: 1 addition & 6 deletions galvanize-instancer/pkg/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading