Skip to content

Commit 4b8e042

Browse files
committed
Added callback context in action execution result
1 parent e37b44a commit 4b8e042

10 files changed

+164
-48
lines changed

go.mod

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,17 @@ replace (
99

1010
require (
1111
github.com/BurntSushi/toml v0.3.1 // indirect
12+
github.com/Microsoft/go-winio v0.4.12
1213
github.com/aws/aws-sdk-go v1.23.20
1314
github.com/go-git/go-git/v5 v5.2.0
14-
github.com/gogo/protobuf v1.3.2 // indirect
1515
github.com/google/uuid v1.1.1
1616
github.com/kardianos/service v1.0.0
17-
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
1817
github.com/pkg/errors v0.8.1
1918
github.com/prometheus/client_golang v1.1.0
2019
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 // indirect
2120
github.com/prometheus/procfs v0.0.4 // indirect
2221
github.com/sirupsen/logrus v1.4.2
2322
github.com/stretchr/testify v1.4.0
24-
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect
2523
gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531160350-a96e63847dc3
2624
gopkg.in/yaml.v2 v2.4.0
2725
)

go.sum

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
22
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3+
github.com/Microsoft/go-winio v0.4.12 h1:xAfWHN1IrQ0NJ9TBC0KBZoqLjzDTr1ML+4MywiUOryc=
4+
github.com/Microsoft/go-winio v0.4.12/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
35
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs=
46
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
57
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
@@ -36,7 +38,6 @@ github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2
3638
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
3739
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
3840
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
39-
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
4041
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
4142
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
4243
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -63,14 +64,11 @@ github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd h1:Coekwdh0v
6364
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
6465
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
6566
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
67+
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
6668
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
67-
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
68-
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
6969
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
70-
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
7170
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
7271
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
73-
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
7472
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
7573
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
7674
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
@@ -125,7 +123,6 @@ golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnf
125123
golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
126124
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
127125
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
128-
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 h1:xMPOj6Pz6UipU1wXLkrtqpHbR0AVFnyPEQq/wRWz9lM=
129126
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
130127
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
131128
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
@@ -136,11 +133,9 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
136133
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
137134
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
138135
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
139-
golang.org/x/net v0.0.0-20200301022130-244492dfa37a h1:GuSPYbZzB5/dcLNCwLQLsg3obCJtX9IJhpXkvY7kzk0=
140136
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
137+
golang.org/x/net v0.0.0-20201021035429-f5854403a974 h1:IX6qOQeG5uLjB/hjjwjedwfjND0hgjPMMyO1RoIXQNI=
141138
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
142-
golang.org/x/net v0.0.0-20201224014010-6772e930b67b h1:iFwSg7t5GZmB/Q5TjiEAsdoLDrdJRC1RiF2WhuV29Qw=
143-
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
144139
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
145140
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
146141
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -154,12 +149,9 @@ golang.org/x/sys v0.0.0-20190221075227-b4e8571b14e0/go.mod h1:STP8DvDyc/dI5b8T5h
154149
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
155150
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
156151
golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
157-
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So=
158152
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
153+
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA=
159154
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
160-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
161-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
162-
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
163155
golang.org/x/text v0.3.4 h1:0YWbFKbhXG/wIiuHDSKpS0Iy7FSA+u45VtBMfQcFTTc=
164156
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
165157
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
@@ -180,7 +172,6 @@ gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531160350-a96e63847dc3/go.mod h1:l0
180172
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
181173
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
182174
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
183-
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
184175
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
185176
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
186177
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=

queue/message.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ func (mh *messageHandler) Handle(message sqs.Message) (*runbook.ActionResultPayl
7171
}
7272

7373
start := time.Now()
74-
executionResult, err := mh.execute(&mappedAction, *message.Body)
74+
executionResult, callbackContext, err := mh.execute(&mappedAction, &message)
7575
took := time.Since(start)
7676

77+
result.CallbackContext = callbackContext
78+
7779
switch err := err.(type) {
7880
case *runbook.ExecError:
7981
result.IsSuccessful = false
@@ -102,18 +104,18 @@ func (mh *messageHandler) Handle(message sqs.Message) (*runbook.ActionResultPayl
102104
return result, nil
103105
}
104106

105-
func (mh *messageHandler) execute(mappedAction *conf.MappedAction, messageBody string) (string, error) {
107+
func (mh *messageHandler) execute(mappedAction *conf.MappedAction, message *sqs.Message) (string, string, error) {
106108

107109
sourceType := mappedAction.SourceType
108110
switch sourceType {
109111
case conf.GitSourceType:
110112
if mh.repositories == nil {
111-
return "", errors.New("Repositories should be provided.")
113+
return "", "", errors.New("Repositories should be provided.")
112114
}
113115

114116
repository, err := mh.repositories.Get(mappedAction.GitOptions.Url)
115117
if err != nil {
116-
return "", err
118+
return "", "", err
117119
}
118120

119121
repository.RLock()
@@ -122,7 +124,7 @@ func (mh *messageHandler) execute(mappedAction *conf.MappedAction, messageBody s
122124

123125
case conf.LocalSourceType:
124126
args := append(mh.actionSpecs.GlobalFlags.Args(), mappedAction.Flags.Args()...)
125-
args = append(args, []string{"-payload", messageBody}...)
127+
args = append(args, []string{"-payload", *message.Body}...)
126128
args = append(args, mh.actionSpecs.GlobalArgs...)
127129
args = append(args, mappedAction.Args...)
128130
env := append(mh.actionSpecs.GlobalEnv, mappedAction.Env...)
@@ -138,9 +140,9 @@ func (mh *messageHandler) execute(mappedAction *conf.MappedAction, messageBody s
138140
}
139141
stderr := mh.actionLoggers[mappedAction.Stderr]
140142

141-
err := runbook.ExecuteFunc(mappedAction.Filepath, args, env, stdout, stderr)
142-
return stdoutBuff.String(), err
143+
callbackContext, err := runbook.ExecuteFunc(*message.MessageId, mappedAction.Filepath, args, env, stdout, stderr)
144+
return stdoutBuff.String(), callbackContext, err
143145
default:
144-
return "", errors.Errorf("Unknown action sourceType[%s].", sourceType)
146+
return "", "", errors.Errorf("Unknown action sourceType[%s].", sourceType)
145147
}
146148
}

queue/message_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ var mockActionLoggers = map[string]io.Writer{
6363
"/path/to/stderr": mockStderr,
6464
}
6565

66-
func mockExecute(executablePath string, args, environmentVars []string, stdout, stderr io.Writer) error {
67-
return nil
66+
func mockExecute(executionId string, executablePath string, args, environmentVars []string, stdout, stderr io.Writer) (string, error) {
67+
return "", nil
6868
}
6969

7070
func TestProcess(t *testing.T) {
@@ -85,10 +85,10 @@ func testProcessSuccessfully(t *testing.T) {
8585
message := sqs.Message{Body: &body, MessageId: &id}
8686
queueMessage := NewMessageHandler(nil, mockActionSpecs, mockActionLoggers)
8787

88-
runbook.ExecuteFunc = func(executablePath string, args, environmentVars []string, stdout, stderr io.Writer) error {
88+
runbook.ExecuteFunc = func(executionId string, executablePath string, args, environmentVars []string, stdout, stderr io.Writer) (string, error) {
8989
assert.Equal(t, mockStdout, stdout)
9090
assert.Equal(t, mockStderr, stderr)
91-
return nil
91+
return "", nil
9292
}
9393

9494
result, err := queueMessage.Handle(message)
@@ -99,9 +99,9 @@ func testProcessSuccessfully(t *testing.T) {
9999
}
100100

101101
func testProcessHttpActionSuccessfully(t *testing.T) {
102-
runbook.ExecuteFunc = func(executablePath string, args, environmentVars []string, stdout, stderr io.Writer) error {
102+
runbook.ExecuteFunc = func(executionId string, executablePath string, args, environmentVars []string, stdout, stderr io.Writer) (string, error) {
103103
io.Copy(stdout, bytes.NewBufferString(`{"headers": {"Date": "Wed, 14 Oct 2020 08:59:30 GMT"},"body": "done", "statusCode": 200}`))
104-
return nil
104+
return "", nil
105105
}
106106

107107
body := `{"actionType":"http", "action":"Retrieve", "requestId": "RequestId"}`

runbook/callback_context_reader.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package runbook
2+
3+
type CallbackContextReader interface {
4+
CreatePipe()
5+
Read()
6+
RemovePipe()
7+
}
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
package runbook
5+
6+
import (
7+
"fmt"
8+
"github.com/sirupsen/logrus"
9+
"io/ioutil"
10+
"os"
11+
"syscall"
12+
)
13+
14+
type CallbackContextReaderUnix struct {
15+
pipePath string
16+
callbackContextBuffer []byte
17+
}
18+
19+
func NewCallbackContextReader(executionId string) *CallbackContextReaderUnix {
20+
return &CallbackContextReaderUnix{
21+
pipePath: `jecCallbackPipe-` + executionId,
22+
callbackContextBuffer: make([]byte, 4096),
23+
}
24+
}
25+
26+
func (callbackContextReader *CallbackContextReaderUnix) CreatePipe() {
27+
err := syscall.Mkfifo(callbackContextReader.pipePath, 0666)
28+
if err != nil {
29+
logrus.Debugf("Could not create named pipe with name %s. Error: %s", callbackContextReader.pipePath, err.Error())
30+
}
31+
}
32+
33+
func (callbackContextReader *CallbackContextReaderUnix) Read() {
34+
file, err := os.OpenFile(callbackContextReader.pipePath, os.O_RDONLY, os.ModeNamedPipe)
35+
if err != nil {
36+
logrus.Debugf("Could not open named pipe. Error: %s", err.Error())
37+
}
38+
defer file.Close()
39+
40+
data, err := ioutil.ReadAll(file)
41+
_ = copy(callbackContextReader.callbackContextBuffer, data)
42+
if err != nil {
43+
fmt.Println("Error reading from the named pipe:", err)
44+
}
45+
}
46+
47+
func (callbackContextReader *CallbackContextReaderUnix) RemovePipe() {
48+
err := os.Remove(callbackContextReader.pipePath)
49+
if err != nil {
50+
logrus.Debugf("Could not delete named pipe %s", callbackContextReader.pipePath)
51+
}
52+
}
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//go:build windows
2+
// +build windows
3+
4+
package runbook
5+
6+
import (
7+
winio "github.com/Microsoft/go-winio"
8+
"github.com/sirupsen/logrus"
9+
"net"
10+
)
11+
12+
type CallbackContextReaderWindows struct {
13+
pipePath string
14+
pipeListener net.Listener
15+
callbackContextBuffer []byte
16+
}
17+
18+
func NewCallbackContextReader(executionId string) *CallbackContextReaderWindows {
19+
return &CallbackContextReaderWindows{
20+
pipePath: `\\.\pipe\jecCallbackPipe-` + executionId,
21+
callbackContextBuffer: make([]byte, 4096),
22+
}
23+
}
24+
25+
func (callbackContextReader *CallbackContextReaderWindows) CreatePipe() {
26+
listener, err := winio.ListenPipe(callbackContextReader.pipePath, nil)
27+
callbackContextReader.pipeListener = listener
28+
if err != nil {
29+
logrus.Debugf("Could not create named pipe: %s Error: %s", callbackContextReader.pipePath, err.Error())
30+
}
31+
}
32+
33+
func (callbackContextReader *CallbackContextReaderWindows) Read() {
34+
pipe, err := callbackContextReader.pipeListener.Accept()
35+
if err != nil {
36+
logrus.Debugf("Could not accept connection from pipe. Error: %s", err.Error())
37+
return
38+
}
39+
defer pipe.Close()
40+
41+
_, err = pipe.Read(callbackContextReader.callbackContextBuffer)
42+
if err != nil {
43+
logrus.Debugf("Could not read data from pipe. Error: %s", err.Error())
44+
}
45+
}
46+
47+
func (callbackContextReader *CallbackContextReaderWindows) RemovePipe() {
48+
callbackContextReader.pipeListener.Close()
49+
}

runbook/executor.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os/exec"
88
"path/filepath"
99
"strings"
10+
"sync"
1011
)
1112

1213
var ExecuteFunc = Execute
@@ -26,14 +27,24 @@ type ExecError struct {
2627
error
2728
}
2829

29-
func Execute(executablePath string, args, environmentVars []string, stdout, stderr io.Writer) error {
30+
func Execute(executionId string, executablePath string, args, environmentVars []string, stdout, stderr io.Writer) (string, error) {
31+
32+
callbackContextReader := NewCallbackContextReader(executionId)
33+
callbackContextReader.CreatePipe()
34+
35+
var waitGroup sync.WaitGroup
36+
37+
waitGroup.Add(1)
38+
go callbackContextReader.Read()
3039

3140
if args == nil {
3241
args = []string{}
3342
} else if environmentVars == nil {
3443
environmentVars = []string{}
3544
}
3645

46+
args = append(args, []string{"--jecNamedPipe", callbackContextReader.pipePath}...)
47+
3748
var cmd *exec.Cmd
3849
fileExt := filepath.Ext(strings.ToLower(executablePath))
3950
command, exist := executables[fileExt]
@@ -58,8 +69,13 @@ func Execute(executablePath string, args, environmentVars []string, stdout, stde
5869

5970
err := cmd.Run()
6071
if err != nil {
61-
return &ExecError{stderrBuff.String(), err}
72+
return "", &ExecError{stderrBuff.String(), err}
6273
}
6374

64-
return nil
75+
waitGroup.Done()
76+
callbackContextReader.RemovePipe()
77+
78+
callbackContext := bytes.NewBuffer(bytes.Trim(callbackContextReader.callbackContextBuffer, "\x00")).String()
79+
80+
return callbackContext, nil
6581
}

0 commit comments

Comments
 (0)