-
Notifications
You must be signed in to change notification settings - Fork 2
/
worker.go
99 lines (87 loc) · 2.53 KB
/
worker.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
// Copyright 2020 The GoSchedule Authors. All rights reserved.
// Use of this source code is governed by BSD
// license that can be found in the LICENSE file.
package worker
import (
"reflect"
"sync"
"github.com/jasonjoo2010/goschedule/types"
"github.com/jasonjoo2010/goschedule/utils"
"github.com/sirupsen/logrus"
)
var (
registryMap sync.Map
)
func getWorkerFromType(t reflect.Type) types.Worker {
if v, ok := reflect.New(t).Interface().(types.Worker); ok {
return v
}
logrus.Warn("Entry registered is not a convertable type: ", t)
return nil
}
func GetWorker(name string) types.Worker {
var (
ok bool
v interface{}
t reflect.Type
w types.Worker
)
if v, ok = registryMap.Load(name); !ok {
logrus.Warn("No type registered for key: ", name)
return nil
}
t, ok = v.(reflect.Type)
if ok {
return getWorkerFromType(t)
}
w, ok = v.(types.Worker)
if ok {
return w
}
logrus.Warn("Type registered for key: ", name, " is not either a type nor inst")
return nil
}
func GetFunc(name string) types.FuncInterface {
if v, ok := registryMap.Load(name); ok {
if fn, ok := v.(types.FuncInterface); ok {
return fn
}
logrus.Warn("Func registered for key: ", name, " is in incorrect type")
return nil
}
logrus.Warn("No func registered for key: ", name)
return nil
}
// Register registers specific type with its full package path as key
func Register(worker types.Worker) {
if worker == nil {
panic("Could not register a worker type using nil as value")
}
RegisterName(utils.TypeName(utils.Dereference(worker)), worker)
}
// RegisterName registers specific type with specific name as key
func RegisterName(name string, worker types.Worker) {
if name == "" {
panic("Could not register a worker type without name")
}
if worker == nil {
panic("Could not register a worker type using nil as value")
}
t := reflect.TypeOf(utils.Dereference(worker))
registryMap.Store(name, t)
logrus.Info("Register new worker type: ", name)
}
// RegisterInst registers an instance provided instead of its type
func RegisterInst(worker types.Worker) {
RegisterInstName(utils.TypeName(worker), worker)
}
// RegisterInstName registers an instance with given name
func RegisterInstName(name string, worker types.Worker) {
registryMap.Store(name, worker)
logrus.Info("Register a worker instance: ", name)
}
// RegisterFunc registers func worker into registry which could be fetch through GetFunc(name string)
func RegisterFunc(name string, fn types.FuncInterface) {
registryMap.Store(name, fn)
logrus.Info("Register new worker func: ", name)
}