From cee5270dfca366766c31b98eec716098eb63f975 Mon Sep 17 00:00:00 2001 From: injoyai <1113655791@qq.com> Date: Fri, 21 Feb 2025 15:08:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0manage,=E6=96=B9=E4=BE=BF?= =?UTF-8?q?=E5=BF=AB=E9=80=9F=E6=90=AD=E5=BB=BA=E9=A1=B9=E7=9B=AE,?= =?UTF-8?q?=E8=AF=95=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manage.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 manage.go diff --git a/manage.go b/manage.go new file mode 100644 index 0000000..22e468b --- /dev/null +++ b/manage.go @@ -0,0 +1,74 @@ +package tdx + +import ( + "github.com/injoyai/ios/client" + "github.com/robfig/cron/v3" + "path/filepath" +) + +func NewManage(cfg *ManageConfig, op ...client.Option) (*Manage, error) { + //初始化配置 + if len(cfg.Hosts) == 0 { + cfg.Hosts = Hosts + } + if cfg.Database == "" { + cfg.Database = "./data/" + } + + //连接池 + p, err := NewPool(func() (*Client, error) { + return DialHosts(cfg.Hosts, op...) + }, cfg.Number) + if err != nil { + return nil, err + } + + //代码 + codesClient, err := DialHosts(cfg.Hosts, op...) + if err != nil { + return nil, err + } + codes, err := NewCodes(codesClient, filepath.Join(cfg.Database, "database/codes.db")) + if err != nil { + return nil, err + } + + ///工作日 + workdayClient, err := DialHosts(cfg.Hosts, op...) + if err != nil { + return nil, err + } + workday, err := NewWorkday(workdayClient, filepath.Join(cfg.Database, "database/codes.db")) + if err != nil { + return nil, err + } + + return &Manage{ + Pool: p, + Codes: codes, + Workday: workday, + Cron: cron.New(cron.WithSeconds()), + }, nil +} + +type Manage struct { + *Pool + Codes *Codes + Workday *Workday + Cron *cron.Cron +} + +// AddWorkdayTask 添加工作日任务 +func (this *Manage) AddWorkdayTask(spec string, f func(m *Manage)) { + this.Cron.AddFunc(spec, func() { + if this.Workday.TodayIs() { + f(this) + } + }) +} + +type ManageConfig struct { + Hosts []string //服务端IP + Number int //客户端数量 + Database string //数据位置 +}