- Notifications
You must be signed in to change notification settings - Fork1k
refactor(coderd/provisionerdserver): use quartz.Clock instead of TimeNowFn#15642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -46,6 +46,7 @@ import ( | ||
"github.com/coder/coder/v2/provisionerd/proto" | ||
"github.com/coder/coder/v2/provisionersdk" | ||
sdkproto "github.com/coder/coder/v2/provisionersdk/proto" | ||
"github.com/coder/quartz" | ||
) | ||
const ( | ||
@@ -61,8 +62,9 @@ const ( | ||
type Options struct { | ||
OIDCConfig promoauth.OAuth2Config | ||
ExternalAuthConfigs []*externalauth.Config | ||
// Clock for testing | ||
Clock quartz.Clock | ||
// AcquireJobLongPollDur is used in tests | ||
AcquireJobLongPollDur time.Duration | ||
@@ -104,7 +106,7 @@ type server struct { | ||
OIDCConfig promoauth.OAuth2Config | ||
Clock quartz.Clock | ||
acquireJobLongPollDur time.Duration | ||
@@ -191,6 +193,9 @@ func NewServer( | ||
if options.HeartbeatInterval == 0 { | ||
options.HeartbeatInterval = DefaultHeartbeatInterval | ||
} | ||
if options.Clock == nil { | ||
options.Clock = quartz.NewReal() | ||
} | ||
Comment on lines +196 to +198 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. 👍 Even if | ||
s := &server{ | ||
lifecycleCtx: lifecycleCtx, | ||
@@ -213,7 +218,7 @@ func NewServer( | ||
UserQuietHoursScheduleStore: userQuietHoursScheduleStore, | ||
DeploymentValues: deploymentValues, | ||
OIDCConfig: options.OIDCConfig, | ||
Clock:options.Clock, | ||
acquireJobLongPollDur: options.AcquireJobLongPollDur, | ||
heartbeatInterval: options.HeartbeatInterval, | ||
heartbeatFn: options.HeartbeatFn, | ||
@@ -229,11 +234,8 @@ func NewServer( | ||
// timeNow should be used when trying to get the current time for math | ||
// calculations regarding workspace start and stop time. | ||
func (s *server) timeNow(tags ...string) time.Time { | ||
return dbtime.Time(s.Clock.Now(tags...)) | ||
Comment on lines +237 to +238 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. nice 👍 | ||
} | ||
// heartbeatLoop runs heartbeatOnce at the interval specified by HeartbeatInterval | ||
@@ -365,7 +367,7 @@ func (s *server) AcquireJobWithCancel(stream proto.DRPCProvisionerDaemon_Acquire | ||
logger.Error(streamCtx, "recv error and failed to cancel acquire job", slog.Error(recvErr)) | ||
// Well, this is awkward. We hit an error receiving from the stream, but didn't cancel before we locked a job | ||
// in the database. We need to mark this job as failed so the end user can retry if they want to. | ||
now :=s.timeNow() | ||
err := s.Database.UpdateProvisionerJobWithCompleteByID( | ||
//nolint:gocritic // Provisionerd has specific authz rules. | ||
dbauthz.AsProvisionerd(context.Background()), | ||
@@ -406,15 +408,15 @@ func (s *server) acquireProtoJob(ctx context.Context, job database.ProvisionerJo | ||
err := s.Database.UpdateProvisionerJobWithCompleteByID(ctx, database.UpdateProvisionerJobWithCompleteByIDParams{ | ||
ID: job.ID, | ||
CompletedAt: sql.NullTime{ | ||
Time:s.timeNow(), | ||
Valid: true, | ||
}, | ||
Error: sql.NullString{ | ||
String: errorMessage, | ||
Valid: true, | ||
}, | ||
ErrorCode: job.ErrorCode, | ||
UpdatedAt:s.timeNow(), | ||
}) | ||
if err != nil { | ||
return xerrors.Errorf("update provisioner job: %w", err) | ||
@@ -792,7 +794,7 @@ func (s *server) UpdateJob(ctx context.Context, request *proto.UpdateJobRequest) | ||
} | ||
err = s.Database.UpdateProvisionerJobByID(ctx, database.UpdateProvisionerJobByIDParams{ | ||
ID: parsedID, | ||
UpdatedAt:s.timeNow(), | ||
}) | ||
if err != nil { | ||
return nil, xerrors.Errorf("update job: %w", err) | ||
@@ -869,7 +871,7 @@ func (s *server) UpdateJob(ctx context.Context, request *proto.UpdateJobRequest) | ||
err := s.Database.UpdateTemplateVersionDescriptionByJobID(ctx, database.UpdateTemplateVersionDescriptionByJobIDParams{ | ||
JobID: job.ID, | ||
Readme: string(request.Readme), | ||
UpdatedAt:s.timeNow(), | ||
}) | ||
if err != nil { | ||
return nil, xerrors.Errorf("update template version description: %w", err) | ||
@@ -958,7 +960,7 @@ func (s *server) FailJob(ctx context.Context, failJob *proto.FailedJob) (*proto. | ||
return nil, xerrors.Errorf("job already completed") | ||
} | ||
job.CompletedAt = sql.NullTime{ | ||
Time:s.timeNow(), | ||
Valid: true, | ||
} | ||
job.Error = sql.NullString{ | ||
@@ -973,7 +975,7 @@ func (s *server) FailJob(ctx context.Context, failJob *proto.FailedJob) (*proto. | ||
err = s.Database.UpdateProvisionerJobWithCompleteByID(ctx, database.UpdateProvisionerJobWithCompleteByIDParams{ | ||
ID: jobID, | ||
CompletedAt: job.CompletedAt, | ||
UpdatedAt:s.timeNow(), | ||
Error: job.Error, | ||
ErrorCode: job.ErrorCode, | ||
}) | ||
@@ -1008,15 +1010,15 @@ func (s *server) FailJob(ctx context.Context, failJob *proto.FailedJob) (*proto. | ||
if jobType.WorkspaceBuild.State != nil { | ||
err = db.UpdateWorkspaceBuildProvisionerStateByID(ctx, database.UpdateWorkspaceBuildProvisionerStateByIDParams{ | ||
ID: input.WorkspaceBuildID, | ||
UpdatedAt:s.timeNow(), | ||
ProvisionerState: jobType.WorkspaceBuild.State, | ||
}) | ||
if err != nil { | ||
return xerrors.Errorf("update workspace build state: %w", err) | ||
} | ||
err = db.UpdateWorkspaceBuildDeadlineByID(ctx, database.UpdateWorkspaceBuildDeadlineByIDParams{ | ||
ID: input.WorkspaceBuildID, | ||
UpdatedAt:s.timeNow(), | ||
Deadline: build.Deadline, | ||
MaxDeadline: build.MaxDeadline, | ||
}) | ||
@@ -1382,17 +1384,17 @@ func (s *server) CompleteJob(ctx context.Context, completed *proto.CompletedJob) | ||
err = s.Database.UpdateTemplateVersionExternalAuthProvidersByJobID(ctx, database.UpdateTemplateVersionExternalAuthProvidersByJobIDParams{ | ||
JobID: jobID, | ||
ExternalAuthProviders: json.RawMessage(externalAuthProvidersMessage), | ||
UpdatedAt:s.timeNow(), | ||
}) | ||
if err != nil { | ||
return nil, xerrors.Errorf("update template version external auth providers: %w", err) | ||
} | ||
err = s.Database.UpdateProvisionerJobWithCompleteByID(ctx, database.UpdateProvisionerJobWithCompleteByIDParams{ | ||
ID: jobID, | ||
UpdatedAt:s.timeNow(), | ||
CompletedAt: sql.NullTime{ | ||
Time:s.timeNow(), | ||
Valid: true, | ||
}, | ||
Error: completedError, | ||
@@ -1687,9 +1689,9 @@ func (s *server) CompleteJob(ctx context.Context, completed *proto.CompletedJob) | ||
err = s.Database.UpdateProvisionerJobWithCompleteByID(ctx, database.UpdateProvisionerJobWithCompleteByIDParams{ | ||
ID: jobID, | ||
UpdatedAt:s.timeNow(), | ||
CompletedAt: sql.NullTime{ | ||
Time:s.timeNow(), | ||
Valid: true, | ||
}, | ||
Error: sql.NullString{}, | ||
Uh oh!
There was an error while loading.Please reload this page.