Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rpc wrapper [draft] #356

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 21 additions & 53 deletions pulumi-language-dotnet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"syscall"
Expand Down Expand Up @@ -55,23 +53,29 @@ var dotnetProcessExitedAfterShowingUserActionableMessage = 32
// Launches the language host RPC endpoint, which in turn fires up an RPC server implementing the
// LanguageRuntimeServer RPC endpoint.
func main() {
var tracing string

logging.InitLogging(false, 0, false)

rc, err := rpcCmd.NewRpcCmd(&rpcCmd.RpcCmdConfig{
TracingName: "pulumi-language-dotnet",
RootSpanName: "pulumi-language-dotnet",
})
if err != nil {
cmdutil.Exit(err)
}

var binary string
var root string
flag.StringVar(&tracing, "tracing", "", "Emit tracing to a Zipkin-compatible tracing endpoint")
flag.StringVar(&binary, "binary", "", "A relative or an absolute path to a precompiled .NET assembly to execute")
flag.StringVar(&root, "root", "", "Project root path to use")

rc.Flag.StringVar(&binary, "binary", "", "A relative or an absolute path to a precompiled .NET assembly to execute")
rc.Flag.StringVar(&root, "root", "", "Project root path to use")
// You can use the below flag to request that the language host load a specific executor instead of probing the
// PATH. This can be used during testing to override the default location.
var givenExecutor string
flag.StringVar(&givenExecutor, "use-executor", "",
rc.Flag.StringVar(&givenExecutor, "use-executor", "",
"Use the given program as the executor instead of looking for one on PATH")

flag.Parse()
args := flag.Args()
logging.InitLogging(false, 0, false)
cmdutil.InitTracing("pulumi-language-dotnet", "pulumi-language-dotnet", tracing)
rc.Flag.Parse(os.Args[1:])

var dotnetExec string
switch {
case givenExecutor != "":
Expand All @@ -90,47 +94,11 @@ func main() {
dotnetExec = pathExec
}

// Optionally pluck out the engine so we can do logging, etc.
var engineAddress string
if len(args) > 0 {
engineAddress = args[0]
}

ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
// Map the context Done channel to the rpcutil boolean cancel channel.
// The context will close on SIGINT or Healthcheck failure.
cancelChannel := make(chan bool)
go func() {
<-ctx.Done()
cancel() // remove the interrupt handler
close(cancelChannel)
}()
err := rpcutil.Healthcheck(ctx, engineAddress, 5*time.Minute, cancel)
if err != nil {
cmdutil.Exit(errors.Wrapf(err, "could not start health check host RPC server"))
}

// Fire up a gRPC server, letting the kernel choose a free port.
handle, err := rpcutil.ServeWithOptions(rpcutil.ServeOptions{
Cancel: cancelChannel,
Init: func(srv *grpc.Server) error {
host := newLanguageHost(dotnetExec, engineAddress, tracing, binary)
pulumirpc.RegisterLanguageRuntimeServer(srv, host)
return nil
},
Options: rpcutil.OpenTracingServerInterceptorOptions(nil),
})
if err != nil {
cmdutil.Exit(errors.Wrapf(err, "could not start language host RPC server"))
}

// Otherwise, print out the port so that the spawner knows how to reach us.
fmt.Printf("%d\n", handle.Port)

// And finally wait for the server to stop serving.
if err := <-handle.Done; err != nil {
cmdutil.Exit(errors.Wrapf(err, "language host RPC stopped serving"))
}
rc.Run(func(srv *grpc.Server) error {
host := newLanguageHost(dotnetExec, rc.EngineAddress, rc.Tracing, binary)
pulumirpc.RegisterLanguageRuntimeServer(srv, host)
return nil
}, func() {})
}

// dotnetLanguageHost implements the LanguageRuntimeServer interface
Expand Down