feat:完善项目

This commit is contained in:
henry
2021-11-19 09:24:15 +08:00
parent ba423a2d19
commit 5ff6164295
12 changed files with 149 additions and 65 deletions

View File

@ -1,9 +1,7 @@
package task
func Init() {
NewTaskListen().Listen()
//NewTaskListen().Listen()
//NewTask()("order", 60, NewOrder()).Push()
NewTaskQueue().Queue()
//NewTask()("order", 10, NewOrder()).Push()
}

View File

@ -7,11 +7,13 @@ import (
"ArmedPolice/utils"
"fmt"
"sync"
"time"
)
type TaskListen struct {
Produce chan *Task
Consume chan *Task
ticker *time.Ticker
lock *sync.Mutex
}
@ -35,12 +37,29 @@ func (this *TaskListen) Listen() {
if err != nil {
fmt.Printf("Task Produce Redis Sadd Error【%s】", err)
} else {
err = cache.Cache.HSet(config.RedisKeyForTaskQueueBody, p.ID, p)
fmt.Printf("err%v\n", err)
_ = cache.Cache.HSet(config.RedisKeyForTaskQueueBody, p.ID, p)
}
case c := <-this.Consume:
if err := c.Handle(); err != nil {
fmt.Printf("err%v\n", err)
// 监听到数据,执行后续操作
_ = c.Handle()
case <-this.ticker.C:
now := time.Now()
_cache, _ := cache.Cache.ZRangebyscore(config.RedisKeyForTaskQueue, &logic.ScoreRangeBy{Min: "0",
Max: fmt.Sprintf("%d", now.Unix())})
if len(_cache) > 0 {
for _, v := range _cache {
body, _ := cache.Cache.HGet(config.RedisKeyForTaskQueueBody, v)
// TODO有Bug读取接口数据为空
task := new(Task)
task.Body = new(Order)
_ = task.UnmarshalBinary([]byte(body))
task.Consume()
}
// 销毁信息
_ = cache.Cache.ZRem(config.RedisKeyForTaskQueue, _cache)
_ = cache.Cache.HDel(config.RedisKeyForTaskQueueBody, _cache...)
}
}
}
@ -48,12 +67,11 @@ func (this *TaskListen) Listen() {
}
func NewTaskListen() *TaskListen {
if TaskListenEvent == nil {
TaskListenEvent = &TaskListen{
Produce: make(chan *Task, 1),
Consume: make(chan *Task, 1),
lock: new(sync.Mutex),
}
TaskListenEvent = &TaskListen{
Produce: make(chan *Task, 1),
Consume: make(chan *Task, 1),
ticker: time.NewTicker(time.Second * 1),
lock: new(sync.Mutex),
}
return TaskListenEvent
}

View File

@ -3,6 +3,7 @@ package task
import (
"ArmedPolice/utils"
"encoding/json"
"fmt"
)
type Order struct {
@ -19,6 +20,8 @@ func (this *Order) UnmarshalBinary(data []byte) error {
}
func (this *Order) Handle() error {
fmt.Println(this.ID)
fmt.Println(this.Name)
return nil
}

View File

@ -49,8 +49,7 @@ func (this *Task) Consume() {
func (this *Task) Handle() error {
// 处理各种方法
this.Body.(*Order).Handle()
return nil
return this.Body.Handle()
}
func NewTask() TaskHandle {