-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.go
42 lines (38 loc) · 1.05 KB
/
scheduler.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
package acclient
import (
"github.com/subiz/header"
"github.com/subiz/kafka"
"google.golang.org/protobuf/proto"
)
func BookTask(topic, key, accid string, sec int64, data []byte) {
kafka.Publish("scheduler", &header.SchedulerTask{
AccountId: accid,
Sec: sec,
Payload: data,
Topic: topic,
Partition: -1,
Key: key,
})
}
func BookTaskP(topic, key, accid string, sec int64, partition int32, data []byte) {
kafka.Publish("scheduler", &header.SchedulerTask{
AccountId: accid,
Sec: sec,
Payload: data,
Topic: topic,
Partition: partition,
Key: key,
})
}
func OnSchedule(csm, topic string, predicate func(accid string, data []byte)) error {
return kafka.Listen(csm, topic, func(topic string, partition int32, offset int64, data []byte) {
task := &header.SchedulerTask{}
proto.Unmarshal(data, task)
if task.GetAccountId() == "" {
kafka.MarkOffset(csm, topic, partition, offset+1)
return
}
predicate(task.GetAccountId(), task.GetPayload())
kafka.MarkOffset(csm, topic, partition, offset+1)
})
}