-
Notifications
You must be signed in to change notification settings - Fork 30
/
check_task.go
36 lines (30 loc) · 731 Bytes
/
check_task.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
package gosundheit
import (
"context"
"time"
)
type checkTask struct {
stopChan chan bool
ticker *time.Ticker
check Check
timeout time.Duration
}
func (t *checkTask) stop() {
if t.ticker != nil {
t.ticker.Stop()
}
}
func (t *checkTask) execute(ctx context.Context) (details interface{}, duration time.Duration, err error) {
timeoutCtx, cancel := contextWithTimeout(ctx, t.timeout)
defer cancel()
startTime := time.Now()
details, err = t.check.Execute(timeoutCtx)
duration = time.Since(startTime)
return
}
func contextWithTimeout(parent context.Context, t time.Duration) (context.Context, context.CancelFunc) {
if t <= 0 {
return context.WithCancel(parent)
}
return context.WithTimeout(parent, t)
}