2023-07-20 18:01:10 +08:00
|
|
|
|
// Package tcpclient
|
|
|
|
|
// @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
|
2023-05-10 23:54:50 +08:00
|
|
|
|
package tcpclient
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
2023-07-20 18:01:10 +08:00
|
|
|
|
"github.com/gogf/gf/v2/os/gctx"
|
2023-05-10 23:54:50 +08:00
|
|
|
|
"hotgo/internal/library/network/tcp"
|
|
|
|
|
"hotgo/internal/service"
|
|
|
|
|
"hotgo/utility/simple"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// tcp客户端
|
|
|
|
|
type sCronClient struct {
|
|
|
|
|
client *tcp.Client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
service.RegisterCronClient(newCronClient())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newCronClient() *sCronClient {
|
|
|
|
|
return &sCronClient{}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-20 18:01:10 +08:00
|
|
|
|
// Instance 获取实例
|
|
|
|
|
func (s *sCronClient) Instance() *tcp.Client {
|
|
|
|
|
return s.client
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-10 23:54:50 +08:00
|
|
|
|
// Start 启动服务
|
|
|
|
|
func (s *sCronClient) Start(ctx context.Context) {
|
|
|
|
|
g.Log().Debug(ctx, "CronClient start..")
|
|
|
|
|
|
|
|
|
|
config, err := service.SysConfig().GetLoadTCP(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
g.Log().Errorf(ctx, "CronClient start fail:%+v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-11 15:53:51 +08:00
|
|
|
|
if config == nil || config.Client == nil || config.Client.Cron == nil {
|
2023-05-10 23:54:50 +08:00
|
|
|
|
g.Log().Errorf(ctx, "CronClient config is invalid")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-20 18:01:10 +08:00
|
|
|
|
// 创建客户端配置
|
|
|
|
|
clientConfig := &tcp.ClientConfig{
|
|
|
|
|
Addr: config.Client.Cron.Address,
|
|
|
|
|
AutoReconnect: true,
|
|
|
|
|
Auth: &tcp.AuthMeta{
|
|
|
|
|
Name: config.Client.Cron.Name,
|
|
|
|
|
Group: config.Client.Cron.Group,
|
|
|
|
|
AppId: config.Client.Cron.AppId,
|
|
|
|
|
SecretKey: config.Client.Cron.SecretKey,
|
|
|
|
|
},
|
|
|
|
|
LoginEvent: s.onLoginEvent,
|
|
|
|
|
CloseEvent: s.onCloseEvent,
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-10 23:54:50 +08:00
|
|
|
|
simple.SafeGo(ctx, func(ctx context.Context) {
|
2023-07-20 18:01:10 +08:00
|
|
|
|
s.client = tcp.NewClient(clientConfig)
|
2023-05-10 23:54:50 +08:00
|
|
|
|
|
2023-07-20 18:01:10 +08:00
|
|
|
|
// 注册RPC路由
|
|
|
|
|
s.client.RegisterRPCRouter(
|
2023-11-25 18:36:11 +08:00
|
|
|
|
s.OnCronDelete, // 删除任务
|
|
|
|
|
s.OnCronEdit, // 编辑任务
|
|
|
|
|
s.OnCronStatus, // 修改任务状态
|
|
|
|
|
s.OnCronOnlineExec, // 执行一次任务
|
|
|
|
|
s.OnCronDispatchLog, // 查看调度日志
|
2023-07-20 18:01:10 +08:00
|
|
|
|
)
|
2023-05-10 23:54:50 +08:00
|
|
|
|
|
2023-07-20 18:01:10 +08:00
|
|
|
|
// 注册拦截器
|
|
|
|
|
s.client.RegisterInterceptor(s.DefaultInterceptor)
|
2023-05-10 23:54:50 +08:00
|
|
|
|
|
|
|
|
|
if err = s.client.Start(); err != nil {
|
|
|
|
|
g.Log().Errorf(ctx, "CronClient Start fail:%+v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stop 停止服务
|
|
|
|
|
func (s *sCronClient) Stop(ctx context.Context) {
|
2023-05-15 18:37:40 +08:00
|
|
|
|
if s.client != nil && !s.client.IsStop() {
|
2023-05-10 23:54:50 +08:00
|
|
|
|
s.client.Stop()
|
|
|
|
|
g.Log().Debug(ctx, "CronClient stop..")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// onLoginEvent 登录认证成功事件
|
|
|
|
|
func (s *sCronClient) onLoginEvent() {
|
2023-07-20 18:01:10 +08:00
|
|
|
|
g.Log().Debug(gctx.New(), "CronClient login succeed.")
|
2023-05-10 23:54:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// onCloseEvent 连接关闭回调事件
|
|
|
|
|
func (s *sCronClient) onCloseEvent() {
|
2023-07-20 18:01:10 +08:00
|
|
|
|
g.Log().Debug(gctx.New(), "CronClient closed.")
|
2023-05-10 23:54:50 +08:00
|
|
|
|
}
|