-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwait_warning.go
More file actions
67 lines (53 loc) · 1.31 KB
/
Copy pathwait_warning.go
File metadata and controls
67 lines (53 loc) · 1.31 KB
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
package logger
import (
"context"
"fmt"
"sync"
"time"
)
type WaitingWarning struct {
active bool
interrupt chan interface{}
sync.Mutex
}
// NewWaitingWarning creates a repeated warning message when waiting for something to complete.
// name is displayed in the log entry that is repeated until the process finishes.
// frequency is the number of seconds between log entries.
func NewWaitingWarning(ctx context.Context, frequency time.Duration, format string,
values ...interface{}) *WaitingWarning {
result := &WaitingWarning{
active: true,
interrupt: make(chan interface{}),
}
caller := GetCaller(1)
// start thread
go func() {
runWaitWarning(ctx, fmt.Sprintf(format, values...), caller, frequency,
result.interrupt)
}()
return result
}
func runWaitWarning(ctx context.Context, name, caller string, frequency time.Duration,
interrupt <-chan interface{}) {
start := time.Now()
for {
select {
case <-time.After(frequency):
LogDepthWithFields(ctx, LevelWarn, caller, []Field{
Timestamp("start", start.UnixNano()),
MillisecondsFromNano("elapsed_ms", time.Since(start).Nanoseconds()),
}, "Waiting for: %s", name)
case <-interrupt:
return
}
}
}
func (w *WaitingWarning) Cancel() {
w.Lock()
defer w.Unlock()
if !w.active {
return
}
close(w.interrupt)
w.active = false
}