Skip to content

Commit

Permalink
review comment changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kapishmalik committed Apr 13, 2024
1 parent ca64d19 commit 556341c
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions core/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,6 @@ func (action *Action) Execute(pair *models.RequestResponsePair, journalIDChannel
//adding 200 ms to include some buffer for it to return response
time.Sleep(time.Duration(200+action.DelayInMs) * time.Millisecond)

journalID := <-journalIDChannel
log.Info("Journal ID received ", journalID)

//if it is remote callback
if action.Remote != "" {

Expand All @@ -154,16 +151,18 @@ func (action *Action) Execute(pair *models.RequestResponsePair, journalIDChannel

resp, err := http.DefaultClient.Do(req)
completionTime := time.Now()
journalID, received := receiveJournalIdWithTimeout(journalIDChannel, time.Second)
log.Info("Journal ID received ", journalID)

if err != nil {
journal.UpdatePostServeActionDetailsInJournal(journalID, pair.Response.PostServeAction, correlationID, invokedTime, completionTime, 0)
updateJournal(pair, received, journal, journalID, correlationID, invokedTime, completionTime, 0)
log.WithFields(log.Fields{
"error": err.Error(),
}).Error("Error when communicating with remote post serve action")
return err
}

journal.UpdatePostServeActionDetailsInJournal(journalID, pair.Response.PostServeAction, correlationID, invokedTime, completionTime, resp.StatusCode)
updateJournal(pair, received, journal, journalID, correlationID, invokedTime, completionTime, resp.StatusCode)
if resp.StatusCode != 200 {
log.Error("Remote post serve action did not process payload")

Expand All @@ -176,10 +175,6 @@ func (action *Action) Execute(pair *models.RequestResponsePair, journalIDChannel
invokedTime := time.Now()
actionCommand := exec.Command(action.Binary, action.Script.Name())
actionCommand.Stdin = bytes.NewReader(pairViewBytes)
completionTime := time.Now()

journal.UpdatePostServeActionDetailsInJournal(journalID, pair.Response.PostServeAction, "", invokedTime, completionTime, 0)

var stdout bytes.Buffer
var stderr bytes.Buffer
actionCommand.Stdout = &stdout
Expand All @@ -191,6 +186,9 @@ func (action *Action) Execute(pair *models.RequestResponsePair, journalIDChannel
if err := actionCommand.Wait(); err != nil {
return err
}
completionTime := time.Now()
journalID, received := receiveJournalIdWithTimeout(journalIDChannel, time.Second)
updateJournal(pair, received, journal, journalID, "", invokedTime, completionTime, 0)

if len(stderr.Bytes()) > 0 {
log.Error("Error occurred while executing script " + stderr.String())
Expand All @@ -202,6 +200,34 @@ func (action *Action) Execute(pair *models.RequestResponsePair, journalIDChannel
return nil
}

func updateJournal(pair *models.RequestResponsePair,
received bool,
journal *journal.Journal,
journalID string,
correlationID string,
invokedTime time.Time,
completionTime time.Time,
httpStatus int) {

if received {
journal.UpdatePostServeActionDetailsInJournal(journalID,
pair.Response.PostServeAction,
correlationID,
invokedTime,
completionTime,
httpStatus)
}
}

func receiveJournalIdWithTimeout(journalIDChannel chan string, timeout time.Duration) (string, bool) {
select {
case msg := <-journalIDChannel:
return msg, true
case <-time.After(timeout):
return "", false
}
}

func isValidURL(host string) bool {

if _, err := url.ParseRequestURI(host); err == nil {
Expand Down

0 comments on commit 556341c

Please sign in to comment.