Files
bl/common/cool/func.go
昔念 aa53001982
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
```
refactor(cool): 移除Redis监听功能和用户结构体定义

移除ListenFunc函数,该函数提供Redis PubSub监听功能,
包括自动重连和心跳保活机制。同时删除User结构体定义和
相关有序集合变量,这些功能将由rpc模块替代实现。

feat(rpc): 添加对ListenFunc的调用以处理Redis监听

在login模块中
2026-03-04 23:38:21 +08:00

66 lines
1.8 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 cool
import (
"time"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/text/gstr"
)
type CoolFunc interface {
// Func handler
Func(ctx g.Ctx, param string) (err error)
// IsSingleton 是否单例,当为true时只能有一个任务在执行,在注意函数为计划任务时使用
IsSingleton() bool
// IsAllWorker 是否所有worker都执行
IsAllWorker() bool
}
// FuncMap 函数列表
var FuncMap = make(map[string]CoolFunc)
// RegisterFunc 注册函数
func RegisterFunc(name string, f CoolFunc) {
FuncMap[name] = f
}
// GetFunc 获取函数
func GetFunc(name string) CoolFunc {
return FuncMap[name]
}
// RunFunc 运行函数
func RunFunc(ctx g.Ctx, funcstring string) (err error) {
funcName := gstr.SubStr(funcstring, 0, gstr.Pos(funcstring, "("))
funcParam := gstr.SubStr(funcstring, gstr.Pos(funcstring, "(")+1, gstr.Pos(funcstring, ")")-gstr.Pos(funcstring, "(")-1)
if _, ok := FuncMap[funcName]; !ok {
err = gerror.New("函数不存在:" + funcName)
return
}
if !FuncMap[funcName].IsAllWorker() {
// 检查当前是否为主进程, 如果不是主进程, 则不执行
if ProcessFlag != CacheManager.MustGetOrSet(ctx, "cool:masterflag", ProcessFlag, 60*time.Second).String() {
Logger.Debug(ctx, "当前进程不是主进程, 不执行单例函数", funcName)
return
}
}
err = FuncMap[funcName].Func(ctx, funcParam)
return
}
// ClusterRunFunc 集群运行函数,如果是单机模式, 则直接运行函数
func ClusterRunFunc(ctx g.Ctx, funcstring string) (err error) {
if IsRedisMode {
conn, err := g.Redis("cool").Conn(ctx)
if err != nil {
return err
}
defer conn.Close(ctx)
_, err = conn.Do(ctx, "publish", "cool:func", funcstring)
return err
} else {
return RunFunc(ctx, funcstring)
}
}