-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
60 lines (50 loc) · 1.31 KB
/
utils.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
package uwe
import (
"errors"
"log"
"reflect"
)
// WorkerExistRule is a custom validation rule for the validation libs.
type WorkerExistRule struct {
message string
AvailableWorkers map[WorkerName]struct{}
}
// Validate checks that service exist on the system
func (r *WorkerExistRule) Validate(value interface{}) error {
if value == nil || reflect.ValueOf(value).IsNil() {
return nil
}
arr, ok := value.([]string)
if !ok {
return errors.New("can't convert list of workers to []string")
}
for _, v := range arr {
if _, ok := r.AvailableWorkers[WorkerName(v)]; !ok {
return errors.New("invalid service name " + v)
}
}
return nil
}
// Error sets the error message for the rule.
func (r *WorkerExistRule) Error(message string) *WorkerExistRule {
return &WorkerExistRule{message: message}
}
// STDLogEventHandler returns a callback that handles internal `Chief` events and logs its.
func STDLogEventHandler() EventHandler {
return func(event Event) {
var level string
switch event.Level {
case LvlFatal, LvlError:
level = "ERROR"
case LvlInfo:
level = "INFO"
default:
level = "WARN"
}
if event.Fields == nil {
event.Fields = map[string]interface{}{}
}
event.Fields["worker"] = event.Worker
log.Printf("%s: %s %s\n", level, event.Message, event.FormatFields())
}
}