MCPcopy Index your code
hub / github.com/coder/coder / Set

Method Set

enterprise/coderd/schedule/template.go:106–297  ·  view source on GitHub ↗

Set implements agpl.TemplateScheduleStore.

(ctx context.Context, db database.Store, tpl database.Template, opts agpl.TemplateScheduleOptions)

Source from the content-addressed store, hash-verified

104
105// Set implements agpl.TemplateScheduleStore.
106func (s *EnterpriseTemplateScheduleStore) Set(ctx context.Context, db database.Store, tpl database.Template, opts agpl.TemplateScheduleOptions) (database.Template, error) {
107 ctx, span := tracing.StartSpan(ctx)
108 defer span.End()
109
110 if opts.AutostopRequirement.Weeks <= 0 {
111 opts.AutostopRequirement.Weeks = 1
112 }
113 if tpl.AutostopRequirementWeeks <= 0 {
114 tpl.AutostopRequirementWeeks = 1
115 }
116
117 if int64(opts.DefaultTTL) == tpl.DefaultTTL &&
118 int64(opts.ActivityBump) == tpl.ActivityBump &&
119 int16(opts.AutostopRequirement.DaysOfWeek) == tpl.AutostopRequirementDaysOfWeek &&
120 opts.AutostartRequirement.DaysOfWeek == tpl.AutostartAllowedDays() &&
121 opts.AutostopRequirement.Weeks == tpl.AutostopRequirementWeeks &&
122 int64(opts.FailureTTL) == tpl.FailureTTL &&
123 int64(opts.TimeTilDormant) == tpl.TimeTilDormant &&
124 int64(opts.TimeTilDormantAutoDelete) == tpl.TimeTilDormantAutoDelete &&
125 opts.UserAutostartEnabled == tpl.AllowUserAutostart &&
126 opts.UserAutostopEnabled == tpl.AllowUserAutostop {
127 // Avoid updating the UpdatedAt timestamp if nothing will be changed.
128 return tpl, nil
129 }
130
131 err := agpl.VerifyTemplateAutostopRequirement(opts.AutostopRequirement.DaysOfWeek, opts.AutostopRequirement.Weeks)
132 if err != nil {
133 return database.Template{}, xerrors.Errorf("verify autostop requirement: %w", err)
134 }
135
136 err = agpl.VerifyTemplateAutostartRequirement(opts.AutostartRequirement.DaysOfWeek)
137 if err != nil {
138 return database.Template{}, xerrors.Errorf("verify autostart requirement: %w", err)
139 }
140
141 var (
142 template database.Template
143 dormantWorkspacesUpdated []database.WorkspaceTable
144 )
145 err = db.InTx(func(tx database.Store) error {
146 ctx, span := tracing.StartSpanWithName(ctx, "(*schedule.EnterpriseTemplateScheduleStore).Set()-InTx()")
147 defer span.End()
148
149 err := tx.UpdateTemplateScheduleByID(ctx, database.UpdateTemplateScheduleByIDParams{
150 ID: tpl.ID,
151 UpdatedAt: s.now(),
152 AllowUserAutostart: opts.UserAutostartEnabled,
153 AllowUserAutostop: opts.UserAutostopEnabled,
154 DefaultTTL: int64(opts.DefaultTTL),
155 ActivityBump: int64(opts.ActivityBump),
156 AutostopRequirementDaysOfWeek: int16(opts.AutostopRequirement.DaysOfWeek),
157 AutostopRequirementWeeks: opts.AutostopRequirement.Weeks,
158 // Database stores the inverse of the allowed days of the week.
159 // Make sure the 8th bit is always zeroed out, as there is no 8th day of the week.
160 AutostartBlockDaysOfWeek: int16(^opts.AutostartRequirement.DaysOfWeek & 0b01111111),
161 FailureTTL: int64(opts.FailureTTL),
162 TimeTilDormant: int64(opts.TimeTilDormant),
163 TimeTilDormantAutoDelete: int64(opts.TimeTilDormantAutoDelete),

Callers 5

TestNotificationsFunction · 0.95
TestTemplateTTLFunction · 0.95

Calls 15

nowMethod · 0.95
updateWorkspaceBuildsMethod · 0.95
GetMethod · 0.95
StartSpanFunction · 0.92
StartSpanWithNameFunction · 0.92
AsSystemRestrictedFunction · 0.92
TimeFunction · 0.92
AsNotifierFunction · 0.92
AutostartAllowedDaysMethod · 0.80
InTxMethod · 0.65

Tested by 5

TestNotificationsFunction · 0.76
TestTemplateTTLFunction · 0.76