-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiplexed_handler.go
106 lines (88 loc) · 2.72 KB
/
multiplexed_handler.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
package delayed_job
import (
"errors"
"fmt"
"time"
)
type multiplexedHandler struct {
backend *dbBackend
rules []*Job
}
func newMultiplexedHandler(ctx, params map[string]interface{}) (Handler, error) {
if nil == ctx {
return nil, errors.New("ctx is nil")
}
if nil == params {
return nil, errors.New("params is nil")
}
o, ok := ctx["backend"]
if !ok || nil == o {
return nil, errors.New("backend in the ctx is required")
}
backend, ok := o.(*dbBackend)
if !ok {
return nil, fmt.Errorf("backend in the ctx is not a backend - %T", o)
}
if nil == backend {
return nil, errors.New("backend in the ctx is nil")
}
is_valid_rule := boolWithDefault(params, "is_valid_rule", false)
gpriority := intWithDefault(params, "priority", *default_priority)
gqueue := stringWithDefault(params, "queue", *default_queue_name)
gmax_attempts := intWithDefault(params, "max_attempts", *default_max_attempts)
grun_at := timeWithDefault(params, "run_at", time.Time{})
args := params["arguments"]
o, ok = params["rules"]
if !ok || nil == o {
return nil, errors.New("'rules' is required.")
}
array, ok := o.([]interface{})
if !ok {
return nil, errors.New("'rules' is not a array")
}
if 0 == len(array) {
return &multiplexedHandler{backend: backend}, nil
}
var m_args map[string]interface{}
if nil != args {
m_args, ok = args.(map[string]interface{})
}
rules := make([]*Job, 0, len(array))
for idx, v := range array {
options, ok := v.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("rules[%v] is not a map", idx)
}
priority := intWithDefault(options, "priority", gpriority)
queue := stringWithDefault(options, "queue", gqueue)
repeat_count := intWithDefault(options, "repeat_count", 0)
repeat_interval := stringWithDefault(options, "repeat_interval", "")
max_attempts := intWithDefault(options, "max_attempts", gmax_attempts)
run_at := timeWithDefault(options, "run_at", grun_at)
if nil != args {
if own_args, ok := options["arguments"]; !ok || nil == own_args {
options["arguments"] = args
} else if mm, ok := own_args.(map[string]interface{}); ok && nil != mm && nil != m_args {
for k, v := range m_args {
mm[k] = v
}
options["arguments"] = mm
}
}
j, e := newJob(backend, priority, repeat_count, repeat_interval, max_attempts, queue, run_at, options, is_valid_rule)
if nil != e {
return nil, fmt.Errorf("rules[%d] is invalid, %v", idx, e)
}
rules = append(rules, j)
}
return &multiplexedHandler{backend: backend, rules: rules}, nil
}
func (self *multiplexedHandler) Perform() error {
if nil == self.backend {
return errors.New("backend is nil.")
}
return self.backend.create(self.rules...)
}
func init() {
Handlers["multiplexed"] = newMultiplexedHandler
}