Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a99ce12

Browse files
committed
fix: Simplify provisionerd job acquire
This uses a simple channel to detect whether a job is running or not, and moves all cancels to be in goroutines.
1 parent e75bde4 commit a99ce12

File tree

7 files changed

+109
-105
lines changed

7 files changed

+109
-105
lines changed

coderd/coderd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Options struct {
2323
func New(options *Options) http.Handler {
2424
api := &api{
2525
Database: options.Database,
26+
Logger: options.Logger,
2627
Pubsub: options.Pubsub,
2728
}
2829

@@ -110,5 +111,6 @@ func New(options *Options) http.Handler {
110111
// be added to this struct for code clarity.
111112
type api struct {
112113
Database database.Store
114+
Logger slog.Logger
113115
Pubsub database.Pubsub
114116
}

coderd/coderdtest/coderdtest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func New(t *testing.T) Server {
125125
}
126126

127127
handler := coderd.New(&coderd.Options{
128-
Logger: slogtest.Make(t, nil),
128+
Logger: slogtest.Make(t, nil).Leveled(slog.LevelDebug),
129129
Database: db,
130130
Pubsub: pubsub,
131131
})

coderd/provisionerdaemons.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"storj.io/drpc/drpcmux"
2020
"storj.io/drpc/drpcserver"
2121

22+
"cdr.dev/slog"
23+
2224
"github.com/coder/coder/coderd/projectparameter"
2325
"github.com/coder/coder/database"
2426
"github.com/coder/coder/httpapi"
@@ -84,6 +86,7 @@ func (api *api) provisionerDaemonsServe(rw http.ResponseWriter, r *http.Request)
8486
Database: api.Database,
8587
Pubsub: api.Pubsub,
8688
Provisioners: daemon.Provisioners,
89+
Logger: api.Logger.Named(fmt.Sprintf("provisionerd-%s", daemon.Name)),
8790
})
8891
if err != nil {
8992
_ = conn.Close(websocket.StatusInternalError, fmt.Sprintf("drpc register provisioner daemon: %s", err))
@@ -109,6 +112,7 @@ type projectImportJob struct {
109112
// Implementation of the provisioner daemon protobuf server.
110113
type provisionerdServer struct {
111114
ID uuid.UUID
115+
Logger slog.Logger
112116
Provisioners []database.ProvisionerType
113117
Database database.Store
114118
Pubsub database.Pubsub
@@ -136,6 +140,8 @@ func (server *provisionerdServer) AcquireJob(ctx context.Context, _ *proto.Empty
136140
if err != nil {
137141
return nil, xerrors.Errorf("acquire job: %w", err)
138142
}
143+
server.Logger.Debug(ctx, "locked job from database", slog.F("id", job.ID))
144+
139145
// Marks the acquired job as failed with the error message provided.
140146
failJob := func(errorMessage string) error {
141147
err = server.Database.UpdateProvisionerJobByID(ctx, database.UpdateProvisionerJobByIDParams{
@@ -495,6 +501,7 @@ func (server *provisionerdServer) CompleteJob(ctx context.Context, completed *pr
495501
return xerrors.Errorf("insert project parameter %q: %w", projectParameter.Name, err)
496502
}
497503
}
504+
server.Logger.Debug(ctx, "marked import job as completed", slog.F("job_id", jobID))
498505
return nil
499506
})
500507
if err != nil {

coderd/provisioners.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type ProvisionerJobStatus string
1212

1313
// Completed returns whether the job is still processing.
1414
func (p ProvisionerJobStatus) Completed() bool {
15-
return p == ProvisionerJobStatusSucceeded || p == ProvisionerJobStatusFailed
15+
return p == ProvisionerJobStatusSucceeded || p == ProvisionerJobStatusFailed || p == ProvisionerJobStatusCancelled
1616
}
1717

1818
const (

coderd/workspacehistory.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ func (api *api) postWorkspaceHistoryByUser(rw http.ResponseWriter, r *http.Reque
8282
Message: fmt.Sprintf("The provided project history %q has failed to import. You cannot create workspaces using it!", projectHistory.Name),
8383
})
8484
return
85+
case ProvisionerJobStatusCancelled:
86+
httpapi.Write(rw, http.StatusPreconditionFailed, httpapi.Response{
87+
Message: "The provided project history was canceled during import. You cannot create workspaces using it!",
88+
})
8589
}
8690

8791
project, err := api.Database.GetProjectByID(r.Context(), projectHistory.ProjectID)

coderd/workspacehistory_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func TestWorkspaceHistory(t *testing.T) {
5656
require.Eventually(t, func() bool {
5757
hist, err := client.ProjectHistory(context.Background(), user.Organization, project.Name, projectHistory.Name)
5858
require.NoError(t, err)
59+
t.Logf("Import status: %s\n", hist.Import.Status)
5960
return hist.Import.Status.Completed()
6061
}, 15*time.Second, 50*time.Millisecond)
6162
return projectHistory

0 commit comments

Comments
 (0)