-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added callback context in action execution result #5
Conversation
702ee06
to
89029d6
Compare
runbook/executor.go
Outdated
} | ||
|
||
return nil | ||
waitGroup.Done() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not need a wait group here since running the script is a synchronous operation
"net" | ||
) | ||
|
||
var CallbackContextReaderFunc = CallbackContextReaderForWindows |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can define a CallbackContextHandler interface and implement it for linux and windows. then you can initialize the correct struct once in the runbook executor
runbook/executor.go
Outdated
var waitGroup sync.WaitGroup | ||
|
||
var pipePath string | ||
if runtime.GOOS == "windows" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this logic should be in CallbackContextHandler
runbook/executor.go
Outdated
@@ -45,7 +65,7 @@ func Execute(executablePath string, args, environmentVars []string, stdout, stde | |||
cmd = exec.Command(executablePath, args...) | |||
} | |||
|
|||
cmd.Env = append(os.Environ(), environmentVars...) | |||
cmd.Env = append(append(os.Environ(), fmt.Sprintf("JEC_CALLBACK_PIPE_PATH=%s", pipePath)), environmentVars...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
won't JEC_CALLBACK_PIPE_PATH be overriden by each worker concurrently? we have multiple workers running at the same time. better to add it into args
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
os.Environ() returns a copy of environment variables, so it will not be overwritten. Though I will add it in args
runbook/executor.go
Outdated
pipe := CreatePipeFunc(pipePath) | ||
|
||
waitGroup.Add(1) | ||
go CallbackContextReaderFunc(callbackContextBuffer, pipe) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if we need a separate go routine here. is it a significant performance improver?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually its not for performance improvement. when we open the named pipe for reading (or for windows when we call listener.Accept()) the process will stop there until any other process opens the other end of pipe for writing the data.
So with this go routine we will parallelly start waiting for pipe write and the main routine will continue to execute the script.
Before the script execution started we have already created the pipe and waiting for data to be written. Now inside script execution we can open the pipe and write data to it.
If the script has written some data then the go routine will terminate after reading it. But if the script has not opened the pipe and finished execution then we can call waitGroup.Done() to stop that go rountine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to listen to the pipe in parallel? do we have an option like open file -> execute -> read all -> close conn?
queue/message_test.go
Outdated
@@ -63,8 +63,8 @@ var mockActionLoggers = map[string]io.Writer{ | |||
"/path/to/stderr": mockStderr, | |||
} | |||
|
|||
func mockExecute(executablePath string, args, environmentVars []string, stdout, stderr io.Writer) error { | |||
return nil | |||
func mockExecute(messageId string, executablePath string, args, environmentVars []string, stdout, stderr io.Writer) (string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
messageId is not a meaningful naming in the execution context. it might be executionId instead.
go.mod
Outdated
github.com/pkg/errors v0.8.1 | ||
github.com/prometheus/client_golang v1.1.0 | ||
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 // indirect | ||
github.com/prometheus/procfs v0.0.4 // indirect | ||
github.com/sirupsen/logrus v1.4.2 | ||
github.com/sirupsen/logrus v1.2.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it downgraded?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems like it got downgraded by winio package. upgrading it back to 1.4.2
queue/message.go
Outdated
@@ -102,18 +104,18 @@ func (mh *messageHandler) Handle(message sqs.Message) (*runbook.ActionResultPayl | |||
return result, nil | |||
} | |||
|
|||
func (mh *messageHandler) execute(mappedAction *conf.MappedAction, messageBody string) (string, error) { | |||
func (mh *messageHandler) execute(mappedAction *conf.MappedAction, message sqs.Message) (string, string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use a pointer for sqs message not to copy the value. also it is an already private method
69aab9d
to
50599a0
Compare
github.com/google/uuid v1.1.1 | ||
github.com/kardianos/service v1.0.0 | ||
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are the indirect ones removed? also, I guess go-windows-terminal-sequences is downgraded
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these were unused packages which got removed after running go mod tidy
runbook/callback_context_reader.go
Outdated
@@ -0,0 +1,7 @@ | |||
package runbook | |||
|
|||
type CallbackContextReader interface { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not just "reader". maybe "handler" or other generic wording could be good
data, err := ioutil.ReadAll(file) | ||
_ = copy(callbackContextReader.callbackContextBuffer, data) | ||
if err != nil { | ||
fmt.Println("Error reading from the named pipe:", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use logrus instead of fmt
runbook/executor.go
Outdated
|
||
var waitGroup sync.WaitGroup | ||
|
||
waitGroup.Add(1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no one is waiting for this waitGroup. what is the purpose?
4cb9c4c
to
f02510c
Compare
} | ||
|
||
return nil | ||
callbackContext := bytes.NewBuffer(bytes.Trim(callbackContextHandler.callbackContextBuffer, "\x00")).String() | ||
logrus.Debug("Callback context: " + callbackContext) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be removed
@@ -111,7 +113,7 @@ github.com/jmespath/go-jmespath | |||
github.com/kardianos/service | |||
# github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd | |||
github.com/kevinburke/ssh_config | |||
# github.com/konsorten/go-windows-terminal-sequences v1.0.2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is the version downgraded?
No description provided.