forked from atticlab/wormhole
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsupervisor_support.go
46 lines (40 loc) · 1.29 KB
/
supervisor_support.go
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
package supervisor
// Supporting infrastructure to allow running some non-Go payloads under supervision.
import (
"context"
"net"
"os/exec"
"google.golang.org/grpc"
)
// GRPCServer creates a Runnable that serves gRPC requests as longs as it's not canceled.
// If graceful is set to true, the server will be gracefully stopped instead of plain stopped. This means all pending
// RPCs will finish, but also requires streaming gRPC handlers to check their context liveliness and exit accordingly.
// If the server code does not support this, `graceful` should be false and the server will be killed violently instead.
func GRPCServer(srv *grpc.Server, lis net.Listener, graceful bool) Runnable {
return func(ctx context.Context) error {
Signal(ctx, SignalHealthy)
errC := make(chan error)
go func() {
errC <- srv.Serve(lis)
}()
select {
case <-ctx.Done():
if graceful {
srv.GracefulStop()
} else {
srv.Stop()
}
return ctx.Err()
case err := <-errC:
return err
}
}
}
// Command will create a Runnable that starts a long-running command, whose exit is determined to be a failure.
func Command(name string, arg ...string) Runnable {
return func(ctx context.Context) error {
Signal(ctx, SignalHealthy)
cmd := exec.CommandContext(ctx, name, arg...)
return cmd.Run()
}
}