Skip to content

Commit 3fadcb9

Browse files
author
Oleg Sucharevich
committed
implement wait api for workflow status
1 parent 55e8665 commit 3fadcb9

File tree

4 files changed

+119
-1
lines changed

4 files changed

+119
-1
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.5.1
1+
0.6.0

cmd/wait.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright © 2019 Codefresh.Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"time"
19+
20+
"github.com/codefresh-io/go-sdk/pkg/utils"
21+
"github.com/spf13/cobra"
22+
"github.com/spf13/viper"
23+
)
24+
25+
// waitCmd represents the logs command
26+
var waitCmd = &cobra.Command{
27+
Use: "wait",
28+
Run: func(cmd *cobra.Command, args []string) {
29+
client := viper.Get("codefresh")
30+
codefreshClient := utils.CastToCodefreshOrDie(client)
31+
codefreshClient.Workflows().WaitForStatus(args[0], "success", 2*time.Second, 5*time.Minute)
32+
},
33+
}
34+
35+
func init() {
36+
rootCmd.AddCommand(waitCmd)
37+
}

pkg/codefresh/codefresh.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type (
1818
Pipelines() IPipelineAPI
1919
Tokens() ITokenAPI
2020
RuntimeEnvironments() IRuntimeEnvironmentAPI
21+
Workflows() IWorkflowAPI
2122
}
2223
)
2324

@@ -42,6 +43,10 @@ func (c *codefresh) RuntimeEnvironments() IRuntimeEnvironmentAPI {
4243
return newRuntimeEnvironmentAPI(c)
4344
}
4445

46+
func (c *codefresh) Workflows() IWorkflowAPI {
47+
return newWorkflowAPI(c)
48+
}
49+
4550
func (c *codefresh) requestAPI(opt *requestOptions) (*http.Response, error) {
4651
var body []byte
4752
finalURL := fmt.Sprintf("%s%s", c.host, opt.path)

pkg/codefresh/workflow.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package codefresh
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"time"
7+
)
8+
9+
type (
10+
IWorkflowAPI interface {
11+
WaitForStatus(string, string, time.Duration, time.Duration) error
12+
}
13+
14+
workflow struct {
15+
codefresh Codefresh
16+
}
17+
18+
Workflow struct {
19+
ID string `json:"id"`
20+
Status string `json:"status"`
21+
}
22+
)
23+
24+
func newWorkflowAPI(codefresh Codefresh) IWorkflowAPI {
25+
return &workflow{codefresh}
26+
}
27+
28+
func (w *workflow) WaitForStatus(id string, status string, interval time.Duration, timeout time.Duration) error {
29+
err := waitFor(interval, timeout, func() (bool, error) {
30+
31+
wf := &Workflow{}
32+
resp, err := w.codefresh.requestAPI(&requestOptions{
33+
path: fmt.Sprintf("/api/builds/%s", id),
34+
method: "GET",
35+
})
36+
// failed in api call
37+
if err != nil {
38+
return false, err
39+
}
40+
err = w.codefresh.decodeResponseInto(resp, wf)
41+
// failed to decode
42+
if err != nil {
43+
return false, err
44+
}
45+
// status match
46+
if wf.Status == status {
47+
return true, nil
48+
}
49+
// status dosent match
50+
return false, nil
51+
})
52+
if err != nil {
53+
return err
54+
}
55+
return nil
56+
}
57+
58+
func waitFor(interval time.Duration, timeout time.Duration, execution func() (bool, error)) error {
59+
t := time.After(timeout)
60+
tick := time.Tick(interval)
61+
// Keep trying until we're timed out or got a result or got an error
62+
for {
63+
select {
64+
// Got a timeout! fail with a timeout error
65+
case <-t:
66+
return errors.New("timed out")
67+
case <-tick:
68+
ok, err := execution()
69+
if err != nil {
70+
return err
71+
} else if ok {
72+
return nil
73+
}
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)