Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dca1bc7

Browse files
fix: set a workspace's ttl to template's ttl if allow user autostop gets disabled
1 parent 021a806 commit dca1bc7

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

enterprise/coderd/schedule/template.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,11 @@ func (s *EnterpriseTemplateScheduleStore) Set(ctx context.Context, db database.S
195195
return xerrors.Errorf("get updated template schedule: %w", err)
196196
}
197197

198-
// If this template disallows users from customizing their own autostop, then
199-
// we want to keep their workspaces in track with any template TTL changes.
200-
if !template.AllowUserAutostop && int64(opts.DefaultTTL) != tpl.DefaultTTL {
198+
// Update all workspace's TTL using this template if either of the following:
199+
// - The template's AllowUserAutostop has just been disabled
200+
// - The template's TTL has been modified and AllowUserAutostop is disabled
201+
if (!opts.UserAutostopEnabled && opts.UserAutostopEnabled != tpl.AllowUserAutostop) ||
202+
(!template.AllowUserAutostop && int64(opts.DefaultTTL) != tpl.DefaultTTL) {
201203
var ttl sql.NullInt64
202204
if opts.DefaultTTL != 0 {
203205
ttl = sql.NullInt64{Valid: true, Int64: int64(opts.DefaultTTL)}

enterprise/coderd/schedule/template_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,74 @@ func TestTemplateTTL(t *testing.T) {
871871
require.Equal(t, sql.NullInt64{Valid: true, Int64: int64(otherTTL)}, ws.Ttl)
872872
})
873873
}
874+
875+
t.Run("WorkspaceTTLUpdatedWhenAllowUserAutostopGetsDisabled", func(t *testing.T) {
876+
t.Parallel()
877+
878+
var (
879+
logger = slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
880+
db, _ = dbtestutil.NewDB(t)
881+
ctx = testutil.Context(t, testutil.WaitLong)
882+
user = dbgen.User(t, db, database.User{})
883+
file = dbgen.File(t, db, database.File{CreatedBy: user.ID})
884+
// Create first template
885+
templateJob = dbgen.ProvisionerJob(t, db, nil, database.ProvisionerJob{
886+
FileID: file.ID,
887+
InitiatorID: user.ID,
888+
Tags: database.StringMap{"foo": "bar"},
889+
})
890+
templateVersion = dbgen.TemplateVersion(t, db, database.TemplateVersion{
891+
CreatedBy: user.ID,
892+
JobID: templateJob.ID,
893+
OrganizationID: templateJob.OrganizationID,
894+
})
895+
template = dbgen.Template(t, db, database.Template{
896+
ActiveVersionID: templateVersion.ID,
897+
CreatedBy: user.ID,
898+
OrganizationID: templateJob.OrganizationID,
899+
})
900+
)
901+
902+
// Setup the template schedule store
903+
notifyEnq := notifications.NewNoopEnqueuer()
904+
const userQuietHoursSchedule = "CRON_TZ=UTC 0 0 * * *" // midnight UTC
905+
userQuietHoursStore, err := schedule.NewEnterpriseUserQuietHoursScheduleStore(userQuietHoursSchedule, true)
906+
require.NoError(t, err)
907+
userQuietHoursStorePtr := &atomic.Pointer[agplschedule.UserQuietHoursScheduleStore]{}
908+
userQuietHoursStorePtr.Store(&userQuietHoursStore)
909+
templateScheduleStore := schedule.NewEnterpriseTemplateScheduleStore(userQuietHoursStorePtr, notifyEnq, logger, nil)
910+
911+
// Enable AllowUserAutostop
912+
template, err = templateScheduleStore.Set(ctx, db, template, agplschedule.TemplateScheduleOptions{
913+
DefaultTTL: 24 * time.Hour,
914+
UserAutostopEnabled: true,
915+
})
916+
require.NoError(t, err)
917+
918+
// Create a workspace with a TTL different to the template schedule
919+
workspace := dbgen.Workspace(t, db, database.WorkspaceTable{
920+
OwnerID: user.ID,
921+
TemplateID: template.ID,
922+
OrganizationID: templateJob.OrganizationID,
923+
LastUsedAt: dbtime.Now(),
924+
Ttl: sql.NullInt64{Valid: true, Int64: int64(48 * time.Hour)},
925+
})
926+
927+
// Ensure the workspace's start with the correct TTLs
928+
require.Equal(t, sql.NullInt64{Valid: true, Int64: int64(48 * time.Hour)}, workspace.Ttl)
929+
930+
// Disable AllowUserAutostop
931+
template, err = templateScheduleStore.Set(ctx, db, template, agplschedule.TemplateScheduleOptions{
932+
DefaultTTL: 24 * time.Hour,
933+
UserAutostopEnabled: false,
934+
})
935+
require.NoError(t, err)
936+
937+
// Ensure the workspace's ends with the correct TTLs
938+
ws, err := db.GetWorkspaceByID(ctx, workspace.ID)
939+
require.NoError(t, err)
940+
require.Equal(t, sql.NullInt64{Valid: true, Int64: int64(24 * time.Hour)}, ws.Ttl)
941+
})
874942
}
875943

876944
func must[V any](v V, err error) V {

0 commit comments

Comments
 (0)