Files
ArmedPolice/task/listen.go
2021-11-02 16:22:07 +08:00

60 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package task
import (
"ArmedPolice/config"
"ArmedPolice/serve/cache"
"ArmedPolice/serve/cache/logic"
"ArmedPolice/utils"
"fmt"
"sync"
)
type TaskListen struct {
Produce chan *Task
Consume chan *Task
lock *sync.Mutex
}
var TaskListenEvent *TaskListen
func (this *TaskListen) Join(task *Task) {
TaskListenEvent.Produce <- task
}
func (this *TaskListen) Quit(task *Task) {
TaskListenEvent.Consume <- task
}
func (this *TaskListen) Listen() {
go utils.TryCatch(func() {
for {
select {
case p := <-this.Produce:
err := cache.Cache.ZAdd(config.RedisKeyForTaskQueue, &logic.ScoreParams{Score: float64(p.DelayUnix()), Member: p.ID})
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)
}
case c := <-this.Consume:
if err := c.Handle(); err != nil {
fmt.Printf("err%v\n", err)
}
}
}
})
}
func NewTaskListen() *TaskListen {
if TaskListenEvent == nil {
TaskListenEvent = &TaskListen{
Produce: make(chan *Task, 1),
Consume: make(chan *Task, 1),
lock: new(sync.Mutex),
}
}
return TaskListenEvent
}