-
Notifications
You must be signed in to change notification settings - Fork 51
/
handlers.go
186 lines (163 loc) · 5.68 KB
/
handlers.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (c) 2024 Canonical Ltd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package checkstate
import (
"context"
"errors"
"fmt"
"time"
tombpkg "gopkg.in/tomb.v2"
"github.com/canonical/pebble/internals/logger"
"github.com/canonical/pebble/internals/overlord/state"
"github.com/canonical/pebble/internals/plan"
)
func (m *CheckManager) doPerformCheck(task *state.Task, tomb *tombpkg.Tomb) error {
m.state.Lock()
changeID := task.Change().ID()
var details checkDetails
err := task.Get(checkDetailsAttr, &details)
config := m.state.Cached(performConfigKey{changeID}).(*plan.Check) // panic if key not present (always should be)
m.state.Unlock()
if err != nil {
return fmt.Errorf("cannot get check details for perform-check task %q: %v", task.ID(), err)
}
logger.Debugf("Performing check %q with period %v", details.Name, config.Period.Value)
ticker := time.NewTicker(config.Period.Value)
defer ticker.Stop()
chk := newChecker(config)
for {
select {
case <-ticker.C:
err := runCheck(tomb.Context(nil), chk, config.Timeout.Value)
if !tomb.Alive() {
return checkStopped(config.Name, task.Kind(), tomb.Err())
}
if err != nil {
// Record check failure and perform any action if the threshold
// is reached (for example, restarting a service).
details.Failures++
atThreshold := details.Failures >= config.Threshold
if !atThreshold {
// Update number of failures in check info. In threshold
// case, check info will be updated with new change ID by
// changeStatusChanged.
m.updateCheckInfo(config, changeID, details.Failures)
}
m.state.Lock()
if atThreshold {
details.Proceed = true
} else {
// Add error to task log, but only if we haven't reached the
// threshold. When we hit the threshold, the "return err"
// below will cause the error to be logged.
logTaskError(task, err)
}
task.Set(checkDetailsAttr, &details)
m.state.Unlock()
logger.Noticef("Check %q failure %d/%d: %v", config.Name, details.Failures, config.Threshold, err)
if atThreshold {
logger.Noticef("Check %q threshold %d hit, triggering action and recovering", config.Name, config.Threshold)
m.callFailureHandlers(config.Name)
// Returning the error means perform-check goes to Error status
// and logs the error to the task log.
return err
}
} else if details.Failures > 0 {
m.updateCheckInfo(config, changeID, 0)
m.state.Lock()
task.Logf("succeeded after %s", pluralise(details.Failures, "failure", "failures"))
details.Failures = 0
task.Set(checkDetailsAttr, &details)
m.state.Unlock()
}
case <-tomb.Dying():
return checkStopped(config.Name, task.Kind(), tomb.Err())
}
}
}
func runCheck(ctx context.Context, chk checker, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
err := chk.check(ctx)
if errors.Is(err, context.DeadlineExceeded) {
return fmt.Errorf("check timed out after %v", timeout)
}
return err
}
func (m *CheckManager) doRecoverCheck(task *state.Task, tomb *tombpkg.Tomb) error {
m.state.Lock()
changeID := task.Change().ID()
var details checkDetails
err := task.Get(checkDetailsAttr, &details)
config := m.state.Cached(recoverConfigKey{changeID}).(*plan.Check) // panic if key not present (always should be)
m.state.Unlock()
if err != nil {
return fmt.Errorf("cannot get check details for recover-check task %q: %v", task.ID(), err)
}
logger.Debugf("Recovering check %q with period %v", details.Name, config.Period.Value)
ticker := time.NewTicker(config.Period.Value)
defer ticker.Stop()
chk := newChecker(config)
for {
select {
case <-ticker.C:
err := runCheck(tomb.Context(nil), chk, config.Timeout.Value)
if !tomb.Alive() {
return checkStopped(config.Name, task.Kind(), tomb.Err())
}
if err != nil {
details.Failures++
m.updateCheckInfo(config, changeID, details.Failures)
m.state.Lock()
task.Set(checkDetailsAttr, &details)
logTaskError(task, err)
m.state.Unlock()
logger.Noticef("Check %q failure %d/%d: %v", config.Name, details.Failures, config.Threshold, err)
break
}
// Check succeeded, switch to performing a succeeding check.
// Check info will be updated with new change ID by changeStatusChanged.
details.Failures = 0 // not strictly needed, but just to be safe
details.Proceed = true
m.state.Lock()
task.Set(checkDetailsAttr, &details)
m.state.Unlock()
return nil
case <-tomb.Dying():
return checkStopped(config.Name, task.Kind(), tomb.Err())
}
}
}
func logTaskError(task *state.Task, err error) {
message := err.Error()
var detailsErr *detailsError
if errors.As(err, &detailsErr) && detailsErr.Details() != "" {
message += "; " + detailsErr.Details()
}
task.Errorf("%s", message)
}
func checkStopped(checkName, taskKind string, tombErr error) error {
reason := " (no error)"
if tombErr != nil {
reason = ": " + tombErr.Error()
}
logger.Debugf("Check %q stopped during %s%s", checkName, taskKind, reason)
return tombErr
}
func pluralise(n int, singular, plural string) string {
if n == 1 {
return fmt.Sprintf("%d %s", n, singular)
}
return fmt.Sprintf("%d %s", n, plural)
}