-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrapper.go
276 lines (252 loc) · 7.52 KB
/
drapper.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
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
package dapr_wrapper
import (
"bufio"
"context"
"fmt"
dapr "github.com/dapr/go-sdk/client"
"github.com/dapr/go-sdk/service/common"
daprd "github.com/dapr/go-sdk/service/http"
"net/http"
"os"
"os/exec"
"strings"
"sync"
"syscall"
"time"
)
// daprInit initializes Dapr
func daprInit() { //nolint:deadcode,unused
cmd := exec.Command("dapr", "init")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Printf("Error initializing Dapr: %v\n", err)
return
}
}
func daprUninstall() {
cmd := exec.Command("dapr", "uninstall", "--all")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Printf("Error uninstalling Dapr: %v\n", err)
return
}
}
func EnsureDapr() {
cmd := exec.Command("dapr", "version")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Printf("Dapr is not installed. Please install dapr CLI first: %v\n", err)
}
daprUninstall()
daprInit()
}
func startDaprSidecar(ctx context.Context, appID string, appPort int, daprPort int, daprGrpcPort int, componentsPath string, done chan int) {
cmd := exec.CommandContext(ctx, "dapr",
"run",
"--app-id", appID,
"--app-port", fmt.Sprintf("%d", appPort),
"--dapr-http-port", fmt.Sprintf("%d", daprPort),
"--dapr-grpc-port", fmt.Sprintf("%d", daprGrpcPort),
"--components-path", componentsPath,
)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true, // Ensure the command runs in its own process group
}
//cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Printf("Error getting stdout pipe: %v\n", err)
return
}
err = cmd.Start()
if err != nil {
fmt.Printf("Error starting Dapr sidecar: %v\n", err)
done <- -1
return
} else {
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
line := scanner.Text()
fmt.Printf("%s\n", line)
// Check if the line contains the specific log message
if strings.Contains(line, "You're up and running") {
fmt.Println("Dapr sidecar started successfully")
done <- cmd.Process.Pid
}
}
}
// Wait for the command to finish
err = cmd.Wait()
if err != nil {
fmt.Printf("Dapr sidecar stopped with error: %v %v\n", err, cmd.Stderr)
}
}
func RegisterEventHandler(ctx context.Context, appID string, pubsubName string, topic string, route string, resourcePath string, handler func(ctx context.Context, e *common.TopicEvent) (retry bool, err error)) (cancel func(), err error) {
s := daprd.NewService(":8886") //TODO get random free port
// Create a mock subscription handler
err = s.AddTopicEventHandler(&common.Subscription{
PubsubName: pubsubName,
Topic: topic,
Route: route,
}, handler)
if err != nil {
return
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
fmt.Printf("Starting consumer service\n")
if err := s.Start(); err != nil && err != http.ErrServerClosed {
fmt.Printf("error: %v", err)
return
}
fmt.Println("Consumer service stopped")
}()
daprStarted := make(chan int)
go startDaprSidecar(ctx, appID, 8886, 3500, 50001, resourcePath, daprStarted)
var sidecarpid int = -1
if x := <-daprStarted; x > 0 {
fmt.Println("Dapr sidecar started successfully", x)
sidecarpid = x
} else {
return nil, fmt.Errorf("Error starting Dapr sidecar")
}
return func() {
err := s.GracefulStop()
if err != nil {
fmt.Printf("Error stopping server: %v\n", err)
}
fmt.Println("Stopping ....")
if sidecarpid > 0 {
err := syscall.Kill(-sidecarpid, syscall.SIGTERM)
if err != nil {
fmt.Printf("Error stopping Dapr sidecar proccess: %d - %v\n", sidecarpid, err)
}
}
}, nil
}
type TopicEventHandler struct {
Subscription *common.Subscription
Handler func(ctx context.Context, e *common.TopicEvent) (retry bool, err error)
}
type DaprService struct {
AppID string
AppPort int
DaprHTTPPort int
DaprGRPCPort int
Command *exec.Cmd
ServiceInvocationHandlers map[string]common.ServiceInvocationHandler
TopicEventHandlers []TopicEventHandler
Service common.Service
Wg sync.WaitGroup
ResourcePath string
}
func (s *DaprService) Start() (dapr.Client, error) {
errChan := make(chan error, 1)
service := daprd.NewService(fmt.Sprintf(":%d", s.AppPort))
s.Service = service
for handle, serviceHandler := range s.ServiceInvocationHandlers {
if err := service.AddServiceInvocationHandler(handle, serviceHandler); err != nil {
errChan <- fmt.Errorf("failed to add handler %s: %v", s.AppID, err)
}
}
for _, topicHandler := range s.TopicEventHandlers {
if err := service.AddTopicEventHandler(topicHandler.Subscription, topicHandler.Handler); err != nil {
errChan <- fmt.Errorf("failed to add topic handler %s: %v", s.AppID, err)
}
}
s.Wg.Add(1)
go func(s *DaprService, daprService common.Service) {
defer s.Wg.Done()
fmt.Printf("Starting service worker...\n")
if err := daprService.Start(); err != nil {
errChan <- fmt.Errorf("failed to start service %s: %v", s.AppID, err)
}
}(s, service)
select {
case e := <-errChan:
return nil, fmt.Errorf("failed to start Dapr service %s, %v", s.AppID, e)
case <-time.After(2 * time.Second): // Adjust timeout as necessary
fmt.Printf("OK") // things should start within 2 sec - this is brittle but works for now
}
extraArgs := []string{
"run",
"--app-id", s.AppID,
"--app-port", fmt.Sprintf("%d", s.AppPort),
"--dapr-http-port", fmt.Sprintf("%d", s.DaprHTTPPort),
"--dapr-grpc-port", fmt.Sprintf("%d", s.DaprGRPCPort),
}
if s.ResourcePath != "" {
extraArgs = append(extraArgs, "--resources-path", s.ResourcePath)
}
s.Command = exec.Command("dapr",
extraArgs...,
)
s.Command.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true, // Ensure the command runs in its own process group
}
//cmd.Stdout = os.Stdout
s.Command.Stderr = os.Stderr
stdout, err := s.Command.StdoutPipe()
if err != nil {
fmt.Printf("Error getting stdout pipe: %v\n", err)
return nil, err
}
err = s.Command.Start()
if err != nil {
fmt.Printf("Error starting Dapr sidecar: %v\n", err)
return nil, err
} else {
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
line := scanner.Text()
//fmt.Printf("%s\n", line)
// Check if the line contains the specific log message
if strings.Contains(line, "You're up and running") {
fmt.Println("Dapr sidecar started successfully")
break
}
}
}
fmt.Printf("Waiting for proc")
serrChan := make(chan error, 1)
s.Wg.Add(1)
go func() {
defer s.Wg.Done()
err = s.Command.Wait()
if err != nil {
fmt.Printf("Dapr sidecar stopped with error: %v %v\n", err, s.Command.Stderr)
serrChan <- err
}
}()
fmt.Printf("Process started\n")
select {
case e := <-serrChan:
return nil, fmt.Errorf("failed to start Dapr service %s, %v\n", s.AppID, e)
case <-time.After(2 * time.Second): // Adjust timeout as necessary
fmt.Printf("OK waiting for proc to terminate\n") // things should start within 2 sec - this is brittle but works for now
}
daprClient, err := dapr.NewClientWithPort(fmt.Sprintf("%d", s.DaprGRPCPort))
if err != nil {
stopError := s.Stop()
return nil, fmt.Errorf("failed to create Dapr client: %v\nStop error: %v", err, stopError)
}
return daprClient, nil
}
func (s *DaprService) Stop() error {
if s.Command != nil && s.Command.Process != nil {
err := syscall.Kill(-s.Command.Process.Pid, syscall.SIGTERM)
if err != nil {
fmt.Printf("Error stopping Dapr sidecar proccess: %d - %v\n", s.Command.Process.Pid, err)
}
}
return nil
}