Skip to content

Commit 42a683b

Browse files
committed
refactor: 代码优化
1 parent 6c0a8bc commit 42a683b

File tree

3 files changed

+61
-56
lines changed

3 files changed

+61
-56
lines changed

README.md

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ URL地址 `/delete`
136136
| id | string | Job唯一标识 | |
137137

138138

139-
### 查询任务
140-
URL地址 `/get`
139+
### 完成任务
140+
URL地址 `/finish`
141141

142142
```json
143143
{
@@ -147,39 +147,54 @@ URL地址 `/get`
147147

148148
| 参数名 | 类型 | 含义 | 备注 |
149149
|:-------:|:-----------:|:------------:|:-----------------:|
150-
| id | string | Job唯一标识 | |
150+
| id | string | Job唯一标识 | |
151151

152152

153+
### 查询任务
154+
URL地址 `/get`
153155

154-
队列中有任务返回值
155156
```json
156157
{
157-
"code": 0,
158-
"message": "操作成功",
159-
"data": {
160-
"id": "15702398321",
161-
"body": "{\"uid\": 10829378,\"created\": 1498657365 }"
162-
}
158+
"id": "15702398321"
163159
}
164160
```
165-
队列为空返回值
161+
162+
| 参数名 | 类型 | 含义 | 备注 |
163+
|:-------:|:-----------:|:------------:|:-----------------:|
164+
| id | string | Job唯一标识 | |
165+
166+
167+
返回值
166168
```json
167169
{
168-
"code": 0,
169-
"message": "操作成功",
170-
"data": null
170+
"code": 0,
171+
"message": "操作成功",
172+
"data": {
173+
"topic": "order",
174+
"id": "15702398321",
175+
"delay": 1506787453,
176+
"ttr": 60,
177+
"body": "{\"uid\": 10829378,\"created\": 1498657365 }"
178+
179+
}
171180
}
172181
```
173-
174-
### 完成任务
175-
URL地址 `/finish`
176182

183+
| 参数名 | 类型 | 含义 | 备注 |
184+
|:-------:|:-----------:|:------------:|:-----------------:|
185+
| topic | string | Job类型 | |
186+
| id | string | Job唯一标识 | |
187+
| delay | int | Job延迟执行的时间戳 | |
188+
| ttr | int | Job执行超时时间, 单位:秒 | |
189+
| body | string | Job内容,供消费者做具体的业务处理 |
190+
191+
192+
Job不存在返回值
177193
```json
178194
{
179-
"id": "15702398321"
195+
"code": 0,
196+
"message": "操作成功",
197+
"data": null
180198
}
181199
```
182-
183-
| 参数名 | 类型 | 含义 | 备注 |
184-
|:-------:|:-----------:|:------------:|:-----------------:|
185-
| id | string | Job唯一标识 | |
200+

delayqueue/job.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import (
55
)
66

77
type Job struct {
8-
Topic string `json:"topic"`
9-
Id string `json:"id"` // job唯一标识ID
10-
Delay int64 `json:"delay"` // 延迟时间, unix时间戳
11-
TTR int64 `json:"ttr"`
12-
Body string `json:"body"`
8+
Topic string `json:"topic" msgpack:"1"`
9+
Id string `json:"id" msgpack:"2"` // job唯一标识ID
10+
Delay int64 `json:"delay" msgpack:"3"` // 延迟时间, unix时间戳
11+
TTR int64 `json:"ttr" msgpack:"4"`
12+
Body string `json:"body" msgpack:"5"`
1313
}
1414

1515
// 获取Job

routers/routers.go

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import (
1111
"github.com/ouqiang/delay-queue/delayqueue"
1212
)
1313

14-
type PopRequest struct {
14+
type TopicRequest struct {
1515
Topic string `json:"topic"`
1616
}
1717

18-
type DeleteRequest struct {
18+
type IdRequest struct {
1919
Id string `json:"id"`
2020
}
2121

@@ -54,20 +54,21 @@ func Push(resp http.ResponseWriter, req *http.Request) {
5454
err = delayqueue.Push(job)
5555

5656
if err != nil {
57-
resp.Write(generateFailureBody("添加失败"))
57+
log.Printf("添加job失败", err.Error())
58+
resp.Write(generateFailureBody(err.Error()))
5859
} else {
5960
resp.Write(generateSuccessBody("添加成功", nil))
6061
}
6162
}
6263

6364
// 获取job
6465
func Pop(resp http.ResponseWriter, req *http.Request) {
65-
var popRequest PopRequest
66-
err := readBody(resp, req, &popRequest)
66+
var topicRequest TopicRequest
67+
err := readBody(resp, req, &topicRequest)
6768
if err != nil {
6869
return
6970
}
70-
topic := strings.TrimSpace(popRequest.Topic)
71+
topic := strings.TrimSpace(topicRequest.Topic)
7172
if topic == "" {
7273
resp.Write(generateFailureBody("topic不能为空"))
7374
return
@@ -77,7 +78,7 @@ func Pop(resp http.ResponseWriter, req *http.Request) {
7778
job, err := delayqueue.Pop(topics)
7879
if err != nil {
7980
log.Printf("获取job失败#%s", err.Error())
80-
resp.Write(generateFailureBody("获取Job失败"))
81+
resp.Write(generateFailureBody(err.Error()))
8182
return
8283
}
8384

@@ -103,43 +104,44 @@ func Pop(resp http.ResponseWriter, req *http.Request) {
103104

104105
// 删除job
105106
func Delete(resp http.ResponseWriter, req *http.Request) {
106-
var deleteRequest DeleteRequest
107-
err := readBody(resp, req, &deleteRequest)
107+
var idRequest IdRequest
108+
err := readBody(resp, req, &idRequest)
108109
if err != nil {
109110
return
110111
}
111-
id := strings.TrimSpace(deleteRequest.Id)
112+
id := strings.TrimSpace(idRequest.Id)
112113
if id == "" {
113114
resp.Write(generateFailureBody("job id不能为空"))
114115
return
115116
}
116117

118+
log.Printf("delete job#jobId-%s\n", id)
117119
err = delayqueue.Remove(id)
118120
if err != nil {
119-
resp.Write(generateFailureBody("删除失败"))
121+
log.Printf("删除job失败#%s", err.Error())
122+
resp.Write(generateFailureBody(err.Error()))
120123
return
121124
}
122-
log.Printf("delete job#jobId-%s\n", id)
123125

124126
resp.Write(generateSuccessBody("操作成功", nil))
125127
}
126128

127129
// 查询job
128130
func Get(resp http.ResponseWriter, req *http.Request) {
129-
var deleteRequest DeleteRequest
130-
err := readBody(resp, req, &deleteRequest)
131+
var idRequest IdRequest
132+
err := readBody(resp, req, &idRequest)
131133
if err != nil {
132134
return
133135
}
134-
id := strings.TrimSpace(deleteRequest.Id)
136+
id := strings.TrimSpace(idRequest.Id)
135137
if id == "" {
136138
resp.Write(generateFailureBody("job id不能为空"))
137139
return
138140
}
139141
job, err := delayqueue.Get(id)
140142
if err != nil {
141143
log.Printf("查询job失败#%s", err.Error())
142-
resp.Write(generateFailureBody("查询Job失败"))
144+
resp.Write(generateFailureBody(err.Error()))
143145
return
144146
}
145147

@@ -148,19 +150,7 @@ func Get(resp http.ResponseWriter, req *http.Request) {
148150
return
149151
}
150152

151-
type Data struct {
152-
Id string `json:"id"`
153-
Body string `json:"body"`
154-
}
155-
156-
data := Data{
157-
Id: job.Id,
158-
Body: job.Body,
159-
}
160-
161-
log.Printf("get job#%+v", data)
162-
163-
resp.Write(generateSuccessBody("操作成功", data))
153+
resp.Write(generateSuccessBody("操作成功", job))
164154
}
165155

166156
type ResponseBody struct {

0 commit comments

Comments
 (0)