Skip to content

Commit 47b8bf6

Browse files
authored
feat: update workspace deadline when template policy changes (#8964)
1 parent 37f9d4b commit 47b8bf6

File tree

14 files changed

+871
-22
lines changed

14 files changed

+871
-22
lines changed

coderd/database/dbauthz/dbauthz.go

+8
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,14 @@ func (q *querier) GetActiveUserCount(ctx context.Context) (int64, error) {
784784
return q.db.GetActiveUserCount(ctx)
785785
}
786786

787+
func (q *querier) GetActiveWorkspaceBuildsByTemplateID(ctx context.Context, templateID uuid.UUID) ([]database.WorkspaceBuild, error) {
788+
// This is a system-only function.
789+
if err := q.authorizeContext(ctx, rbac.ActionRead, rbac.ResourceSystem); err != nil {
790+
return []database.WorkspaceBuild{}, err
791+
}
792+
return q.db.GetActiveWorkspaceBuildsByTemplateID(ctx, templateID)
793+
}
794+
787795
func (q *querier) GetAllTailnetAgents(ctx context.Context) ([]database.TailnetAgent, error) {
788796
if err := q.authorizeContext(ctx, rbac.ActionRead, rbac.ResourceTailnetCoordinator); err != nil {
789797
return []database.TailnetAgent{}, err

coderd/database/dbfake/dbfake.go

+28
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,34 @@ func (q *FakeQuerier) GetActiveUserCount(_ context.Context) (int64, error) {
918918
return active, nil
919919
}
920920

921+
func (q *FakeQuerier) GetActiveWorkspaceBuildsByTemplateID(ctx context.Context, templateID uuid.UUID) ([]database.WorkspaceBuild, error) {
922+
workspaceIDs := func() []uuid.UUID {
923+
q.mutex.RLock()
924+
defer q.mutex.RUnlock()
925+
926+
ids := []uuid.UUID{}
927+
for _, workspace := range q.workspaces {
928+
if workspace.TemplateID == templateID {
929+
ids = append(ids, workspace.ID)
930+
}
931+
}
932+
return ids
933+
}()
934+
935+
builds, err := q.GetLatestWorkspaceBuildsByWorkspaceIDs(ctx, workspaceIDs)
936+
if err != nil {
937+
return nil, err
938+
}
939+
940+
filteredBuilds := []database.WorkspaceBuild{}
941+
for _, build := range builds {
942+
if build.Transition == database.WorkspaceTransitionStart {
943+
filteredBuilds = append(filteredBuilds, build)
944+
}
945+
}
946+
return filteredBuilds, nil
947+
}
948+
921949
func (*FakeQuerier) GetAllTailnetAgents(_ context.Context) ([]database.TailnetAgent, error) {
922950
return nil, ErrUnimplemented
923951
}

coderd/database/dbmetrics/dbmetrics.go

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbmock/dbmock.go

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

+66
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspacebuilds.sql

+24
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,27 @@ SET
144144
WHERE
145145
id = $1;
146146

147+
-- name: GetActiveWorkspaceBuildsByTemplateID :many
148+
SELECT wb.*
149+
FROM (
150+
SELECT
151+
workspace_id, MAX(build_number) as max_build_number
152+
FROM
153+
workspace_build_with_user AS workspace_builds
154+
WHERE
155+
workspace_id IN (
156+
SELECT
157+
id
158+
FROM
159+
workspaces
160+
WHERE
161+
template_id = $1
162+
)
163+
GROUP BY
164+
workspace_id
165+
) m
166+
JOIN
167+
workspace_build_with_user AS wb
168+
ON m.workspace_id = wb.workspace_id AND m.max_build_number = wb.build_number
169+
WHERE
170+
wb.transition = 'start'::workspace_transition;

coderd/schedule/autostop.go

+10
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import (
44
"context"
55
"time"
66

7+
"go.opentelemetry.io/otel/attribute"
8+
"go.opentelemetry.io/otel/trace"
79
"golang.org/x/xerrors"
810

911
"github.com/coder/coder/coderd/database"
12+
"github.com/coder/coder/coderd/tracing"
1013
)
1114

1215
const (
@@ -72,6 +75,13 @@ type AutostopTime struct {
7275
// Deadline is a cost saving measure, while max deadline is a
7376
// compliance/updating measure.
7477
func CalculateAutostop(ctx context.Context, params CalculateAutostopParams) (AutostopTime, error) {
78+
ctx, span := tracing.StartSpan(ctx,
79+
trace.WithAttributes(attribute.String("coder.workspace_id", params.Workspace.ID.String())),
80+
trace.WithAttributes(attribute.String("coder.template_id", params.Workspace.TemplateID.String())),
81+
)
82+
defer span.End()
83+
defer span.End()
84+
7585
var (
7686
db = params.Database
7787
workspace = params.Workspace

coderd/schedule/template.go

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"golang.org/x/xerrors"
99

1010
"github.com/coder/coder/coderd/database"
11+
"github.com/coder/coder/coderd/tracing"
1112
)
1213

1314
const MaxTemplateRestartRequirementWeeks = 16
@@ -122,6 +123,9 @@ func NewAGPLTemplateScheduleStore() TemplateScheduleStore {
122123
}
123124

124125
func (*agplTemplateScheduleStore) Get(ctx context.Context, db database.Store, templateID uuid.UUID) (TemplateScheduleOptions, error) {
126+
ctx, span := tracing.StartSpan(ctx)
127+
defer span.End()
128+
125129
tpl, err := db.GetTemplateByID(ctx, templateID)
126130
if err != nil {
127131
return TemplateScheduleOptions{}, err
@@ -148,6 +152,9 @@ func (*agplTemplateScheduleStore) Get(ctx context.Context, db database.Store, te
148152
}
149153

150154
func (*agplTemplateScheduleStore) Set(ctx context.Context, db database.Store, tpl database.Template, opts TemplateScheduleOptions) (database.Template, error) {
155+
ctx, span := tracing.StartSpan(ctx)
156+
defer span.End()
157+
151158
if int64(opts.DefaultTTL) == tpl.DefaultTTL {
152159
// Avoid updating the UpdatedAt timestamp if nothing will be changed.
153160
return tpl, nil

enterprise/coderd/coderd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ func (api *API) updateEntitlements(ctx context.Context) error {
507507

508508
if initial, changed, enabled := featureChanged(codersdk.FeatureAdvancedTemplateScheduling); shouldUpdate(initial, changed, enabled) {
509509
if enabled {
510-
templateStore := schedule.NewEnterpriseTemplateScheduleStore()
510+
templateStore := schedule.NewEnterpriseTemplateScheduleStore(api.AGPL.UserQuietHoursScheduleStore)
511511
templateStoreInterface := agplschedule.TemplateScheduleStore(templateStore)
512512
api.AGPL.TemplateScheduleStore.Store(&templateStoreInterface)
513513
} else {

0 commit comments

Comments
 (0)