/
process.go
126 lines (112 loc) · 4.51 KB
/
process.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
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
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package expansionx
import (
"context"
"fmt"
"net"
"os/exec"
"time"
"google.golang.org/grpc"
)
// ExpansionServiceRunner is a type that holds information required to
// start up a Beam Expansion Service JAR and maintain a handle on the
// process running the service to enable shutdown as well.
type ExpansionServiceRunner struct {
execPath string
servicePort string
serviceCommand *exec.Cmd
}
func findOpenPort() (int, error) {
listener, err := net.Listen("tcp", ":0")
if err != nil {
return 0, err
}
defer listener.Close()
return listener.Addr().(*net.TCPAddr).Port, nil
}
// NewExpansionServiceRunner builds an ExpansionServiceRunner struct for a given gradle target and
// Beam version and returns a pointer to it. Passing an empty string as servicePort will request an
// open port to be assigned to the service.
func NewExpansionServiceRunner(jarPath, servicePort string) (*ExpansionServiceRunner, error) {
if servicePort == "" {
port, err := findOpenPort()
if err != nil {
return nil, fmt.Errorf("failed to find open port for service, got %v", err)
}
servicePort = fmt.Sprintf("%d", port)
}
serviceCommand := exec.Command("java", "-jar", jarPath, servicePort)
return &ExpansionServiceRunner{execPath: jarPath, servicePort: servicePort, serviceCommand: serviceCommand}, nil
}
// NewPyExpansionServiceRunner builds an ExpansionServiceRunner struct for a given python module and
// Beam version and returns a pointer to it. Passing an empty string as servicePort will request an
// open port to be assigned to the service.
func NewPyExpansionServiceRunner(pythonExec, module, servicePort string) (*ExpansionServiceRunner, error) {
if servicePort == "" {
port, err := findOpenPort()
if err != nil {
return nil, fmt.Errorf("failed to find open port for service, got %v", err)
}
servicePort = fmt.Sprintf("%d", port)
}
serviceCommand := exec.Command(pythonExec, "-m", module, "-p", servicePort, "--fully_qualified_name_glob=*")
return &ExpansionServiceRunner{execPath: pythonExec, servicePort: servicePort, serviceCommand: serviceCommand}, nil
}
func (e *ExpansionServiceRunner) String() string {
return fmt.Sprintf("Exec Path: %v, Port: %v, Process: %v", e.execPath, e.servicePort, e.serviceCommand.Process)
}
// Endpoint returns the formatted endpoint the ExpansionServiceRunner is set to start the expansion
// service on.
func (e *ExpansionServiceRunner) Endpoint() string {
return "localhost:" + e.servicePort
}
func (e *ExpansionServiceRunner) pingEndpoint(timeout time.Duration) error {
ctx, canFunc := context.WithTimeout(context.Background(), timeout)
defer canFunc()
conn, err := grpc.DialContext(ctx, e.Endpoint(), grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
return err
}
conn.Close()
return nil
}
const connectionTimeout = 15 * time.Second
// StartService starts the expansion service for a given ExpansionServiceRunner. If this is
// called and does not return an error, the expansion service will be running in the background
// until StopService is called. This will leak resources if not addressed.
func (e *ExpansionServiceRunner) StartService() error {
err := e.serviceCommand.Start()
if err != nil {
return err
}
err = e.pingEndpoint(connectionTimeout)
if err != nil {
return err
}
return nil
}
// StopService stops the expansion service for a given ExpansionServiceRunner. Returns an error
// if the command hasn't been run or if the process has already exited.
func (e *ExpansionServiceRunner) StopService() error {
expansionProcess := e.serviceCommand.Process
if expansionProcess == nil {
return fmt.Errorf("process does not exist for runner %v", e)
}
if e.serviceCommand.ProcessState != nil {
return fmt.Errorf("process has already completed, state: %v", e)
}
return expansionProcess.Kill()
}