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

check wf status to avoid infinite loops #45

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions orchard/orchard_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ func (c OrchardRestClient) create(result string) (string, error) {
return string(orchardID), nil
}

func (c OrchardRestClient) Details(orchardID string) (*Details, error) {
url := fmt.Sprintf("%s/v1/workflow/%s/details", c.Host, orchardID)
resp, err := c.request(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
Copy link
Contributor

Choose a reason for hiding this comment

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

make sure you close the body afterwards with defer rsp.Body.Close()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it. Thanks

if resp.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("workflow %s is not found", orchardID)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
details, err := ParseDetails(body)
return details, err
}

func (c OrchardRestClient) Activate(orchardID string) error {
url := fmt.Sprintf("%s/v1/workflow/%s/activate", c.Host, orchardID)
_, err := c.request(http.MethodPut, url, nil)
Expand Down
27 changes: 27 additions & 0 deletions orchard/orchard_models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2022, Salesforce, Inc.
// All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
// For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause

package orchard

import (
"encoding/json"
"fmt"
)

type Details struct {
Id string `json:"id" binding:"required"`
Name string `json:"name" binding:"required"`
Status string `json:"status" binding:"required"`
CreatedAt string `json:"createdAt" binding:"required"`
}

func ParseDetails(resp []byte) (*Details, error) {
var data Details
err := json.Unmarshal(resp, &data)
if err != nil {
return nil, fmt.Errorf("unable to decode the response: %v", err)
}
return &data, nil
}
36 changes: 36 additions & 0 deletions orchard/orchard_models_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2022, Salesforce, Inc.
// All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
// For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause

package orchard

import (
"fmt"
"testing"
)

func TestDetails(t *testing.T) {
wfId := "wf-e7901f9f-8e88-4173-9d6e-d63f8b1e647f"
wfName := "test.workflow"
wfStats := "pending"
wfCreatedAt := "2024-12-12T02:12:19.260463"
wfStr := fmt.Sprintf(`{"id":"%s","name":"%s","status":"%s","createdAt":"%s"}`, wfId, wfName, wfStats, wfCreatedAt)
resp := []byte(wfStr)
details, err := ParseDetails(resp)
if err != nil {
t.Fatalf("unable to parse response: %v", err)
}
if details.Id != wfId {
t.Fatalf("parsed %s doesn't match %s", details.Id, wfId)
}
if details.Name != wfName {
t.Fatalf("parsed %s doesn't match %s", details.Name, wfName)
}
if details.Status != wfStats {
t.Fatalf("parsed %s doesn't match %s", details.Status, wfStats)
}
if details.CreatedAt != wfCreatedAt {
t.Fatalf("parsed %s doesn't match %s", details.CreatedAt, wfCreatedAt)
}
}
4 changes: 4 additions & 0 deletions service/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ func (s *Scheduler) activateWorkflow(
wf table.Workflow,
) string {
if err := client.Activate(swf.OrchardID); err != nil {
details, err2 := client.Details(swf.OrchardID)
if err2 == nil && details.Status != "pending" {
Copy link
Collaborator

Choose a reason for hiding this comment

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

could details be nil? does it need a nil check before details.Status

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should just change client.Activate to check the return code upon error and send a Detail call, instead of doing it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

could details be nil? does it need a nil check before details.Status

@schowsf yeah, can add a nil check before details.Status

return Activated.ToString()
}
fmt.Printf("[error] error activating workflow (name: %s, orchard_id: %s): %s\n", wf.Name, swf.OrchardID, err)
notifyOwner(wf, err)
return swf.Status
Expand Down