-
Notifications
You must be signed in to change notification settings - Fork 2
/
rabbit.go
172 lines (140 loc) · 3.91 KB
/
rabbit.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package rabbit
import (
"encoding/json"
"errors"
"fmt"
"github.com/go-redis/redis"
"github.com/google/uuid"
"strings"
"time"
)
type Config struct {
Conn *redis.Client
AppName string
Job string
Queue string
Delay uint64
}
type Param struct {
Type string
Name string
Value string
}
func (c Config) Dispatch(params ...Param) error {
jobId := uuid.New().String()
pushedAt := time.Now().Unix()
if c.AppName == "" {
return errors.New("AppName is required in config")
}
if c.Queue == "" {
return errors.New("queue is required in config")
}
if c.Job == "" {
return errors.New("job is required in config")
}
appName := strings.ToLower(c.AppName)
payload, err := json.Marshal(map[string]interface{}{
"uuid": jobId,
"displayName": c.Job,
"job": "Illuminate\\Queue\\CallQueuedHandler@call",
"maxTries": nil,
"maxExceptions": nil,
"failOnTimeout": false,
"backoff": nil,
"timeout": nil,
"retryUntil": nil,
"data": map[string]string{
"commandName": c.Job,
"command": fmt.Sprintf("O:%d:\"%s\":11:{%ss:3:\"job\";N;s:10:\"connection\";N;s:5:\"queue\";s:7:\"%s\";s:15:\"chainConnection\";N;s:10:\"chainQueue\";N;s:19:\"chainCatchCallbacks\";N;s:5:\"delay\";%ss:11:\"afterCommit\";N;s:10:\"middleware\";a:0:{}s:7:\"chained\";a:0:{}}", len(c.Job), c.Job, c.generateProperty(params...), c.Queue, c.generateDelay()),
},
"id": jobId,
"attempts": 0,
"type": "job",
"tags": []interface{}{},
"silenced": false,
"pushedAt": pushedAt,
})
if err != nil {
return err
}
if c.Delay == 0 {
err = c.Conn.RPush(fmt.Sprintf("queues:%s", c.Queue), string(payload)).Err()
if err != nil {
return err
}
err = c.Conn.RPush(fmt.Sprintf("queues:%s:notify", c.Queue), 1).Err()
if err != nil {
return err
}
} else {
delayUnixTime := time.Now().Add(time.Second * time.Duration(c.Delay)).Unix()
err = c.Conn.ZAdd(fmt.Sprintf("queues:%s:delayed", c.Queue), redis.Z{
Score: float64(delayUnixTime),
Member: string(payload),
}).Err()
if err != nil {
return err
}
}
hashItems := map[string]interface{}{
"created_at": pushedAt,
"connection": "redis",
"updated_at": pushedAt,
"name": c.Job,
"id": jobId,
"queue": c.Queue,
"payload": string(payload),
"status": "pending",
}
for field, hashItem := range hashItems {
err = c.Conn.HSet(fmt.Sprintf("%s_horizon:%s", c.AppName, jobId), field, hashItem).Err()
if err != nil {
return err
}
err = c.Conn.Expire(fmt.Sprintf("%s_horizon:%s", c.AppName, jobId), time.Hour).Err()
if err != nil {
return err
}
}
err = c.Conn.ZAdd(fmt.Sprintf("%s_horizon:pending_jobs", appName), redis.Z{
Score: float64(-pushedAt),
Member: jobId,
}).Err()
if err != nil {
return err
}
err = c.Conn.ZAdd(fmt.Sprintf("%s_horizon:recent_jobs", appName), redis.Z{
Score: float64(-pushedAt),
Member: jobId,
}).Err()
if err != nil {
return err
}
return nil
}
func (c Config) generateProperty(params ...Param) string {
var generatedParams string
for _, param := range params {
switch param.Type {
case "private":
generatedParams = generatedParams + fmt.Sprintf("s:%d:\"\u0000%s\u0000%s\";s:%d:\"%s\";", len(c.Job)+2, c.Job, param.Name, len(param.Value), param.Value)
break
case "protected":
generatedParams = generatedParams + fmt.Sprintf("s:%d:\"\u0000*\u0000%s\";s:%d:\"%s\";", len(param.Name)+3, param.Name, len(param.Value), param.Value)
break
case "public":
generatedParams = generatedParams + fmt.Sprintf("s:%d:\"%s\";s:%d:\"%s\";", len(param.Name), param.Name, len(param.Value), param.Value)
break
default:
generatedParams = generatedParams + fmt.Sprintf("s:%d:\"%s\";s:%d:\"%s\";", len(param.Name), param.Name, len(param.Value), param.Value)
break
}
}
return generatedParams
}
func (c Config) generateDelay() string {
if c.Delay == 0 {
return "N;"
}
return fmt.Sprintf("i:%d;", c.Delay)
}