forked from qinguoyi/asyncjob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasyncjobtest.go
75 lines (69 loc) · 1.61 KB
/
asyncjobtest.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
package main
import (
"encoding/json"
"fmt"
"github.com/qinguoyi/asyncjob/app/pkg/base"
"io"
"strings"
"time"
)
func sendTestMsg(delay int) map[string]interface{} {
start := time.Now().Add(time.Duration(delay) * time.Second)
body := map[string]interface{}{
"type": "test_msg",
"startTime": start.Format("2006-01-02 15:04:05"),
"body": map[string]interface{}{"id": 1},
"ttl": 100,
"retry": 0,
}
return body
}
func sendMailMsg(cronSpec string) map[string]interface{} {
body := map[string]interface{}{
"type": "mail_msg",
"cronSpec": cronSpec,
"body": map[string]interface{}{
"toList": []string{"[email protected]"},
"subject": "今晚吃什么?",
"content": "不吃火锅,就吃烤匠。",
},
"ttl": 100,
"retry": 0,
}
return body
}
func main() {
baseUrl := "http://127.0.0.1:8888"
// 非周期任务
urlStr := "/api/async/v0/job/async"
jsonBytes, err := json.Marshal(sendTestMsg(10))
if err != nil {
panic(err)
}
req := base.Request{
Url: fmt.Sprintf("%s%s", baseUrl, urlStr),
Body: io.NopCloser(strings.NewReader(string(jsonBytes))),
Method: "POST",
Params: map[string]string{},
}
_, _, _, err = base.Ask(req)
if err != nil {
panic(err)
}
// 周期任务
urlStr = "/api/async/v0/job/periodic"
jsonBytes, err = json.Marshal(sendMailMsg("0/5 * * * *"))
if err != nil {
panic(err)
}
req = base.Request{
Url: fmt.Sprintf("%s%s", baseUrl, urlStr),
Body: io.NopCloser(strings.NewReader(string(jsonBytes))),
Method: "POST",
Params: map[string]string{},
}
_, _, _, err = base.Ask(req)
if err != nil {
panic(err)
}
}