-
Notifications
You must be signed in to change notification settings - Fork 34
/
status.go
52 lines (47 loc) · 1.48 KB
/
status.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
package main
import (
"strconv"
"github.com/Azure/azure-docker-extension/pkg/vmextension"
"github.com/Azure/azure-docker-extension/pkg/vmextension/status"
"github.com/go-kit/kit/log"
"github.com/pkg/errors"
)
// reportStatus saves operation status to the status file for the extension
// handler with the optional given message, if the given cmd requires reporting
// status.
//
// If an error occurs reporting the status, it will be logged and returned.
func reportStatus(ctx *log.Context, hEnv vmextension.HandlerEnvironment, t status.Type, c cmd, msg string) error {
if !c.shouldReportStatus {
ctx.Log("status", "not reported for operation (by design)")
return nil
}
s := status.NewStatus(t, c.name, statusMsg(c, t, msg))
seq, _ := strconv.Atoi(hEnv.SeqNo)
if err := s.Save(hEnv.HandlerEnvironment.StatusFolder, seq); err != nil {
ctx.Log("event", "failed to save handler status", "error", err)
return errors.Wrap(err, "failed to save handler status")
}
return nil
}
// statusMsg creates the reported status message based on the provided operation
// type and the given message string.
//
// A message will be generated for empty string. For error status, pass the
// error message.
func statusMsg(c cmd, t status.Type, msg string) string {
s := c.name
switch t {
case status.StatusSuccess:
s += " succeeded"
case status.StatusTransitioning:
s += " in progress"
case status.StatusError:
s += " failed"
}
if msg != "" {
// append the original
s += ": " + msg
}
return s
}