-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathprovisionerd.go
More file actions
714 lines (654 loc) · 22.6 KB
/
Copy pathprovisionerd.go
File metadata and controls
714 lines (654 loc) · 22.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
package provisionerd
import (
"context"
"crypto/sha256"
"errors"
"fmt"
"io"
"net/http"
"reflect"
"sync"
"time"
"github.com/hashicorp/yamux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/valyala/fasthttp/fasthttputil"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.14.0"
"go.opentelemetry.io/otel/trace"
"golang.org/x/xerrors"
protobuf "google.golang.org/protobuf/proto"
"cdr.dev/slog/v3"
"github.com/coder/coder/v2/coderd/tracing"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/drpcsdk"
"github.com/coder/coder/v2/provisionerd/proto"
"github.com/coder/coder/v2/provisionerd/runner"
"github.com/coder/coder/v2/provisionersdk"
sdkproto "github.com/coder/coder/v2/provisionersdk/proto"
"github.com/coder/retry"
)
// Dialer represents the function to create a daemon client connection.
type Dialer func(ctx context.Context) (proto.DRPCProvisionerDaemonClient, error)
// ConnectResponse is the response returned asynchronously from Connector.Connect
// containing either the Provisioner Client or an Error. The Job is also returned
// unaltered to disambiguate responses if the respCh is shared among multiple jobs
type ConnectResponse struct {
Job *proto.AcquiredJob
Client sdkproto.DRPCProvisionerClient
Error error
}
// Connector allows the provisioner daemon to Connect to a provisioner
// for the given job.
type Connector interface {
// Connect to the correct provisioner for the given job. The response is
// delivered asynchronously over the respCh. If the provided context expires,
// the Connector may stop waiting for the provisioner and return an error
// response.
Connect(ctx context.Context, job *proto.AcquiredJob, respCh chan<- ConnectResponse)
}
// Options provides customizations to the behavior of a provisioner daemon.
type Options struct {
Logger slog.Logger
TracerProvider trace.TracerProvider
Metrics *Metrics
ExternalProvisioner bool
ForceCancelInterval time.Duration
UpdateInterval time.Duration
LogBufferInterval time.Duration
Connector Connector
InitConnectionCh chan struct{} // only to be used in tests
}
// New creates and starts a provisioner daemon.
func New(clientDialer Dialer, opts *Options) *Server {
if opts == nil {
opts = &Options{}
}
if opts.UpdateInterval == 0 {
opts.UpdateInterval = 5 * time.Second
}
if opts.ForceCancelInterval == 0 {
opts.ForceCancelInterval = 10 * time.Minute
}
if opts.LogBufferInterval == 0 {
opts.LogBufferInterval = 250 * time.Millisecond
}
if opts.TracerProvider == nil {
opts.TracerProvider = trace.NewNoopTracerProvider()
}
if opts.Metrics == nil {
reg := prometheus.NewRegistry()
mets := NewMetrics(reg)
opts.Metrics = &mets
}
if opts.InitConnectionCh == nil {
opts.InitConnectionCh = make(chan struct{})
}
ctx, ctxCancel := context.WithCancel(context.Background())
daemon := &Server{
opts: opts,
tracer: opts.TracerProvider.Tracer(tracing.TracerName),
clientDialer: clientDialer,
clientCh: make(chan proto.DRPCProvisionerDaemonClient),
closeContext: ctx,
closeCancel: ctxCancel,
closedCh: make(chan struct{}),
shuttingDownCh: make(chan struct{}),
acquireDoneCh: make(chan struct{}),
initConnectionCh: opts.InitConnectionCh,
externalProvisioner: opts.ExternalProvisioner,
}
daemon.wg.Add(2)
go daemon.connect()
go daemon.acquireLoop()
return daemon
}
type Server struct {
opts *Options
tracer trace.Tracer
clientDialer Dialer
clientCh chan proto.DRPCProvisionerDaemonClient
wg sync.WaitGroup
// initConnectionCh will receive when the daemon connects to coderd for the
// first time.
initConnectionCh chan struct{}
initConnectionOnce sync.Once
// mutex protects all subsequent fields
mutex sync.Mutex
// closeContext is canceled when we start closing.
closeContext context.Context
closeCancel context.CancelFunc
// closeError stores the error when closing to return to subsequent callers
closeError error
// closingB is set to true when we start closing
closingB bool
// closedCh will receive when we complete closing
closedCh chan struct{}
// shuttingDownB is set to true when we start graceful shutdown
shuttingDownB bool
// shuttingDownCh will receive when we start graceful shutdown
shuttingDownCh chan struct{}
// acquireDoneCh will receive when the acquireLoop exits
acquireDoneCh chan struct{}
activeJob *runner.Runner
externalProvisioner bool
}
type Metrics struct {
Runner runner.Metrics
}
func NewMetrics(reg prometheus.Registerer) Metrics {
auto := promauto.With(reg)
return Metrics{
Runner: runner.Metrics{
ConcurrentJobs: auto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "coderd",
Subsystem: "provisionerd",
Name: "jobs_current",
Help: "The number of currently running provisioner jobs.",
}, []string{"provisioner"}),
NumDaemons: auto.NewGauge(prometheus.GaugeOpts{
Namespace: "coderd",
Subsystem: "provisionerd",
Name: "num_daemons",
Help: "The number of provisioner daemons.",
}),
JobTimings: auto.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "coderd",
Subsystem: "provisionerd",
Name: "job_timings_seconds",
Help: "The provisioner job time duration in seconds.",
Buckets: []float64{
1, // 1s
10,
30,
60, // 1min
60 * 5,
60 * 10,
60 * 30, // 30min
60 * 60, // 1hr
},
}, []string{"provisioner", "status"}),
WorkspaceBuilds: auto.NewCounterVec(prometheus.CounterOpts{
Namespace: "coderd",
Subsystem: "", // Explicitly empty to make this a top-level metric.
Name: "workspace_builds_total",
Help: "The number of workspaces started, updated, or deleted.",
}, []string{"workspace_owner", "workspace_name", "template_name", "template_version", "workspace_transition", "status"}),
WorkspaceBuildTimings: auto.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "coderd",
Subsystem: "provisionerd",
Name: "workspace_build_timings_seconds",
Help: "The time taken for a workspace to build.",
Buckets: []float64{
1, // 1s
10,
30,
60, // 1min
60 * 5,
60 * 10,
60 * 30, // 30min
60 * 60, // 1hr
},
}, []string{"template_name", "template_version", "workspace_transition", "status"}),
},
}
}
// Connect establishes a connection to coderd.
func (p *Server) connect() {
defer p.opts.Logger.Debug(p.closeContext, "connect loop exited")
defer p.wg.Done()
logConnect := p.opts.Logger.Debug
if p.externalProvisioner {
logConnect = p.opts.Logger.Info
}
// An exponential back-off occurs when the connection is failing to dial.
// This is to prevent server spam in case of a coderd outage.
connectLoop:
for retrier := retry.New(50*time.Millisecond, 10*time.Second); retrier.Wait(p.closeContext); {
// It's possible for the provisioner daemon to be shut down
// before the wait is complete!
if p.isClosed() {
return
}
p.opts.Logger.Debug(p.closeContext, "dialing coderd")
client, err := p.clientDialer(p.closeContext)
if err != nil {
if errors.Is(err, context.Canceled) {
return
}
var sdkErr *codersdk.Error
// If something is wrong with our auth, stop trying to connect.
if errors.As(err, &sdkErr) && sdkErr.StatusCode() == http.StatusForbidden {
p.opts.Logger.Error(p.closeContext, "not authorized to dial coderd", slog.Error(err))
return
}
if p.isClosed() {
return
}
p.opts.Logger.Warn(p.closeContext, "coderd client failed to dial", slog.Error(err))
continue
}
// This log is useful to verify that an external provisioner daemon is
// successfully connecting to coderd. It doesn't add much value if the
// daemon is built-in, so we only log it on the info level if p.externalProvisioner
// is true. This log message is mentioned in the docs:
// https://github.com/coder/coder/blob/5bd86cb1c06561d1d3e90ce689da220467e525c0/docs/admin/provisioners.md#L346
logConnect(p.closeContext, "successfully connected to coderd")
retrier.Reset()
p.initConnectionOnce.Do(func() {
close(p.initConnectionCh)
})
// serve the client until we are closed or it disconnects
for {
select {
case <-p.closeContext.Done():
client.DRPCConn().Close()
return
case <-client.DRPCConn().Closed():
logConnect(p.closeContext, "connection to coderd closed")
continue connectLoop
case p.clientCh <- client:
continue
}
}
}
}
func (p *Server) client() (proto.DRPCProvisionerDaemonClient, bool) {
select {
case <-p.closeContext.Done():
return nil, false
case <-p.shuttingDownCh:
// Shutting down should return a nil client and unblock
return nil, false
case client := <-p.clientCh:
return client, true
}
}
func (p *Server) acquireLoop() {
defer p.opts.Logger.Debug(p.closeContext, "acquire loop exited")
defer p.wg.Done()
defer func() { close(p.acquireDoneCh) }()
ctx := p.closeContext
for retrier := retry.New(10*time.Millisecond, 1*time.Second); retrier.Wait(ctx); {
if p.acquireExit() {
return
}
client, ok := p.client()
if !ok {
p.opts.Logger.Debug(ctx, "shut down before client (re) connected")
return
}
err := p.acquireAndRunOne(client)
if err != nil && ctx.Err() == nil { // Only log if context is not done.
// Short-circuit: don't wait for the retry delay to exit, if required.
if p.acquireExit() {
return
}
p.opts.Logger.Warn(ctx, "failed to acquire job, retrying", slog.F("delay", fmt.Sprintf("%vms", retrier.Delay.Milliseconds())), slog.Error(err))
} else {
// Reset the retrier after each successful acquisition.
retrier.Reset()
}
}
}
// acquireExit returns true if the acquire loop should exit
func (p *Server) acquireExit() bool {
p.mutex.Lock()
defer p.mutex.Unlock()
if p.closingB {
p.opts.Logger.Debug(p.closeContext, "exiting acquire; provisionerd is closing")
return true
}
if p.shuttingDownB {
p.opts.Logger.Debug(p.closeContext, "exiting acquire; provisionerd is shutting down")
return true
}
return false
}
func (p *Server) acquireAndRunOne(client proto.DRPCProvisionerDaemonClient) error {
ctx := p.closeContext
p.opts.Logger.Debug(ctx, "start of acquireAndRunOne")
job, err := p.acquireGraceful(client)
p.opts.Logger.Debug(ctx, "graceful acquire done", slog.F("job_id", job.GetJobId()), slog.Error(err))
if err != nil {
if errors.Is(err, context.Canceled) ||
errors.Is(err, yamux.ErrSessionShutdown) ||
errors.Is(err, fasthttputil.ErrInmemoryListenerClosed) {
return err
}
p.opts.Logger.Warn(ctx, "provisionerd was unable to acquire job", slog.Error(err))
return xerrors.Errorf("failed to acquire job: %w", err)
}
if job.JobId == "" {
p.opts.Logger.Debug(ctx, "acquire job successfully canceled")
return nil
}
if len(job.TraceMetadata) > 0 {
ctx = tracing.MetadataToContext(ctx, job.TraceMetadata)
}
ctx, span := p.tracer.Start(ctx, tracing.FuncName(), trace.WithAttributes(
semconv.ServiceNameKey.String("coderd.provisionerd"),
attribute.String("job_id", job.JobId),
attribute.String("job_type", reflect.TypeOf(job.GetType()).Elem().Name()),
attribute.Int64("job_created_at", job.CreatedAt),
attribute.String("initiator_username", job.UserName),
attribute.String("provisioner", job.Provisioner),
attribute.Int("template_size_bytes", len(job.TemplateSourceArchive)),
))
defer span.End()
fields := []slog.Field{
slog.F("initiator_username", job.UserName),
slog.F("provisioner", job.Provisioner),
slog.F("job_id", job.JobId),
}
if build := job.GetWorkspaceBuild(); build != nil {
fields = append(fields,
slog.F("workspace_transition", build.Metadata.WorkspaceTransition.String()),
slog.F("workspace_owner", build.Metadata.WorkspaceOwner),
slog.F("template_name", build.Metadata.TemplateName),
slog.F("template_version", build.Metadata.TemplateVersion),
slog.F("workspace_build_id", build.WorkspaceBuildId),
slog.F("workspace_id", build.Metadata.WorkspaceId),
slog.F("workspace_name", build.WorkspaceName),
slog.F("prebuilt_workspace_build_stage", build.Metadata.GetPrebuiltWorkspaceBuildStage().String()),
)
span.SetAttributes(
attribute.String("workspace_build_id", build.WorkspaceBuildId),
attribute.String("workspace_id", build.Metadata.WorkspaceId),
attribute.String("workspace_name", build.WorkspaceName),
attribute.String("workspace_owner_id", build.Metadata.WorkspaceOwnerId),
attribute.String("workspace_owner", build.Metadata.WorkspaceOwner),
attribute.String("workspace_transition", build.Metadata.WorkspaceTransition.String()),
attribute.String("prebuilt_workspace_build_stage", build.Metadata.GetPrebuiltWorkspaceBuildStage().String()),
)
}
p.opts.Logger.Debug(ctx, "acquired job", fields...)
respCh := make(chan ConnectResponse)
p.opts.Connector.Connect(ctx, job, respCh)
resp := <-respCh
if resp.Error != nil {
err := p.FailJob(ctx, &proto.FailedJob{
JobId: job.JobId,
Error: fmt.Sprintf("failed to connect to provisioner: %s", resp.Error),
})
if err != nil {
p.opts.Logger.Error(ctx, "failed to report provisioner job failed", slog.F("job_id", job.JobId), slog.Error(err))
}
return xerrors.Errorf("failed to report provisioner job failed: %w", err)
}
p.mutex.Lock()
p.activeJob = runner.New(
ctx,
job,
runner.Options{
Updater: p,
QuotaCommitter: p,
FileDownloader: p,
Logger: p.opts.Logger.Named("runner"),
Provisioner: resp.Client,
UpdateInterval: p.opts.UpdateInterval,
ForceCancelInterval: p.opts.ForceCancelInterval,
LogDebounceInterval: p.opts.LogBufferInterval,
Tracer: p.tracer,
Metrics: p.opts.Metrics.Runner,
},
)
p.mutex.Unlock()
p.activeJob.Run()
p.mutex.Lock()
p.activeJob = nil
p.mutex.Unlock()
return nil
}
// acquireGraceful attempts to acquire a job from the server, handling canceling the acquisition if we gracefully shut
// down.
func (p *Server) acquireGraceful(client proto.DRPCProvisionerDaemonClient) (*proto.AcquiredJob, error) {
stream, err := client.AcquireJobWithCancel(p.closeContext)
if err != nil {
return nil, err
}
acquireDone := make(chan struct{})
go func() {
select {
case <-p.closeContext.Done():
return
case <-p.shuttingDownCh:
p.opts.Logger.Debug(p.closeContext, "sending acquire job cancel")
err := stream.Send(&proto.CancelAcquire{})
if err != nil {
p.opts.Logger.Warn(p.closeContext, "failed to gracefully cancel acquire job")
}
return
case <-acquireDone:
return
}
}()
job, err := stream.Recv()
close(acquireDone)
return job, err
}
func retryable(err error) bool {
return xerrors.Is(err, yamux.ErrSessionShutdown) || xerrors.Is(err, io.EOF) || xerrors.Is(err, fasthttputil.ErrInmemoryListenerClosed) ||
// annoyingly, dRPC sometimes returns context.Canceled if the transport was closed, even if the context for
// the RPC *is not canceled*. Retrying is fine if the RPC context is not canceled.
xerrors.Is(err, context.Canceled)
}
// clientDoWithRetries runs the function f with a client, and retries with
// backoff until either the error returned is not retryable() or the context
// expires.
func clientDoWithRetries[T any](ctx context.Context,
getClient func() (proto.DRPCProvisionerDaemonClient, bool),
f func(context.Context, proto.DRPCProvisionerDaemonClient) (T, error),
) (ret T, _ error) {
for retrier := retry.New(25*time.Millisecond, 5*time.Second); retrier.Wait(ctx); {
client, ok := getClient()
if !ok {
continue
}
resp, err := f(ctx, client)
if retryable(err) {
continue
}
return resp, err
}
return ret, ctx.Err()
}
func (p *Server) CommitQuota(ctx context.Context, in *proto.CommitQuotaRequest) (*proto.CommitQuotaResponse, error) {
out, err := clientDoWithRetries(ctx, p.client, func(ctx context.Context, client proto.DRPCProvisionerDaemonClient) (*proto.CommitQuotaResponse, error) {
return client.CommitQuota(ctx, in)
})
if err != nil {
return nil, err
}
return out, nil
}
func (p *Server) UpdateJob(ctx context.Context, in *proto.UpdateJobRequest) (*proto.UpdateJobResponse, error) {
out, err := clientDoWithRetries(ctx, p.client, func(ctx context.Context, client proto.DRPCProvisionerDaemonClient) (*proto.UpdateJobResponse, error) {
return client.UpdateJob(ctx, in)
})
if err != nil {
return nil, err
}
return out, nil
}
func (p *Server) FailJob(ctx context.Context, in *proto.FailedJob) error {
_, err := clientDoWithRetries(ctx, p.client, func(ctx context.Context, client proto.DRPCProvisionerDaemonClient) (*proto.Empty, error) {
return client.FailJob(ctx, in)
})
return err
}
// UploadModuleFiles will insert a file into the database of coderd.
func (p *Server) UploadModuleFiles(ctx context.Context, moduleFiles []byte) error {
// Send the files separately if the message size is too large.
_, err := clientDoWithRetries(ctx, p.client, func(ctx context.Context, client proto.DRPCProvisionerDaemonClient) (*proto.Empty, error) {
// Add some timeout to prevent the stream from hanging indefinitely.
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
stream, err := client.UploadFile(ctx)
if err != nil {
return nil, xerrors.Errorf("failed to start UploadModuleFiles stream: %w", err)
}
defer stream.Close()
dataUp, chunks, err := sdkproto.BytesToDataUpload(sdkproto.DataUploadType_UPLOAD_TYPE_MODULE_FILES, moduleFiles)
if err != nil {
return nil, xerrors.Errorf("prepare module files upload: %w", err)
}
err = stream.Send(&sdkproto.FileUpload{Type: &sdkproto.FileUpload_DataUpload{DataUpload: dataUp}})
if err != nil {
if retryable(err) { // Do not retry
return nil, xerrors.Errorf("send data upload: %s", err.Error())
}
return nil, xerrors.Errorf("send data upload: %w", err)
}
for i, chunk := range chunks {
err = stream.Send(&sdkproto.FileUpload{Type: &sdkproto.FileUpload_ChunkPiece{ChunkPiece: chunk}})
if err != nil {
if retryable(err) { // Do not retry
return nil, xerrors.Errorf("send chunk piece: %s", err.Error())
}
return nil, xerrors.Errorf("send chunk piece %d: %w", i, err)
}
}
resp, err := stream.CloseAndRecv()
if err != nil {
if retryable(err) { // Do not retry
return nil, xerrors.Errorf("close stream: %s", err.Error())
}
return nil, xerrors.Errorf("close stream: %w", err)
}
return resp, nil
})
if err != nil {
return xerrors.Errorf("upload module files: %w", err)
}
return nil
}
// DownloadFile will download a module file from coderd.
func (p *Server) DownloadFile(ctx context.Context, request *proto.FileRequest) ([]byte, error) {
data, err := clientDoWithRetries(ctx, p.client, func(ctx context.Context, client proto.DRPCProvisionerDaemonClient) ([]byte, error) {
// Add some timeout to prevent the stream from hanging indefinitely if something goes wrong.
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
stream, err := client.DownloadFile(ctx, request)
if err != nil {
return nil, xerrors.Errorf("failed to start DownloadFile stream: %w", err)
}
defer stream.Close()
file, err := provisionersdk.HandleReceivingDataUpload(stream)
if err != nil {
return nil, xerrors.Errorf("failed to handle receiving data upload: %w", err)
}
data, err := file.Complete()
if err != nil {
return nil, xerrors.Errorf("failed to download file: %w", err)
}
return data, nil
})
if err != nil {
return nil, xerrors.Errorf("download file %s: %w", request.FileId, err)
}
return data, nil
}
func (p *Server) CompleteJob(ctx context.Context, in *proto.CompletedJob) error {
// If the moduleFiles exceed the max message size, we need to upload them separately.
if ti, ok := in.Type.(*proto.CompletedJob_TemplateImport_); ok {
messageSize := protobuf.Size(in)
if messageSize > drpcsdk.MaxMessageSize &&
messageSize-len(ti.TemplateImport.ModuleFiles) < drpcsdk.MaxMessageSize {
// Hashing the module files to reference them in the CompletedJob message.
moduleFilesHash := sha256.Sum256(ti.TemplateImport.ModuleFiles)
moduleFiles := ti.TemplateImport.ModuleFiles
ti.TemplateImport.ModuleFiles = []byte{} // Clear the files in the final message
ti.TemplateImport.ModuleFilesHash = moduleFilesHash[:]
err := p.UploadModuleFiles(ctx, moduleFiles)
if err != nil {
return err
}
}
}
_, err := clientDoWithRetries(ctx, p.client, func(ctx context.Context, client proto.DRPCProvisionerDaemonClient) (*proto.Empty, error) {
return client.CompleteJob(ctx, in)
})
return err
}
// isClosed returns whether the API is closed or not.
func (p *Server) isClosed() bool {
select {
case <-p.closeContext.Done():
return true
default:
return false
}
}
// Shutdown gracefully exists with the option to cancel the active job.
// If false, it will wait for the job to complete.
//
//nolint:revive
func (p *Server) Shutdown(ctx context.Context, cancelActiveJob bool) error {
p.mutex.Lock()
p.opts.Logger.Info(ctx, "attempting graceful shutdown")
if !p.shuttingDownB {
close(p.shuttingDownCh)
p.shuttingDownB = true
}
if cancelActiveJob && p.activeJob != nil {
p.activeJob.Cancel()
}
p.mutex.Unlock()
select {
case <-ctx.Done():
p.opts.Logger.Warn(ctx, "graceful shutdown failed", slog.Error(ctx.Err()))
return ctx.Err()
case <-p.acquireDoneCh:
p.opts.Logger.Info(ctx, "gracefully shutdown")
return nil
}
}
// Close ends the provisioner. It will mark any running jobs as failed.
func (p *Server) Close() error {
p.opts.Logger.Info(p.closeContext, "closing provisionerd")
return p.closeWithError(nil)
}
// closeWithError closes the provisioner; subsequent reads/writes will return the error err.
func (p *Server) closeWithError(err error) error {
p.mutex.Lock()
var activeJob *runner.Runner
first := false
if !p.closingB {
first = true
p.closingB = true
// only the first caller to close should attempt to fail the active job
activeJob = p.activeJob
}
// don't hold the mutex while doing I/O.
p.mutex.Unlock()
if activeJob != nil {
errMsg := "provisioner daemon was shutdown gracefully"
if err != nil {
errMsg = err.Error()
}
p.opts.Logger.Debug(p.closeContext, "failing active job because of close")
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
failErr := activeJob.Fail(ctx, &proto.FailedJob{Error: errMsg})
if failErr != nil {
activeJob.ForceStop()
}
if err == nil {
err = failErr
}
}
if first {
p.closeCancel()
p.opts.Logger.Debug(context.Background(), "waiting for goroutines to exit")
p.wg.Wait()
p.opts.Logger.Debug(context.Background(), "closing server with error", slog.Error(err))
p.closeError = err
close(p.closedCh)
return err
}
p.opts.Logger.Debug(p.closeContext, "waiting for first closer to complete")
<-p.closedCh
p.opts.Logger.Debug(p.closeContext, "first closer completed")
return p.closeError
}