Set implements agpl.TemplateScheduleStore.
(ctx context.Context, db database.Store, tpl database.Template, opts agpl.TemplateScheduleOptions)
| 104 | |
| 105 | // Set implements agpl.TemplateScheduleStore. |
| 106 | func (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), |