/
subprocess.go
257 lines (219 loc) · 6.85 KB
/
subprocess.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
package input
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"os/exec"
"sync"
"time"
"github.com/Jeffail/benthos/v3/internal/docs"
"github.com/Jeffail/benthos/v3/lib/input/reader"
"github.com/Jeffail/benthos/v3/lib/log"
"github.com/Jeffail/benthos/v3/lib/message"
"github.com/Jeffail/benthos/v3/lib/metrics"
"github.com/Jeffail/benthos/v3/lib/types"
)
//------------------------------------------------------------------------------
func init() {
Constructors[TypeSubprocess] = TypeSpec{
constructor: fromSimpleConstructor(func(conf Config, mgr types.Manager, log log.Modular, stats metrics.Type) (Type, error) {
b, err := newSubprocess(conf.Subprocess)
if err != nil {
return nil, err
}
return NewAsyncReader(TypeSubprocess, true, b, log, stats)
}),
Status: docs.StatusBeta,
Summary: `
Executes a command, runs it as a subprocess, and consumes messages from it over stdout.`,
Description: `
Messages are consumed according to a specified codec. The command is executed once and if it terminates the input also closes down gracefully. Alternatively, the field ` + "`restart_on_close` can be set to `true`" + ` in order to have Benthos re-execute the command each time it stops.
The field ` + "`max_buffer`" + ` defines the maximum message size able to be read from the subprocess. This value should be set significantly above the real expected maximum message size.
The execution environment of the subprocess is the same as the Benthos instance, including environment variables and the current working directory.`,
FieldSpecs: docs.FieldSpecs{
docs.FieldCommon("name", "The command to execute as a subprocess.", "cat", "sed", "awk"),
docs.FieldString("args", "A list of arguments to provide the command.").Array(),
docs.FieldCommon(
"codec", "The way in which messages should be consumed from the subprocess.",
).HasOptions("lines"),
docs.FieldCommon("restart_on_exit", "Whether the command should be re-executed each time the subprocess ends."),
docs.FieldAdvanced("max_buffer", "The maximum expected size of an individual message."),
},
Categories: []Category{
CategoryUtility,
},
}
}
//------------------------------------------------------------------------------
type subprocScanner interface {
Bytes() []byte
Text() string
Err() error
Scan() bool
}
func linesSubprocCodec(conf SubprocessConfig, stdout, stderr io.Reader) (outScanner, errScanner subprocScanner) {
outScanner = bufio.NewScanner(stdout)
errScanner = bufio.NewScanner(stderr)
if conf.MaxBuffer != bufio.MaxScanTokenSize {
outScanner.(*bufio.Scanner).Buffer([]byte{}, conf.MaxBuffer)
errScanner.(*bufio.Scanner).Buffer([]byte{}, conf.MaxBuffer)
}
return outScanner, errScanner
}
type subprocCodec func(SubprocessConfig, io.Reader, io.Reader) (subprocScanner, subprocScanner)
func codecFromStr(codec string) (subprocCodec, error) {
// TODO: Flesh this out with more options based on s.conf.Codec.
if codec == "lines" {
return linesSubprocCodec, nil
}
return nil, fmt.Errorf("codec not recognised: %v", codec)
}
//------------------------------------------------------------------------------
// SubprocessConfig contains configuration for the Subprocess input type.
type SubprocessConfig struct {
Name string `json:"name" yaml:"name"`
Args []string `json:"args" yaml:"args"`
Codec string `json:"codec" yaml:"codec"`
RestartOnExit bool `json:"restart_on_exit" yaml:"restart_on_exit"`
MaxBuffer int `json:"max_buffer" yaml:"max_buffer"`
}
// NewSubprocessConfig creates a new SubprocessConfig with default values.
func NewSubprocessConfig() SubprocessConfig {
return SubprocessConfig{
Name: "",
Args: []string{},
Codec: "lines",
RestartOnExit: false,
MaxBuffer: bufio.MaxScanTokenSize,
}
}
// Subprocess executes a bloblang mapping with an empty context each time this
// input is read from. An interval period must be specified that determines how
// often a message is generated.
type Subprocess struct {
conf SubprocessConfig
codec subprocCodec
msgChan chan []byte
errChan chan error
close func()
ctx context.Context
}
// newSubprocess creates a new bloblang input reader type.
func newSubprocess(conf SubprocessConfig) (*Subprocess, error) {
s := &Subprocess{
conf: conf,
}
s.ctx, s.close = context.WithCancel(context.Background())
var err error
if s.codec, err = codecFromStr(s.conf.Codec); err != nil {
return nil, err
}
return s, nil
}
// ConnectWithContext establishes a Subprocess reader.
func (s *Subprocess) ConnectWithContext(ctx context.Context) error {
if s.msgChan != nil {
return nil
}
cmd := exec.CommandContext(s.ctx, s.conf.Name, s.conf.Args...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
msgChan := make(chan []byte)
errChan := make(chan error)
outScanner, errScanner := s.codec(s.conf, stdout, stderr)
go func() {
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
for outScanner.Scan() {
data := outScanner.Bytes()
dataCopy := make([]byte, len(data))
copy(dataCopy, data)
select {
case msgChan <- dataCopy:
case <-s.ctx.Done():
}
}
if err := outScanner.Err(); err != nil {
select {
case errChan <- err:
case <-s.ctx.Done():
}
}
}()
go func() {
defer wg.Done()
for errScanner.Scan() {
select {
case errChan <- errors.New(errScanner.Text()):
case <-s.ctx.Done():
}
}
if err := errScanner.Err(); err != nil {
select {
case errChan <- err:
case <-s.ctx.Done():
}
}
}()
wg.Wait()
close(msgChan)
close(errChan)
}()
s.msgChan = msgChan
s.errChan = errChan
return nil
}
// ReadWithContext a new bloblang generated message.
func (s *Subprocess) ReadWithContext(ctx context.Context) (types.Message, reader.AsyncAckFn, error) {
msgChan, errChan := s.msgChan, s.errChan
if msgChan == nil {
return nil, nil, types.ErrNotConnected
}
select {
case b, open := <-msgChan:
if !open {
if s.conf.RestartOnExit {
s.msgChan = nil
s.errChan = nil
return nil, nil, types.ErrNotConnected
}
return nil, nil, types.ErrTypeClosed
}
msg := message.New(nil)
msg.Append(message.NewPart(b))
return msg, func(context.Context, types.Response) error { return nil }, nil
case err, open := <-errChan:
if !open {
if s.conf.RestartOnExit {
s.msgChan = nil
s.errChan = nil
return nil, nil, types.ErrNotConnected
}
return nil, nil, types.ErrTypeClosed
}
return nil, nil, err
case <-ctx.Done():
}
return nil, nil, types.ErrTimeout
}
// CloseAsync shuts down the bloblang reader.
func (s *Subprocess) CloseAsync() {
s.close()
}
// WaitForClose blocks until the bloblang input has closed down.
func (s *Subprocess) WaitForClose(timeout time.Duration) error {
return nil
}