From 23176056ac1eb98833bcbf596a4a28f09e54ee94 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Wed, 24 Aug 2022 09:53:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=AE=9A=E6=97=B6=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- comm/const.go | 1 + modules/timer/forum.go | 33 ++++++++++++++++++++++++++++++ modules/timer/module.go | 45 +++++++++++++++++++++++++++++++++++++++++ services/mainte/main.go | 11 ++++++++++ 4 files changed, 90 insertions(+) create mode 100644 modules/timer/forum.go create mode 100644 modules/timer/module.go diff --git a/comm/const.go b/comm/const.go index 87f4b0268..a94da3b91 100644 --- a/comm/const.go +++ b/comm/const.go @@ -54,6 +54,7 @@ const ( ModuleGourmet core.M_Modules = "gourmet" //美食馆 ModuleRtask core.M_Modules = "rtask" //随机任务 ModuleSmithy core.M_Modules = "smithy" //铁匠铺 + ModuleTimer core.M_Modules = "timer" //定时任务模块 ) //数据表名定义处 diff --git a/modules/timer/forum.go b/modules/timer/forum.go new file mode 100644 index 000000000..213c47cd6 --- /dev/null +++ b/modules/timer/forum.go @@ -0,0 +1,33 @@ +package timer + +import ( + "go_dreamfactory/modules" + + "go_dreamfactory/lego/core" + "go_dreamfactory/lego/sys/cron" +) + +/* +装备模块 API +*/ +type forumComp struct { + modules.MCompGate + service core.IService +} + +//组件初始化接口 +func (this *forumComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { + this.MCompGate.Init(service, module, comp, options) + this.service = service + return +} + +func (this *forumComp) Start() (err error) { + err = this.MCompGate.Start() + cron.AddFunc("*/5 * * * * ?", this.Timer) //每五秒执行一次 + return +} + +func (this *forumComp) Timer() { + +} diff --git a/modules/timer/module.go b/modules/timer/module.go new file mode 100644 index 000000000..71a3a94d6 --- /dev/null +++ b/modules/timer/module.go @@ -0,0 +1,45 @@ +package timer + +import ( + "go_dreamfactory/comm" + "go_dreamfactory/lego/base" + "go_dreamfactory/lego/core" + "go_dreamfactory/lego/core/cbase" +) + +/* +模块名:定时任务 +描述:处理区服集群下需要做的一些定时任务 +开发:李伟 +*/ +func NewModule() core.IModule { + m := new(Timer) + return m +} + +type Timer struct { + cbase.ModuleBase + service base.IRPCXService //rpc服务对象 通过这个对象可以发布服务和调用其他服务的接口 +} + +//模块名 +func (this *Timer) GetType() core.M_Modules { + return comm.ModuleTimer +} + +//模块初始化接口 注册用户创建角色事件 +func (this *Timer) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { + err = this.ModuleBase.Init(service, module, options) + this.service = service.(base.IRPCXService) + return +} + +func (this *Timer) Start() (err error) { + err = this.ModuleBase.Start() + return +} + +//装备组件 +func (this *Timer) OnInstallComp() { + this.ModuleBase.OnInstallComp() +} diff --git a/services/mainte/main.go b/services/mainte/main.go index f2f047264..fbef66697 100644 --- a/services/mainte/main.go +++ b/services/mainte/main.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "go_dreamfactory/modules/mgolog" + "go_dreamfactory/modules/timer" "go_dreamfactory/modules/web" "go_dreamfactory/services" "go_dreamfactory/sys/db" @@ -11,6 +12,7 @@ import ( "go_dreamfactory/lego" "go_dreamfactory/lego/base/rpcx" "go_dreamfactory/lego/core" + "go_dreamfactory/lego/sys/cron" "go_dreamfactory/lego/sys/log" ) @@ -35,6 +37,7 @@ func main() { lego.Run(s, //运行模块 mgolog.NewModule(), web.NewModule(), + timer.NewModule(), ) } @@ -52,6 +55,14 @@ type Service struct { //初始化worker需要的一些系统工具 func (this *Service) InitSys() { this.ServiceBase.InitSys() + + //定时系统 + if err := cron.OnInit(nil); err != nil { + panic(fmt.Sprintf("init sys.cron err: %s", err.Error())) + } else { + log.Infof("init sys.cron success!") + } + //存储系统 if err := db.OnInit(this.GetSettings().Sys["db"]); err != nil { panic(fmt.Sprintf("init sys.db err: %s", err.Error()))