mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-08-26 16:46:14 +08:00
发布v2.3.5版本,本次为优化版本。更新内容请查看:https://github.com/bufanyun/hotgo/blob/v2.0/docs/guide-zh-CN/start-update-log.md
This commit is contained in:
@@ -1,70 +0,0 @@
|
||||
// Package queues
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package queues
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"hotgo/internal/library/queue"
|
||||
)
|
||||
|
||||
type jobStrategy interface {
|
||||
getTopic() string
|
||||
handle(ctx context.Context, mqMsg queue.MqMsg) (err error)
|
||||
}
|
||||
|
||||
var jobList []jobStrategy
|
||||
|
||||
func Run(ctx context.Context) {
|
||||
for _, job := range uniqueJob(jobList) {
|
||||
go func(job jobStrategy) {
|
||||
listen(ctx, job)
|
||||
}(job)
|
||||
}
|
||||
}
|
||||
|
||||
func listen(ctx context.Context, job jobStrategy) {
|
||||
var (
|
||||
topic = job.getTopic()
|
||||
consumer, err = queue.InstanceConsumer()
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
g.Log().Fatalf(ctx, "InstanceConsumer %s err:%+v", topic, err)
|
||||
return
|
||||
}
|
||||
|
||||
// 访问日志
|
||||
if listenErr := consumer.ListenReceiveMsgDo(topic, func(mqMsg queue.MqMsg) {
|
||||
err = job.handle(ctx, mqMsg)
|
||||
|
||||
if err != nil {
|
||||
// 遇到错误,重新加入到队列
|
||||
//queue.Push(topic, mqMsg.Body)
|
||||
}
|
||||
|
||||
// 记录队列日志
|
||||
queue.ConsumerLog(ctx, topic, mqMsg, err)
|
||||
|
||||
}); listenErr != nil {
|
||||
g.Log().Fatalf(ctx, "队列:%s 监听失败, err:%+v", topic, listenErr)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// uniqueJob 去重
|
||||
func uniqueJob(languages []jobStrategy) []jobStrategy {
|
||||
result := make([]jobStrategy, 0, len(languages))
|
||||
temp := map[jobStrategy]struct{}{}
|
||||
for _, item := range languages {
|
||||
if _, ok := temp[item]; !ok {
|
||||
temp[item] = struct{}{}
|
||||
result = append(result, item)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
@@ -3,7 +3,6 @@
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package queues
|
||||
|
||||
import (
|
||||
@@ -16,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
func init() {
|
||||
jobList = append(jobList, LoginLog)
|
||||
queue.RegisterConsumer(LoginLog)
|
||||
}
|
||||
|
||||
// LoginLog 登录日志
|
||||
@@ -24,13 +23,13 @@ var LoginLog = &qLoginLog{}
|
||||
|
||||
type qLoginLog struct{}
|
||||
|
||||
// getTopic 主题
|
||||
func (q *qLoginLog) getTopic() string {
|
||||
// GetTopic 主题
|
||||
func (q *qLoginLog) GetTopic() string {
|
||||
return consts.QueueLoginLogTopic
|
||||
}
|
||||
|
||||
// handle 处理消息
|
||||
func (q *qLoginLog) handle(ctx context.Context, mqMsg queue.MqMsg) (err error) {
|
||||
// Handle 处理消息
|
||||
func (q *qLoginLog) Handle(ctx context.Context, mqMsg queue.MqMsg) (err error) {
|
||||
var data entity.SysLoginLog
|
||||
if err = json.Unmarshal(mqMsg.Body, &data); err != nil {
|
||||
return err
|
||||
|
@@ -3,7 +3,6 @@
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package queues
|
||||
|
||||
import (
|
||||
@@ -16,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
func init() {
|
||||
jobList = append(jobList, ServeLog)
|
||||
queue.RegisterConsumer(ServeLog)
|
||||
}
|
||||
|
||||
// ServeLog 登录日志
|
||||
@@ -24,13 +23,13 @@ var ServeLog = &qServeLog{}
|
||||
|
||||
type qServeLog struct{}
|
||||
|
||||
// getTopic 主题
|
||||
func (q *qServeLog) getTopic() string {
|
||||
// GetTopic 主题
|
||||
func (q *qServeLog) GetTopic() string {
|
||||
return consts.QueueServeLogTopic
|
||||
}
|
||||
|
||||
// handle 处理消息
|
||||
func (q *qServeLog) handle(ctx context.Context, mqMsg queue.MqMsg) (err error) {
|
||||
// Handle 处理消息
|
||||
func (q *qServeLog) Handle(ctx context.Context, mqMsg queue.MqMsg) (err error) {
|
||||
var data entity.SysServeLog
|
||||
if err = json.Unmarshal(mqMsg.Body, &data); err != nil {
|
||||
return err
|
||||
|
@@ -3,7 +3,6 @@
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package queues
|
||||
|
||||
import (
|
||||
@@ -16,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
func init() {
|
||||
jobList = append(jobList, SysLog)
|
||||
queue.RegisterConsumer(SysLog)
|
||||
}
|
||||
|
||||
// SysLog 系统日志
|
||||
@@ -24,13 +23,13 @@ var SysLog = &qSysLog{}
|
||||
|
||||
type qSysLog struct{}
|
||||
|
||||
// getTopic 主题
|
||||
func (q *qSysLog) getTopic() string {
|
||||
// GetTopic 主题
|
||||
func (q *qSysLog) GetTopic() string {
|
||||
return consts.QueueLogTopic
|
||||
}
|
||||
|
||||
// handle 处理消息
|
||||
func (q *qSysLog) handle(ctx context.Context, mqMsg queue.MqMsg) (err error) {
|
||||
// Handle 处理消息
|
||||
func (q *qSysLog) Handle(ctx context.Context, mqMsg queue.MqMsg) (err error) {
|
||||
var data entity.SysLog
|
||||
if err = json.Unmarshal(mqMsg.Body, &data); err != nil {
|
||||
return err
|
||||
|
Reference in New Issue
Block a user