-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule.go
93 lines (87 loc) · 2.54 KB
/
schedule.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
package handler
import (
"encoding/json"
"fmt"
"github.com/mikudos/mikudos-schedule/broker"
"github.com/mikudos/mikudos-schedule/clients"
pb "github.com/mikudos/mikudos-schedule/proto/schedule"
"github.com/mikudos/mikudos-schedule/schedule"
"github.com/robfig/cron/v3"
)
// AddGrpcCron add cron to emit grpc-call events
func AddGrpcCron(scheduleStr string, grpc *pb.GrpcCall, scs *pb.Schedule, isOneTime bool) (jobID cron.EntryID, err error) {
// 解析并校验grpc信息是否正确
clientName := grpc.GetClientName()
switch clientName {
case "ai":
client := clients.AiService{}
jobID, err = schedule.Cron.AddFunc(scheduleStr, func() {
client.ClientFunc(grpc)
checkCancelList(jobID)
})
case "learn":
case "messages":
case "users":
default:
return 0, fmt.Errorf("set cron task fail: %s", "没有对应grpc client")
}
scs.Id = int32(jobID)
scs.ScheduleName += "(GRPC)"
if isOneTime {
scs.ScheduleName += "(ONETIME)"
}
b, jsonErr := json.Marshal(scs)
var str string
if jsonErr != nil {
str = fmt.Sprintf("{\"ScheduleName\":\"%s\", \"ScheduleComment\": \"%s\"}", scs.GetScheduleName(), scs.GetScheduleComment())
} else {
str = string(b)
}
if isOneTime {
schedule.OneTimeJobs[jobID] = str
} else {
schedule.CronJobs[jobID] = str
}
return jobID, err
}
// AddBrokerCron add cron to emit broker-call events
func AddBrokerCron(scheduleStr string, brokerEvent *pb.BrokerEvent, scs *pb.Schedule, isOneTime bool) (jobID cron.EntryID, err error) {
jobID, err = schedule.Cron.AddFunc(scheduleStr, func() {
fmt.Println("run oneTime cron job", schedule.CronJobs)
go broker.BrokerInstance.Send(broker.Msg{Topic: brokerEvent.GetTopic(), Key: brokerEvent.GetKey(), Message: brokerEvent.GetMessage()})
checkCancelList(jobID)
})
scs.Id = int32(jobID)
scs.ScheduleName += "(BROKER)"
if isOneTime {
scs.ScheduleName += "(ONETIME)"
}
b, jsonErr := json.Marshal(scs)
var str string
if jsonErr != nil {
str = fmt.Sprintf("{\"ScheduleName\":\"%s\", \"ScheduleComment\": \"%s\"}", scs.GetScheduleName(), scs.GetScheduleComment())
} else {
str = string(b)
}
if isOneTime {
schedule.OneTimeJobs[jobID] = str
} else {
schedule.CronJobs[jobID] = str
}
return jobID, err
}
// RemoveCron aa
func RemoveCron(jobID cron.EntryID, isOneTime bool) {
schedule.Cron.Remove(jobID)
if isOneTime {
delete(schedule.OneTimeJobs, jobID)
} else {
delete(schedule.CronJobs, jobID)
}
}
func checkCancelList(jobID cron.EntryID) {
if _, ok := schedule.OneTimeJobs[jobID]; ok {
schedule.Cron.Remove(jobID)
delete(schedule.OneTimeJobs, jobID)
}
}