/
joule.go
274 lines (244 loc) · 6.6 KB
/
joule.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
package joule
import (
"context"
"fmt"
"github.com/google/uuid"
"golang.org/x/crypto/ssh/terminal"
"io"
"os"
"os/signal"
"runtime/pprof"
"sync"
"syscall"
"time"
)
type UnitOfWork interface {
GetId() string
SetInput(interface{})
GetOutput() interface{}
GetError() error
UiSecret(string) string
UiOut([]byte)
UiErr([]byte)
UiStrOut(string)
UiStrErr(string)
UiOutWriter() io.Writer
UiErrWriter() io.Writer
}
type usp struct { // worker request secret input
id string
prompt string
}
type sUOW struct {
id string
input interface{}
output interface{}
err error
uiSecretPrompt chan usp // worker request secret input
uiSecret chan string // runner provides secret input
uiInReq chan string // worker request read from os.stdin
uiIn chan []byte // runner provides os.stdin until EOF
uiOut chan []byte // worker writes to os.stdout
uiErr chan []byte // worker writes to os.stderr
cliIn chan interface{} // response to uiSecret/uiIn request
work func(context.Context, UnitOfWork, interface{}) (interface{}, error)
}
func (uow *sUOW) GetId() string { return uow.id }
func (uow *sUOW) SetInput(input interface{}) { uow.input = input }
func (uow *sUOW) GetOutput() interface{} { return uow.output }
func (uow *sUOW) GetError() error { return uow.err }
func (uow *sUOW) UiSecret(prompt string) string {
uow.uiSecretPrompt <- usp{id: uow.id, prompt: prompt}
return <-uow.uiSecret
}
func (uow *sUOW) UiOut(bs []byte) { uow.uiOut <- bs }
func (uow *sUOW) UiErr(bs []byte) { uow.uiErr <- bs }
func (uow *sUOW) UiStrOut(s string) { uow.uiOut <- []byte(s) }
func (uow *sUOW) UiStrErr(s string) { uow.uiErr <- []byte(s) }
func (uow *sUOW) UiOutWriter() io.Writer { return newC2w(uow.uiOut) }
func (uow *sUOW) UiErrWriter() io.Writer { return newC2w(uow.uiErr) }
type CLIRunner[OT any] struct {
Ctx *context.Context
mux sync.Mutex
isRunning bool
isStopping bool
workDelay time.Duration
cancel context.CancelFunc
workersWg sync.WaitGroup
finalizer func()
uiSecretPrompt chan usp // worker request secret input
uiSecret chan string // runner provides secret input
uiInReq chan string // worker request read from os.stdin
uiIn chan []byte // runner provides os.stdin until EOF
uiOut chan []byte // workers write to os.stdout
uiErr chan []byte // workers write to os.stderr
Opts OT
Args []string
stdout io.Writer
stderr io.Writer
startup func(cr *CLIRunner[OT]) error
shutdown func(cr *CLIRunner[OT]) error
uows []*sUOW
uowReg map[string]*sUOW
}
func NewCLIRunner[OT any](
opts OT, args []string,
stdin io.Reader, stdout io.Writer, stderr io.Writer,
startup func(cr *CLIRunner[OT]) error,
shutdown func(cr *CLIRunner[OT]) error,
) *CLIRunner[OT] {
cr := &CLIRunner[OT]{
Opts: opts, Args: args,
stdout: stdout, stderr: stderr,
startup: startup, shutdown: shutdown}
cr.finalizer = cr.initAndGetFinalizer()
return cr
}
func (cr *CLIRunner[OT]) SetWorkDelay(workDelay time.Duration) { cr.workDelay = workDelay }
func (cr *CLIRunner[OT]) AddUow(
id string,
work func(context.Context, UnitOfWork, interface{}) (interface{}, error),
) UnitOfWork {
if cr.isStopping {
return nil
}
cr.mux.Lock()
defer cr.mux.Unlock()
if id == "" {
id = uuid.New().String()
}
uow := sUOW{
id: id, work: work,
uiSecretPrompt: cr.uiSecretPrompt, uiSecret: cr.uiSecret,
uiInReq: cr.uiInReq, uiIn: cr.uiIn,
uiOut: cr.uiOut, uiErr: cr.uiErr,
}
cr.uows = append(cr.uows, &uow)
if cr.uowReg == nil {
cr.uowReg = map[string]*sUOW{}
}
cr.uowReg[id] = &uow
cr.workersWg.Add(1)
if cr.isRunning {
cr.controlWork(&uow)
}
return &uow
}
func (cr *CLIRunner[OT]) GetUow(id string) UnitOfWork { return cr.uowReg[id] }
func dumpStackIf() {
if os.Getenv("JOULE_DBG_STACK") == "" {
return
}
fmt.Fprintf(os.Stderr, "joule dumpStackIf\n")
pprof.Lookup("goroutine").WriteTo(os.Stderr, 1)
}
func (cr *CLIRunner[OT]) initAndGetFinalizer() func() {
var ctx context.Context
ctx, cr.cancel = context.WithCancel(context.Background())
cr.Ctx = &ctx
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
count := 0
for sig := range c {
count += 1
if count == 1 {
dumpStackIf()
fmt.Fprintf(os.Stderr, "signal %s received, preparing to exit\n", sig)
cr.cancel()
continue
}
if count == 2 {
fmt.Fprintf(os.Stderr, "signal %s received twice, send it again to force exit\n", sig)
continue
}
fmt.Fprintf(os.Stderr, "signal %s received 3 times, exiting now\n", sig)
os.Exit(1)
}
}()
cr.uiSecretPrompt = make(chan usp)
cr.uiSecret = make(chan string)
cr.uiOut = make(chan []byte)
cr.uiErr = make(chan []byte)
finalize := func() {
close(cr.uiSecretPrompt)
close(cr.uiOut)
close(cr.uiErr)
}
return finalize
}
func (cr *CLIRunner[OT]) controlWork(uow *sUOW) {
go func() {
if cr.workDelay != 0 {
time.Sleep(cr.workDelay)
}
uow.output, uow.err = uow.work(*cr.Ctx, uow, uow.input)
cr.workersWg.Done()
}()
}
func (cr *CLIRunner[OT]) handleSecretPrompt(uow UnitOfWork, prompt string) {
_, _ = os.Stderr.WriteString(prompt)
secret, err := terminal.ReadPassword(int(syscall.Stdin))
_ = err
fmt.Println()
cr.uiSecret <- string(secret)
}
func (cr *CLIRunner[OT]) handleInReq(uow UnitOfWork) {
for {
data := make([]byte, 8192)
n, _ := os.Stdin.Read(data)
if n > 0 {
cr.uiIn <- data[0:n]
}
}
}
func (cr *CLIRunner[OT]) controlUI(done chan interface{}) (completed chan interface{}) {
completed = make(chan interface{})
go func() {
defer close(completed)
for {
select {
case uiUsp := <-cr.uiSecretPrompt:
cr.handleSecretPrompt(cr.GetUow(uiUsp.id), uiUsp.prompt)
case id := <-cr.uiInReq:
cr.handleInReq(cr.GetUow(id))
case uiOut := <-cr.uiOut:
cr.stdout.Write([]byte(uiOut))
case uiErr := <-cr.uiErr:
cr.stderr.Write([]byte(uiErr))
case <-done:
return
}
}
}()
return
}
func (cr *CLIRunner[OT]) Run() error {
defer cr.finalizer()
if cr.startup != nil {
if err := cr.startup(cr); err != nil {
return err
}
}
cr.mux.Lock()
for _, uow := range cr.uows {
cr.controlWork(uow)
}
cr.mux.Unlock()
cr.isRunning = true
stopUi := make(chan interface{})
uiStopped := cr.controlUI(stopUi)
cr.workersWg.Wait()
cr.isStopping = true
stopUi <- nil
<-uiStopped
if cr.shutdown != nil {
if err := cr.shutdown(cr); err != nil {
return err
}
}
return nil
}
func (cr *CLIRunner[OT]) CancelFunc() context.CancelFunc {
return cr.cancel
}