mirror of
https://github.com/bufanyun/hotgo.git
synced 2026-02-19 12:54:32 +08:00
更新2.1.2版本,优化部门、角色权限,增加上下级关系;增加登录、系统、短信日志;优化省市区编码
This commit is contained in:
@@ -8,22 +8,63 @@ package queues
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"hotgo/internal/library/queue"
|
||||
)
|
||||
|
||||
type jobStrategy interface {
|
||||
Listen(ctx context.Context)
|
||||
getTopic() string
|
||||
handle(ctx context.Context, mqMsg queue.MqMsg) (err error)
|
||||
}
|
||||
|
||||
var (
|
||||
jobList = []jobStrategy{
|
||||
SysLog,
|
||||
}
|
||||
)
|
||||
var jobList []jobStrategy
|
||||
|
||||
func Run(ctx context.Context) {
|
||||
for _, job := range jobList {
|
||||
job.Listen(ctx)
|
||||
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
|
||||
}
|
||||
|
||||
39
server/internal/queues/login_log.go
Normal file
39
server/internal/queues/login_log.go
Normal file
@@ -0,0 +1,39 @@
|
||||
// Package queues
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2022 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package queues
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/library/queue"
|
||||
"hotgo/internal/model/entity"
|
||||
"hotgo/internal/service"
|
||||
)
|
||||
|
||||
func init() {
|
||||
jobList = append(jobList, LoginLog)
|
||||
}
|
||||
|
||||
// LoginLog 登录日志
|
||||
var LoginLog = &qLoginLog{}
|
||||
|
||||
type qLoginLog struct{}
|
||||
|
||||
// getTopic 主题
|
||||
func (q *qLoginLog) getTopic() string {
|
||||
return consts.QueueLoginLogTopic
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
return service.SysLoginLog().RealWrite(ctx, data)
|
||||
}
|
||||
39
server/internal/queues/serve_log.go
Normal file
39
server/internal/queues/serve_log.go
Normal file
@@ -0,0 +1,39 @@
|
||||
// Package queues
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2022 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package queues
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/library/queue"
|
||||
"hotgo/internal/model/entity"
|
||||
"hotgo/internal/service"
|
||||
)
|
||||
|
||||
func init() {
|
||||
jobList = append(jobList, ServeLog)
|
||||
}
|
||||
|
||||
// ServeLog 登录日志
|
||||
var ServeLog = &qServeLog{}
|
||||
|
||||
type qServeLog struct{}
|
||||
|
||||
// getTopic 主题
|
||||
func (q *qServeLog) getTopic() string {
|
||||
return consts.QueueServeLogTopic
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
return service.SysServeLog().RealWrite(ctx, data)
|
||||
}
|
||||
@@ -8,40 +8,32 @@ package queues
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/library/queue"
|
||||
"hotgo/internal/model/entity"
|
||||
"hotgo/internal/service"
|
||||
)
|
||||
|
||||
// SysLog 系统日志
|
||||
var SysLog = &qSysLog{topic: consts.QueueLogTopic}
|
||||
func init() {
|
||||
jobList = append(jobList, SysLog)
|
||||
}
|
||||
|
||||
type qSysLog struct {
|
||||
topic string
|
||||
// SysLog 系统日志
|
||||
var SysLog = &qSysLog{}
|
||||
|
||||
type qSysLog struct{}
|
||||
|
||||
// getTopic 主题
|
||||
func (q *qSysLog) getTopic() string {
|
||||
return consts.QueueLogTopic
|
||||
}
|
||||
|
||||
// handle 处理消息
|
||||
func (q *qSysLog) handle(ctx context.Context, mqMsg queue.MqMsg) (err error) {
|
||||
return service.SysLog().QueueJob(ctx, mqMsg)
|
||||
}
|
||||
|
||||
// Listen 监听
|
||||
func (q *qSysLog) Listen(ctx context.Context) {
|
||||
consumer, err := queue.InstanceConsumer()
|
||||
if err != nil {
|
||||
queue.FatalLog(ctx, "InstanceConsumer "+q.topic+"异常:", err)
|
||||
return
|
||||
var data entity.SysLog
|
||||
if err = json.Unmarshal(mqMsg.Body, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 全局日志
|
||||
if listenErr := consumer.ListenReceiveMsgDo(q.topic, func(mqMsg queue.MqMsg) {
|
||||
err = q.handle(ctx, mqMsg)
|
||||
|
||||
// 记录队列日志
|
||||
queue.ConsumerLog(ctx, q.topic, mqMsg, err)
|
||||
|
||||
}); listenErr != nil {
|
||||
queue.FatalLog(ctx, "队列:"+q.topic+" 监听失败", listenErr)
|
||||
}
|
||||
|
||||
return service.SysLog().RealWrite(ctx, data)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user