-
Notifications
You must be signed in to change notification settings - Fork 224
/
machine.go
43 lines (35 loc) · 868 Bytes
/
machine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package monitor
import (
"context"
"time"
"github.com/pkg/errors"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/pkg/retry"
)
func WaitForRunningMachine(ctx context.Context, appName string, machineID string, apiClient *api.Client) (*api.Machine, error) {
var err error
for {
var machines []*api.Machine
fn := func() error {
machines, err = apiClient.ListMachines(ctx, appName, "")
return err
}
if err := retry.Retry(fn, 3); err != nil {
return nil, err
}
for _, machine := range machines {
if machine.ID == machineID {
if machine.State == "started" {
return machine, nil
}
if machine.State != "starting" {
return nil, errors.Errorf("machine isn't starting, state: %s", machine.State)
}
}
}
if err := ctx.Err(); err != nil {
return nil, err
}
time.Sleep(1 * time.Second)
}
}