You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gojobs/jobs_gorm.go

114 lines
3.3 KiB

2 years ago
package gojobs
2 years ago
import (
2 years ago
"context"
"errors"
"fmt"
2 years ago
"go.dtapp.net/dorm"
2 years ago
"go.dtapp.net/goarray"
"go.dtapp.net/goip"
"go.dtapp.net/gojobs/jobs_gorm_model"
2 years ago
"go.dtapp.net/golock"
2 years ago
"log"
2 years ago
"runtime"
2 years ago
)
2 years ago
2 years ago
type JobsGormConfig struct {
2 years ago
GormClient *dorm.GormClient // 数据库驱动
RedisClient *dorm.RedisClient // 缓存数据库驱动
CurrentIp string // 当前ip
LockKeyPrefix string // 锁Key前缀 xxx_lock
LockKeySeparator string // 锁Key分隔符 :
CornKeyPrefix string // 任务Key前缀 xxx_cron
CornKeyCustom string // 任务Key自定义 xxx_cron_自定义 xxx_cron_自定义_*
Debug bool // 调试
2 years ago
}
2 years ago
// JobsGorm Gorm数据库驱动
type JobsGorm struct {
2 years ago
gormClient *dorm.GormClient // 数据库驱动
redisClient *dorm.RedisClient // 缓存驱动
lockClient *golock.LockRedis // 锁驱动
config struct {
2 years ago
debug bool // 调试
runVersion string // 运行版本
os string // 系统类型
arch string // 系统架构
maxProCs int // CPU核数
version string // GO版本
macAddrS string // Mac地址
insideIp string // 内网ip
outsideIp string // 外网ip
lockKeyPrefix string // 锁Key前缀 xxx_lock
lockKeySeparator string // 锁Key分隔符 :
cornKeyPrefix string // 任务Key前缀 xxx_cron
cornKeyCustom string // 任务Key自定义
}
2 years ago
}
2 years ago
// NewJobsGorm 初始化
2 years ago
func NewJobsGorm(config *JobsGormConfig) (*JobsGorm, error) {
2 years ago
2 years ago
// 判断
2 years ago
if config.LockKeyPrefix == "" {
2 years ago
return nil, errors.New("需要配置锁Key前缀")
2 years ago
}
2 years ago
if config.LockKeySeparator == "" {
return nil, errors.New("需要配置锁Key分隔符")
2 years ago
}
2 years ago
if config.CornKeyPrefix == "" {
return nil, errors.New("需要配置任务Key前缀")
}
if config.CornKeyCustom == "" {
return nil, errors.New("需要配置任务Key自定义")
2 years ago
}
if config.CurrentIp == "" {
2 years ago
return nil, errors.New("需要配置当前的IP")
}
2 years ago
if config.GormClient == nil {
2 years ago
return nil, errors.New("需要配置数据库驱动")
2 years ago
}
2 years ago
if config.RedisClient == nil {
return nil, errors.New("需要配置缓存数据库驱动")
}
c := &JobsGorm{}
c.gormClient = config.GormClient
c.redisClient = config.RedisClient
c.config.outsideIp = config.CurrentIp
2 years ago
c.config.lockKeyPrefix = config.LockKeyPrefix
c.config.lockKeySeparator = config.LockKeySeparator
c.config.cornKeyPrefix = config.CornKeyPrefix
c.config.cornKeyCustom = config.CornKeyCustom
2 years ago
c.config.debug = config.Debug
// 锁
2 years ago
c.lockClient = golock.NewLockRedis(c.redisClient)
2 years ago
2 years ago
// 配置信息
2 years ago
c.config.runVersion = Version
c.config.os = runtime.GOOS
c.config.arch = runtime.GOARCH
c.config.maxProCs = runtime.GOMAXPROCS(0)
c.config.version = runtime.Version()
2 years ago
c.config.macAddrS = goarray.TurnString(goip.GetMacAddr(context.Background()))
c.config.insideIp = goip.GetInsideIp(context.Background())
2 years ago
2 years ago
// 创建模型
err := c.gormClient.Db.AutoMigrate(
2 years ago
&jobs_gorm_model.Task{},
&jobs_gorm_model.TaskLog{},
&jobs_gorm_model.TaskLogRun{},
&jobs_gorm_model.TaskIp{},
)
if err != nil {
return nil, errors.New(fmt.Sprintf("创建任务模型失败:%v\n", err))
2 years ago
}
2 years ago
2 years ago
if c.config.debug == true {
log.Printf("JOBS配置%+v\n", c.config)
2 years ago
}
2 years ago
return c, nil
2 years ago
}