pulumi/pkg/resource/plugin/context.go
Sean Gillespie 14baf866f6
Snapshot management overhaul and refactor (#1273)
* Refactor the SnapshotManager interface

Lift snapshot management out of the engine by delegating it to the
SnapshotManager implementation in pkg/backend.

* Add a event interface for plugin loads and use that interface to record plugins in the snapshot

* Remove dead code

* Add comments to Events

* Add a number of tests for SnapshotManager

* CR feedback: use a successful bit on 'End' instead of having a separate 'Abort' API

* CR feedback

* CR feedback: register plugins one-at-a-time instead of the entire state at once
2018-04-25 17:20:08 -07:00

60 lines
1.7 KiB
Go

// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
package plugin
import (
"context"
"github.com/opentracing/opentracing-go"
"github.com/pulumi/pulumi/pkg/diag"
"github.com/pulumi/pulumi/pkg/util/rpcutil"
)
// Context is used to group related operations together so that associated OS resources can be cached, shared, and
// reclaimed as appropriate.
type Context struct {
Diag diag.Sink // the diagnostics sink to use for messages.
Host Host // the host that can be used to fetch providers.
Pwd string // the working directory to spawn all plugins in.
tracingSpan opentracing.Span // the OpenTracing span to parent requests within.
}
// NewContext allocates a new context with a given sink and host. Note that the host is "owned" by this context from
// here forwards, such that when the context's resources are reclaimed, so too are the host's.
func NewContext(d diag.Sink, host Host, cfg ConfigSource, events Events,
pwd string, parentSpan opentracing.Span) (*Context, error) {
ctx := &Context{
Diag: d,
Host: host,
Pwd: pwd,
tracingSpan: parentSpan,
}
if host == nil {
h, err := NewDefaultHost(ctx, cfg, events)
if err != nil {
return nil, err
}
ctx.Host = h
}
return ctx, nil
}
// Request allocates a request sub-context.
func (ctx *Context) Request() context.Context {
// TODO[pulumi/pulumi#143]: support cancellation.
return opentracing.ContextWithSpan(context.Background(), ctx.tracingSpan)
}
// Close reclaims all resources associated with this context.
func (ctx *Context) Close() error {
if ctx.tracingSpan != nil {
ctx.tracingSpan.Finish()
}
err := ctx.Host.Close()
if err != nil && !rpcutil.IsBenignCloseErr(err) {
return err
}
return nil
}