-
Notifications
You must be signed in to change notification settings - Fork 329
/
queue.go
99 lines (77 loc) · 1.67 KB
/
queue.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
package types
import (
"database/sql/driver"
"encoding/json"
"github.com/cortezaproject/corteza/server/pkg/sql"
"time"
"github.com/cortezaproject/corteza/server/pkg/filter"
"github.com/spf13/cast"
)
type (
Message struct {
Q string
P []byte
}
Queue struct {
Name string
Consumer Consumer
Meta QueueMeta
}
QueueDb struct {
Queue string
Consumer string
Meta QueueMeta
}
QueueFilter struct {
Query string
Deleted filter.State
filter.Sorting
filter.Paging
}
QueueSet map[string]*Queue
QueueMeta struct {
PollDelay *time.Duration `json:"poll_delay"`
DispatchEvents bool `json:"dispatch_events"`
}
QueueMessage struct {
Queue string
Payload []byte
}
QueueMessageSet []QueueMessage
)
func (h *QueueMeta) UnmarshalJSON(s []byte) error {
type Alias QueueMeta
aux := &struct {
PollDelay string `json:"poll_delay"`
*Alias
}{
Alias: (*Alias)(h),
}
// set default
h.DispatchEvents = false
if err := json.Unmarshal(s, aux); err != nil {
return err
}
if d, err := cast.ToDurationE(aux.PollDelay); err == nil {
h.PollDelay = &d
}
return nil
}
func (m *QueueMeta) Scan(src any) error { return sql.ParseJSON(src, m) }
func (m QueueMeta) Value() (driver.Value, error) { return json.Marshal(m) }
func (m QueueMeta) MarshalJSON() ([]byte, error) {
pollDelay := ""
if m.PollDelay != nil {
pollDelay = m.PollDelay.String()
}
return json.Marshal(struct {
PollDelay string `json:"poll_delay"`
DispatchEvents bool `json:"dispatch_events"`
}{
PollDelay: pollDelay,
DispatchEvents: m.DispatchEvents,
})
}
func (s *Queue) CanDispatch() bool {
return s.Meta.DispatchEvents
}