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 all 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
28 changes: 26 additions & 2 deletions orchard/orchard_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (c OrchardRestClient) request(method string, url string, body io.Reader) (*
return nil, err
}
if rsp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("invalid http code %d", rsp.StatusCode)
return rsp, fmt.Errorf("invalid http code %d", rsp.StatusCode)
}
return rsp, nil
}
Expand Down Expand Up @@ -86,9 +86,33 @@ 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)
rsp, 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 rsp.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("workflow %s is not found", orchardID)
}
defer rsp.Body.Close()
body, err := io.ReadAll(rsp.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)
rsp, err := c.request(http.MethodPut, url, nil)
if err != nil && rsp != nil && rsp.StatusCode == http.StatusNotFound {
details, _ := c.Details(orchardID)
if details != nil && details.Status != "pending" {
return nil
}
}
return err
}

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)
}
}
Loading