-
Notifications
You must be signed in to change notification settings - Fork 236
/
hosts.go
66 lines (53 loc) · 1.78 KB
/
hosts.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package incidents
import (
"context"
"errors"
"fmt"
"time"
fly "github.com/superfly/fly-go"
"github.com/superfly/flyctl/internal/flyutil"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/logger"
"github.com/superfly/flyctl/internal/task"
"github.com/superfly/flyctl/iostreams"
)
func GetAppHostIssuesRequest(ctx context.Context, appName string) ([]fly.HostIssue, error) {
client := flyutil.ClientFromContext(ctx)
appHostIssues, err := client.GetAppHostIssues(ctx, appName)
if err != nil {
return nil, err
}
return appHostIssues, nil
}
func QueryHostIssues(ctx context.Context) {
logger := logger.FromContext(ctx)
io := iostreams.FromContext(ctx)
colorize := io.ColorScheme()
appName := appconfig.NameFromContext(ctx)
if appName == "" {
return
}
task.FromContext(ctx).RunFinalizer(func(parent context.Context) {
logger.Debug("started querying for host issues")
ctx, cancel := context.WithTimeout(context.WithoutCancel(parent), 3*time.Second)
defer cancel()
switch hostIssues, err := GetAppHostIssuesRequest(ctx, appName); {
case err == nil:
if hostIssues == nil {
break
}
logger.Debugf("querying for host issues resulted to %v", hostIssues)
hostIssuesCount := len(hostIssues)
if hostIssuesCount > 0 {
fmt.Fprintln(io.ErrOut, colorize.WarningIcon(),
colorize.Yellow("WARNING: There are active host issues affecting your app. Please check `fly incidents hosts list` or visit your app in https://fly.io/dashboard\n"),
)
break
}
case errors.Is(err, context.Canceled), errors.Is(err, context.DeadlineExceeded):
logger.Debugf("failed querying for host issues. Context cancelled or deadline exceeded: %v", err)
default:
logger.Debugf("failed querying for host issues incidents: %v", err)
}
})
}