/
exit.go
121 lines (102 loc) · 3.8 KB
/
exit.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package aproto
import (
"fmt"
"github.com/pkg/errors"
)
// ContainerFailure holds the reason why a container did not complete successfully.
type ContainerFailure struct {
FailureType FailureType
ErrMsg string
ExitCode *ExitCode
}
func (c ContainerFailure) Error() string {
if c.ExitCode == nil {
return fmt.Sprintf("%s: %s", c.FailureType, c.ErrMsg)
}
return fmt.Sprintf("%s: %s (exit code %d)", c.FailureType, c.ErrMsg, *c.ExitCode)
}
// ExitCode is the process exit code of the container.
type ExitCode int
const (
// SuccessExitCode is the 0 zero value exit code.
SuccessExitCode = 0
)
// ContainerError returns a container failure wrapping the provided error. If the error is nil,
// a stack trace is provided instead.
func ContainerError(failureType FailureType, err error) ContainerStopped {
if err == nil {
return ContainerStopped{
Failure: &ContainerFailure{
FailureType: failureType,
ErrMsg: errors.WithStack(errors.Errorf("unknown error occurred")).Error(),
},
}
}
return ContainerStopped{
Failure: &ContainerFailure{
FailureType: failureType,
ErrMsg: err.Error(),
},
}
}
// NewContainerFailure returns a container failure wrapping the provided error. If the error is nil,
// a stack trace is provided instead.
func NewContainerFailure(failureType FailureType, err error) *ContainerFailure {
if err == nil {
return &ContainerFailure{
FailureType: failureType,
ErrMsg: errors.WithStack(errors.Errorf("unknown error occurred")).Error(),
}
}
return &ContainerFailure{
FailureType: failureType,
ErrMsg: err.Error(),
}
}
// NewContainerExit returns a container failure with the encoded exit code. If the exit code is a
// the zero value, no failure is returned.
func NewContainerExit(code ExitCode) *ContainerFailure {
if code == SuccessExitCode {
return nil
}
return &ContainerFailure{
FailureType: ContainerFailed,
ErrMsg: errors.Errorf("%s: %d", ContainerFailed, code).Error(),
ExitCode: &code,
}
}
// ContainerExited returns a container failure with the encoded exit code. If the exit code is a
// the zero value, no failure is returned.
func ContainerExited(code ExitCode) ContainerStopped {
if code == SuccessExitCode {
return ContainerStopped{}
}
return ContainerStopped{
&ContainerFailure{
FailureType: ContainerFailed,
ErrMsg: errors.Errorf("%s: %d", ContainerFailed, code).Error(),
ExitCode: &code,
},
}
}
// FailureType denotes the type of failure that resulted in the container stopping.
// Each FailureType must be handled by ./internal/task/allocation.go.
type FailureType string
const (
// ContainerFailed denotes that the container ran but failed with a non-zero exit code.
ContainerFailed FailureType = "container failed with non-zero exit code"
// ContainerAborted denotes the container was canceled before it was started.
ContainerAborted FailureType = "container was aborted before it started"
// ContainerMissing denotes the container was missing when the master asked about it.
ContainerMissing FailureType = "request for action on unknown container"
// TaskAborted denotes that the task was canceled before it was started.
TaskAborted FailureType = "task was aborted before the task was started"
// TaskError denotes that the task failed without an associated exit code.
TaskError FailureType = "task failed without an associated exit code"
// AgentFailed denotes that the agent failed while the container was running.
AgentFailed FailureType = "agent failed while the container was running"
// RestoreError denotes that we failed to restore the container after some agent failure.
RestoreError FailureType = "container failed to restore after agent failure"
// AgentError denotes that the agent failed to launch the container.
AgentError FailureType = "agent failed to launch the container"
)