mirror of
https://github.com/injoyai/tdx.git
synced 2025-11-26 21:25:35 +08:00
增加manage,方便快速搭建项目,试用
This commit is contained in:
74
manage.go
Normal file
74
manage.go
Normal file
@@ -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 //数据位置
|
||||
}
|
||||
Reference in New Issue
Block a user