-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.go
139 lines (116 loc) · 3.18 KB
/
context.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
package outis
import (
"context"
"errors"
"time"
)
type period struct {
startHour, endHour uint
startMinute, endMinute uint
}
// Context defines the data structure of the routine context
type Context struct {
Id ID
RoutineID ID
Name string
Desc string
period period
Interval time.Duration
Path string
RunAt time.Time
Watcher Watch
script func(*Context) error
metadata Metadata
latency time.Duration
notUseLoop bool
histrogram []*histogram
indicator []*indicator
log ILogger
context context.Context
}
// Context returns the application context
func (ctx *Context) Context() context.Context {
return ctx.context
}
// GetLatency get script execution latency (in seconds)
func (ctx *Context) GetLatency() float64 {
return ctx.latency.Seconds()
}
// Error creates a new error message
func (ctx *Context) Error(msg string, v ...interface{}) {
ctx.log.Errorf(msg, v...)
}
// Info creates a new info message
func (ctx *Context) Info(msg string, v ...interface{}) {
ctx.log.Infof(msg, v...)
}
// Debug creates a new debug message
func (ctx *Context) Debug(msg string, v ...interface{}) {
ctx.log.Debugf(msg, v...)
}
// Warn creates a new warn message
func (ctx *Context) Warn(msg string, v ...interface{}) {
ctx.log.Warnf(msg, v...)
}
// Panic creates a new panic message
func (ctx *Context) Panic(msg string, v ...interface{}) {
ctx.log.Panicf(msg, v...)
}
// Metadata method for adding data to routine metadata
func (ctx *Context) Metadata(key string, args interface{}) {
ctx.metadata.Set(key, args)
}
func (ctx *Context) metrics(w *Watch, now time.Time) {
w.outis.Event(ctx, EventMetric{
ID: ctx.Id.ToString(),
StartedAt: now,
FinishedAt: time.Now(),
Latency: time.Since(now),
Metadata: ctx.metadata,
Indicators: ctx.indicator,
Histograms: ctx.histrogram,
Watcher: WatcherMetric{
ID: w.Id.ToString(),
Name: w.Name,
RunAt: w.RunAt,
},
Routine: RoutineMetric{
ID: ctx.RoutineID.ToString(),
Name: ctx.Name,
Path: ctx.Path,
StartedAt: ctx.RunAt,
},
})
ctx.metadata, ctx.indicator, ctx.histrogram = Metadata{}, []*indicator{}, []*histogram{}
}
func (ctx *Context) sleep(now time.Time) {
if ctx.mustWait(now.Hour(), ctx.period.startHour, ctx.period.endHour) {
time.Sleep(time.Date(now.Year(), now.Month(), now.Day(), now.Hour()+int(ctx.period.startHour),
0, 0, 0, now.Location()).Sub(now))
}
if ctx.mustWait(now.Minute(), ctx.period.startMinute, ctx.period.endMinute) {
time.Sleep(time.Date(now.Year(), now.Month(), now.Day(), now.Hour()+int(ctx.period.startHour),
now.Minute()+int(ctx.period.startMinute), 0, 0, now.Location()).Sub(now))
}
}
func (ctx *Context) mustWait(time int, start, end uint) bool {
if start == 0 && end == 0 {
return false
}
if start <= end {
return !(time >= int(start) && time <= int(end))
}
return !(time >= int(start) || time <= int(end))
}
func (ctx *Context) validate() error {
if ctx.RoutineID == "" {
return errors.New("the routine id is required")
}
if ctx.Name == "" {
return errors.New("the routine name is required")
}
if ctx.script == nil {
return errors.New("the routine is required")
}
return nil
}