Skip to content

Commit b65f620

Browse files
committed
created new ORM update to avoid forcing setting StartedAt on every Complete update
1 parent 3815727 commit b65f620

File tree

8 files changed

+107
-1
lines changed

8 files changed

+107
-1
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4254,6 +4254,10 @@ func (q *querier) UpdateProvisionerJobWithCompleteByID(ctx context.Context, arg
42544254
return q.db.UpdateProvisionerJobWithCompleteByID(ctx, arg)
42554255
}
42564256

4257+
func (q *querier) UpdateProvisionerJobWithCompleteWithStartedAtByID(ctx context.Context, arg database.UpdateProvisionerJobWithCompleteWithStartedAtByIDParams) error {
4258+
return q.db.UpdateProvisionerJobWithCompleteWithStartedAtByID(ctx, arg)
4259+
}
4260+
42574261
func (q *querier) UpdateReplica(ctx context.Context, arg database.UpdateReplicaParams) (database.Replica, error) {
42584262
if err := q.authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceSystem); err != nil {
42594263
return database.Replica{}, err

coderd/database/dbmem/dbmem.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10942,6 +10942,30 @@ func (q *FakeQuerier) UpdateProvisionerJobWithCompleteByID(_ context.Context, ar
1094210942
return sql.ErrNoRows
1094310943
}
1094410944

10945+
func (q *FakeQuerier) UpdateProvisionerJobWithCompleteWithStartedAtByID(_ context.Context, arg database.UpdateProvisionerJobWithCompleteWithStartedAtByIDParams) error {
10946+
if err := validateDatabaseType(arg); err != nil {
10947+
return err
10948+
}
10949+
10950+
q.mutex.Lock()
10951+
defer q.mutex.Unlock()
10952+
10953+
for index, job := range q.provisionerJobs {
10954+
if arg.ID != job.ID {
10955+
continue
10956+
}
10957+
job.UpdatedAt = arg.UpdatedAt
10958+
job.CompletedAt = arg.CompletedAt
10959+
job.Error = arg.Error
10960+
job.ErrorCode = arg.ErrorCode
10961+
job.JobStatus = provisionerJobStatus(job)
10962+
job.StartedAt = arg.StartedAt
10963+
q.provisionerJobs[index] = job
10964+
return nil
10965+
}
10966+
return sql.ErrNoRows
10967+
}
10968+
1094510969
func (q *FakeQuerier) UpdateReplica(_ context.Context, arg database.UpdateReplicaParams) (database.Replica, error) {
1094610970
if err := validateDatabaseType(arg); err != nil {
1094710971
return database.Replica{}, err

coderd/database/dbmetrics/querymetrics.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbmock/dbmock.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/provisionerjobs.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,18 @@ SET
256256
WHERE
257257
id = $1;
258258

259+
-- name: UpdateProvisionerJobWithCompleteWithStartedAtByID :exec
260+
UPDATE
261+
provisioner_jobs
262+
SET
263+
updated_at = $2,
264+
completed_at = $3,
265+
error = $4,
266+
error_code = $5,
267+
started_at = $6
268+
WHERE
269+
id = $1;
270+
259271
-- name: GetHungProvisionerJobs :many
260272
SELECT
261273
*

coderd/reaper/detector.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,16 @@ func reapJob(ctx context.Context, log slog.Logger, db database.Store, pub pubsub
311311

312312
// Mark the job as failed.
313313
now = dbtime.Now()
314-
err = db.UpdateProvisionerJobWithCompleteByID(ctx, database.UpdateProvisionerJobWithCompleteByIDParams{
314+
315+
// If the job was never started, set the StartedAt time to the current
316+
// time so that the build duration is correct.
317+
if !job.StartedAt.Valid {
318+
job.StartedAt = sql.NullTime{
319+
Time: now,
320+
Valid: true,
321+
}
322+
}
323+
err = db.UpdateProvisionerJobWithCompleteWithStartedAtByID(ctx, database.UpdateProvisionerJobWithCompleteWithStartedAtByIDParams{
315324
ID: job.ID,
316325
UpdatedAt: now,
317326
CompletedAt: sql.NullTime{
@@ -325,6 +334,7 @@ func reapJob(ctx context.Context, log slog.Logger, db database.Store, pub pubsub
325334
ErrorCode: sql.NullString{
326335
Valid: false,
327336
},
337+
StartedAt: job.StartedAt,
328338
})
329339
if err != nil {
330340
return xerrors.Errorf("mark job as failed: %w", err)

0 commit comments

Comments
 (0)