Skip to content
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

Merged
merged 2 commits into from
May 7, 2024
Merged

Conversation

pagrawal768
Copy link
Collaborator

No description provided.

}

return nil
waitGroup.Done()
Copy link
Collaborator

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
Copy link
Collaborator

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

var waitGroup sync.WaitGroup

var pipePath string
if runtime.GOOS == "windows" {
Copy link
Collaborator

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

@@ -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...)
Copy link
Collaborator

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

Copy link
Collaborator Author

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

pipe := CreatePipeFunc(pipePath)

waitGroup.Add(1)
go CallbackContextReaderFunc(callbackContextBuffer, pipe)
Copy link
Collaborator

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?

Copy link
Collaborator Author

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.

Copy link
Collaborator

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?

@@ -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) {
Copy link
Collaborator

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it downgraded?

Copy link
Collaborator Author

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) {
Copy link
Collaborator

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

@pagrawal768 pagrawal768 force-pushed the BIF-1000 branch 3 times, most recently from 69aab9d to 50599a0 Compare March 12, 2024 06:11
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
Copy link
Collaborator

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

Copy link
Collaborator Author

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

@@ -0,0 +1,7 @@
package runbook

type CallbackContextReader interface {
Copy link
Collaborator

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)
Copy link
Collaborator

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


var waitGroup sync.WaitGroup

waitGroup.Add(1)
Copy link
Collaborator

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?

@pagrawal768 pagrawal768 force-pushed the BIF-1000 branch 5 times, most recently from 4cb9c4c to f02510c Compare March 12, 2024 16:46
}

return nil
callbackContext := bytes.NewBuffer(bytes.Trim(callbackContextHandler.callbackContextBuffer, "\x00")).String()
logrus.Debug("Callback context: " + callbackContext)
Copy link
Collaborator

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
Copy link
Collaborator

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?

@metehanozturk metehanozturk merged commit 66b5f85 into master May 7, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants