parent
f95054baa8
commit
4d2e183b47
@ -0,0 +1,45 @@
|
|||||||
|
package gojobs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/jasonlvhit/gocron"
|
||||||
|
"log"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCron1(t *testing.T) {
|
||||||
|
|
||||||
|
// 创建一个cron实例 精确到秒
|
||||||
|
crontab := NewCron()
|
||||||
|
|
||||||
|
log.Println(crontab)
|
||||||
|
|
||||||
|
err := crontab.AddJobByFunc("1", "*/1 * * * * *", func() {
|
||||||
|
log.Println("哈哈哈哈")
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("添加任务时出错:%s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = crontab.AddJobByFunc("2", "*/2 * * * * *", func() {
|
||||||
|
log.Println("啊啊啊啊")
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("添加任务时出错:%s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
crontab.Start()
|
||||||
|
select {}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCron2(t *testing.T) {
|
||||||
|
i := 0
|
||||||
|
s := gocron.NewScheduler()
|
||||||
|
s.Every(5).Seconds().Do(func() {
|
||||||
|
i++
|
||||||
|
log.Println("execute per 5 seconds", i)
|
||||||
|
})
|
||||||
|
<-s.Start()
|
||||||
|
}
|
@ -1,70 +1,25 @@
|
|||||||
package gojobs
|
package gojobs
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CodeAbnormal = 0 // 异常
|
TASK_IN = "IN" // 任务运行
|
||||||
CodeError = http.StatusInternalServerError // 失败
|
TASK_SUCCESS = "SUCCESS" // 任务完成
|
||||||
CodeSuccess = http.StatusOK // 成功
|
TASK_ERROR = "ERROR" // 任务异常
|
||||||
CodeEnd = http.StatusCreated // 结束
|
TASK_TIMEOUT = "TIMEOUT" // 任务超时
|
||||||
|
TASK_WAIT = "WAIT" // 任务等待
|
||||||
)
|
)
|
||||||
|
|
||||||
// 每隔n秒执行一次
|
// Cron
|
||||||
const specSeconds = "*/%d * * * * *"
|
type jobs interface {
|
||||||
|
// Run 运行
|
||||||
// GetSpecSeconds 每隔n秒执行一次
|
Run()
|
||||||
var GetSpecSeconds = func(n int64) string {
|
// RunAddLog 任务执行日志
|
||||||
if n < 0 && n > 59 {
|
RunAddLog()
|
||||||
return ""
|
// CreateInCustomId 创建正在运行任务
|
||||||
}
|
CreateInCustomId()
|
||||||
return fmt.Sprintf(specSeconds, n)
|
// CreateInCustomIdOnly 创建正在运行唯一任务
|
||||||
}
|
CreateInCustomIdOnly()
|
||||||
|
// CreateInCustomIdMaxNumber 创建正在运行任务并限制数量
|
||||||
// GetFrequencySeconds 每隔n秒执行一次
|
CreateInCustomIdMaxNumber()
|
||||||
var GetFrequencySeconds = func(n int64) int64 {
|
// CreateInCustomIdMaxNumberOnly 创建正在运行唯一任务并限制数量
|
||||||
if n < 0 && n > 59 {
|
CreateInCustomIdMaxNumberOnly()
|
||||||
return -1
|
|
||||||
}
|
|
||||||
return n
|
|
||||||
}
|
|
||||||
|
|
||||||
// 每隔n分钟执行一次
|
|
||||||
const specMinutes = "0 */%d * * * *"
|
|
||||||
|
|
||||||
// GetSpecMinutes 每隔n分钟执行一次
|
|
||||||
var GetSpecMinutes = func(n int64) string {
|
|
||||||
if n < 0 && n > 59 {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return fmt.Sprintf(specMinutes, n)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetFrequencyMinutes 每隔n分钟执行一次
|
|
||||||
var GetFrequencyMinutes = func(n int64) int64 {
|
|
||||||
if n < 0 && n > 59 {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
return n * 60
|
|
||||||
}
|
|
||||||
|
|
||||||
// 每天n点执行一次
|
|
||||||
const specHour = "0 0 */%d * * *"
|
|
||||||
|
|
||||||
// GetSpecHour 每天n点执行一次
|
|
||||||
var GetSpecHour = func(n int64) string {
|
|
||||||
if n < 0 && n > 23 {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return fmt.Sprintf(specHour, n)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetFrequencyHour 每天n点执行一次
|
|
||||||
var GetFrequencyHour = func(n int64) int64 {
|
|
||||||
if n < 0 && n > 23 {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
return n * 60 * 60
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +0,0 @@
|
|||||||
package jobs_common
|
|
||||||
|
|
||||||
const (
|
|
||||||
TASK_IN = "IN" // 任务运行
|
|
||||||
TASK_SUCCESS = "SUCCESS" // 任务完成
|
|
||||||
TASK_ERROR = "ERROR" // 任务异常
|
|
||||||
TASK_TIMEOUT = "TIMEOUT" // 任务超时
|
|
||||||
TASK_WAIT = "WAIT" // 任务等待
|
|
||||||
)
|
|
@ -1,18 +0,0 @@
|
|||||||
package jobs_gorm
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestSpec(t *testing.T) {
|
|
||||||
t.Log(GetSpecSeconds(10))
|
|
||||||
t.Log(GetFrequencySeconds(10))
|
|
||||||
|
|
||||||
t.Log(GetSpecMinutes(1))
|
|
||||||
t.Log(GetFrequencyMinutes(1))
|
|
||||||
t.Log(GetSpecMinutes(10))
|
|
||||||
t.Log(GetFrequencyMinutes(10))
|
|
||||||
t.Log(GetSpecMinutes(30))
|
|
||||||
t.Log(GetFrequencyMinutes(30))
|
|
||||||
|
|
||||||
t.Log(GetSpecHour(10))
|
|
||||||
t.Log(GetFrequencyHour(10))
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
package jobs_gorm
|
|
||||||
|
|
||||||
import (
|
|
||||||
"go.dtapp.net/goip"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
// RefreshIp 刷新Ip
|
|
||||||
func (jobsGorm *JobsGorm) RefreshIp(tx *gorm.DB) {
|
|
||||||
xip := goip.GetOutsideIp()
|
|
||||||
if jobsGorm.outsideIp == "" || jobsGorm.outsideIp == "0.0.0.0" {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if jobsGorm.outsideIp == xip {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
tx.Where("ips = ?", jobsGorm.outsideIp).Delete(&TaskIp{}) // 删除
|
|
||||||
jobsGorm.outsideIp = xip
|
|
||||||
}
|
|
@ -1,286 +0,0 @@
|
|||||||
package jobs_gorm
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"go.dtapp.net/goarray"
|
|
||||||
"go.dtapp.net/goip"
|
|
||||||
"go.dtapp.net/gojobs/jobs_common"
|
|
||||||
"go.dtapp.net/goredis"
|
|
||||||
"go.dtapp.net/gotime"
|
|
||||||
"go.dtapp.net/gouuid"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
"log"
|
|
||||||
"runtime"
|
|
||||||
)
|
|
||||||
|
|
||||||
// JobsGorm 任务
|
|
||||||
type JobsGorm struct {
|
|
||||||
runVersion string // 运行版本
|
|
||||||
os string // 系统类型
|
|
||||||
arch string // 系统架构
|
|
||||||
maxProCs int // CPU核数
|
|
||||||
version string // GO版本
|
|
||||||
macAddrS string // Mac地址
|
|
||||||
insideIp string // 内网ip
|
|
||||||
outsideIp string // 外网ip
|
|
||||||
mainService int // 主要服务
|
|
||||||
Db *gorm.DB // 数据库
|
|
||||||
Redis *goredis.Client // 缓存数据库服务
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewGorm 任务
|
|
||||||
func NewGorm(jobsGorm JobsGorm, mainService int, runVersion string) *JobsGorm {
|
|
||||||
jobsGorm.runVersion = runVersion
|
|
||||||
jobsGorm.os = runtime.GOOS
|
|
||||||
jobsGorm.arch = runtime.GOARCH
|
|
||||||
jobsGorm.maxProCs = runtime.GOMAXPROCS(0)
|
|
||||||
jobsGorm.version = runtime.Version()
|
|
||||||
jobsGorm.macAddrS = goarray.TurnString(goip.GetMacAddr())
|
|
||||||
jobsGorm.insideIp = goip.GetInsideIp()
|
|
||||||
jobsGorm.outsideIp = goip.GetOutsideIp()
|
|
||||||
jobsGorm.mainService = mainService
|
|
||||||
return &jobsGorm
|
|
||||||
}
|
|
||||||
|
|
||||||
// ConfigCreateInCustomId 创建正在运行任务
|
|
||||||
type ConfigCreateInCustomId struct {
|
|
||||||
Tx *gorm.DB // 驱动
|
|
||||||
Params string // 参数
|
|
||||||
Frequency int64 // 频率(秒单位)
|
|
||||||
CustomId string // 自定义编号
|
|
||||||
CustomSequence int64 // 自定义顺序
|
|
||||||
Type string // 类型
|
|
||||||
SpecifyIp string // 指定外网IP
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateInCustomId 创建正在运行任务
|
|
||||||
func (jobsGorm *JobsGorm) CreateInCustomId(config *ConfigCreateInCustomId) error {
|
|
||||||
createStatus := config.Tx.Create(&Task{
|
|
||||||
Status: jobs_common.TASK_IN,
|
|
||||||
Params: config.Params,
|
|
||||||
StatusDesc: "首次添加任务",
|
|
||||||
Frequency: config.Frequency,
|
|
||||||
RunId: gouuid.GetUuId(),
|
|
||||||
CustomId: config.CustomId,
|
|
||||||
CustomSequence: config.CustomSequence,
|
|
||||||
Type: config.Type,
|
|
||||||
CreatedIp: jobsGorm.outsideIp,
|
|
||||||
SpecifyIp: config.SpecifyIp,
|
|
||||||
UpdatedIp: jobsGorm.outsideIp,
|
|
||||||
})
|
|
||||||
if createStatus.RowsAffected == 0 {
|
|
||||||
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ConfigCreateInCustomIdOnly 创建正在运行唯一任务
|
|
||||||
type ConfigCreateInCustomIdOnly struct {
|
|
||||||
Tx *gorm.DB // 驱动
|
|
||||||
Params string // 参数
|
|
||||||
Frequency int64 // 频率(秒单位)
|
|
||||||
CustomId string // 自定义编号
|
|
||||||
CustomSequence int64 // 自定义顺序
|
|
||||||
Type string // 类型
|
|
||||||
SpecifyIp string // 指定外网IP
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateInCustomIdOnly 创建正在运行唯一任务
|
|
||||||
func (jobsGorm *JobsGorm) CreateInCustomIdOnly(config *ConfigCreateInCustomIdOnly) error {
|
|
||||||
query := jobsGorm.TaskTypeTakeIn(config.Tx, config.CustomId, config.Type)
|
|
||||||
if query.Id != 0 {
|
|
||||||
return errors.New(fmt.Sprintf("%d:[%s@%s]任务已存在", query.Id, config.CustomId, config.Type))
|
|
||||||
}
|
|
||||||
createStatus := config.Tx.Create(&Task{
|
|
||||||
Status: jobs_common.TASK_IN,
|
|
||||||
Params: config.Params,
|
|
||||||
StatusDesc: "首次添加任务",
|
|
||||||
Frequency: config.Frequency,
|
|
||||||
RunId: gouuid.GetUuId(),
|
|
||||||
CustomId: config.CustomId,
|
|
||||||
CustomSequence: config.CustomSequence,
|
|
||||||
Type: config.Type,
|
|
||||||
CreatedIp: jobsGorm.outsideIp,
|
|
||||||
SpecifyIp: config.SpecifyIp,
|
|
||||||
UpdatedIp: jobsGorm.outsideIp,
|
|
||||||
})
|
|
||||||
if createStatus.RowsAffected == 0 {
|
|
||||||
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ConfigCreateInCustomIdMaxNumber 创建正在运行任务并限制数量
|
|
||||||
type ConfigCreateInCustomIdMaxNumber struct {
|
|
||||||
Tx *gorm.DB // 驱动
|
|
||||||
Params string // 参数
|
|
||||||
Frequency int64 // 频率(秒单位)
|
|
||||||
MaxNumber int64 // 最大次数
|
|
||||||
CustomId string // 自定义编号
|
|
||||||
CustomSequence int64 // 自定义顺序
|
|
||||||
Type string // 类型
|
|
||||||
SpecifyIp string // 指定外网IP
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateInCustomIdMaxNumber 创建正在运行任务并限制数量
|
|
||||||
func (jobsGorm *JobsGorm) CreateInCustomIdMaxNumber(config *ConfigCreateInCustomIdMaxNumber) error {
|
|
||||||
createStatus := config.Tx.Create(&Task{
|
|
||||||
Status: jobs_common.TASK_IN,
|
|
||||||
Params: config.Params,
|
|
||||||
StatusDesc: "首次添加任务",
|
|
||||||
Frequency: config.Frequency,
|
|
||||||
MaxNumber: config.MaxNumber,
|
|
||||||
RunId: gouuid.GetUuId(),
|
|
||||||
CustomId: config.CustomId,
|
|
||||||
CustomSequence: config.CustomSequence,
|
|
||||||
Type: config.Type,
|
|
||||||
CreatedIp: jobsGorm.outsideIp,
|
|
||||||
SpecifyIp: config.SpecifyIp,
|
|
||||||
UpdatedIp: jobsGorm.outsideIp,
|
|
||||||
})
|
|
||||||
if createStatus.RowsAffected == 0 {
|
|
||||||
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ConfigCreateInCustomIdMaxNumberOnly 创建正在运行唯一任务并限制数量
|
|
||||||
type ConfigCreateInCustomIdMaxNumberOnly struct {
|
|
||||||
Tx *gorm.DB // 驱动
|
|
||||||
Params string // 参数
|
|
||||||
Frequency int64 // 频率(秒单位)
|
|
||||||
MaxNumber int64 // 最大次数
|
|
||||||
CustomId string // 自定义编号
|
|
||||||
CustomSequence int64 // 自定义顺序
|
|
||||||
Type string // 类型
|
|
||||||
SpecifyIp string // 指定外网IP
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateInCustomIdMaxNumberOnly 创建正在运行唯一任务并限制数量
|
|
||||||
func (jobsGorm *JobsGorm) CreateInCustomIdMaxNumberOnly(config *ConfigCreateInCustomIdMaxNumberOnly) error {
|
|
||||||
query := jobsGorm.TaskTypeTakeIn(config.Tx, config.CustomId, config.Type)
|
|
||||||
if query.Id != 0 {
|
|
||||||
return errors.New(fmt.Sprintf("%d:[%s@%s]任务已存在", query.Id, config.CustomId, config.Type))
|
|
||||||
}
|
|
||||||
createStatus := config.Tx.Create(&Task{
|
|
||||||
Status: jobs_common.TASK_IN,
|
|
||||||
Params: config.Params,
|
|
||||||
StatusDesc: "首次添加任务",
|
|
||||||
Frequency: config.Frequency,
|
|
||||||
MaxNumber: config.MaxNumber,
|
|
||||||
RunId: gouuid.GetUuId(),
|
|
||||||
CustomId: config.CustomId,
|
|
||||||
CustomSequence: config.CustomSequence,
|
|
||||||
Type: config.Type,
|
|
||||||
CreatedIp: jobsGorm.outsideIp,
|
|
||||||
SpecifyIp: config.SpecifyIp,
|
|
||||||
UpdatedIp: jobsGorm.outsideIp,
|
|
||||||
})
|
|
||||||
if createStatus.RowsAffected == 0 {
|
|
||||||
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// RunAddLog 任务执行日志
|
|
||||||
func (jobsGorm *JobsGorm) RunAddLog(tx *gorm.DB, id uint, runId string) *gorm.DB {
|
|
||||||
return tx.Create(&TaskLogRun{
|
|
||||||
TaskId: id,
|
|
||||||
RunId: runId,
|
|
||||||
InsideIp: jobsGorm.insideIp,
|
|
||||||
OutsideIp: jobsGorm.outsideIp,
|
|
||||||
Os: jobsGorm.os,
|
|
||||||
Arch: jobsGorm.arch,
|
|
||||||
Gomaxprocs: jobsGorm.maxProCs,
|
|
||||||
GoVersion: jobsGorm.version,
|
|
||||||
MacAddrs: jobsGorm.macAddrS,
|
|
||||||
CreatedAt: gotime.Current().Format(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run 任务执行
|
|
||||||
func (jobsGorm *JobsGorm) Run(tx *gorm.DB, info Task, status int, desc string) {
|
|
||||||
// 请求函数记录
|
|
||||||
statusCreate := tx.Create(&TaskLog{
|
|
||||||
TaskId: info.Id,
|
|
||||||
StatusCode: status,
|
|
||||||
Desc: desc,
|
|
||||||
Version: jobsGorm.runVersion,
|
|
||||||
CreatedAt: gotime.Current().Format(),
|
|
||||||
})
|
|
||||||
if statusCreate.RowsAffected == 0 {
|
|
||||||
log.Println("statusCreate", statusCreate.Error)
|
|
||||||
}
|
|
||||||
if status == 0 {
|
|
||||||
statusEdit := jobsGorm.EditTask(tx, info.Id).Select("run_id").Updates(Task{
|
|
||||||
RunId: gouuid.GetUuId(),
|
|
||||||
})
|
|
||||||
if statusEdit.RowsAffected == 0 {
|
|
||||||
log.Println("statusEdit", statusEdit.Error)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// 任务
|
|
||||||
if status == CodeSuccess {
|
|
||||||
// 执行成功
|
|
||||||
statusEdit := jobsGorm.EditTask(tx, info.Id).
|
|
||||||
Select("status_desc", "number", "run_id", "updated_ip", "updated_at", "result").
|
|
||||||
Updates(Task{
|
|
||||||
StatusDesc: "执行成功",
|
|
||||||
Number: info.Number + 1,
|
|
||||||
RunId: gouuid.GetUuId(),
|
|
||||||
UpdatedIp: jobsGorm.outsideIp,
|
|
||||||
UpdatedAt: gotime.Current().Format(),
|
|
||||||
Result: desc,
|
|
||||||
})
|
|
||||||
if statusEdit.RowsAffected == 0 {
|
|
||||||
log.Println("statusEdit", statusEdit.Error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if status == CodeEnd {
|
|
||||||
// 执行成功、提前结束
|
|
||||||
statusEdit := jobsGorm.EditTask(tx, info.Id).
|
|
||||||
Select("status", "status_desc", "number", "updated_ip", "updated_at", "result").
|
|
||||||
Updates(Task{
|
|
||||||
Status: jobs_common.TASK_SUCCESS,
|
|
||||||
StatusDesc: "结束执行",
|
|
||||||
Number: info.Number + 1,
|
|
||||||
UpdatedIp: jobsGorm.outsideIp,
|
|
||||||
UpdatedAt: gotime.Current().Format(),
|
|
||||||
Result: desc,
|
|
||||||
})
|
|
||||||
if statusEdit.RowsAffected == 0 {
|
|
||||||
log.Println("statusEdit", statusEdit.Error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if status == CodeError {
|
|
||||||
// 执行失败
|
|
||||||
statusEdit := jobsGorm.EditTask(tx, info.Id).
|
|
||||||
Select("status_desc", "number", "run_id", "updated_ip", "updated_at", "result").
|
|
||||||
Updates(Task{
|
|
||||||
StatusDesc: "执行失败",
|
|
||||||
Number: info.Number + 1,
|
|
||||||
RunId: gouuid.GetUuId(),
|
|
||||||
UpdatedIp: jobsGorm.outsideIp,
|
|
||||||
UpdatedAt: gotime.Current().Format(),
|
|
||||||
Result: desc,
|
|
||||||
})
|
|
||||||
if statusEdit.RowsAffected == 0 {
|
|
||||||
log.Println("statusEdit", statusEdit.Error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if info.MaxNumber != 0 {
|
|
||||||
if info.Number+1 >= info.MaxNumber {
|
|
||||||
// 关闭执行
|
|
||||||
statusEdit := jobsGorm.EditTask(tx, info.Id).
|
|
||||||
Select("status").
|
|
||||||
Updates(Task{
|
|
||||||
Status: jobs_common.TASK_TIMEOUT,
|
|
||||||
})
|
|
||||||
if statusEdit.RowsAffected == 0 {
|
|
||||||
log.Println("statusEdit", statusEdit.Error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
package jobs_gorm
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"go.dtapp.net/goredis"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Lock 上锁
|
|
||||||
func (jobsGorm *JobsGorm) Lock(info Task, id any) string {
|
|
||||||
cacheName := fmt.Sprintf("cron:%v:%v", info.Type, id)
|
|
||||||
judgeCache := jobsGorm.Redis.NewStringOperation().Get(cacheName).UnwrapOr("")
|
|
||||||
if judgeCache != "" {
|
|
||||||
return judgeCache
|
|
||||||
}
|
|
||||||
jobsGorm.Redis.NewStringOperation().Set(cacheName, fmt.Sprintf("已在%v机器上锁成功", jobsGorm.outsideIp), goredis.WithExpire(time.Millisecond*time.Duration(info.Frequency)*3))
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unlock Lock 解锁
|
|
||||||
func (jobsGorm *JobsGorm) Unlock(info Task, id any) {
|
|
||||||
cacheName := fmt.Sprintf("cron:%v:%v", info.Type, id)
|
|
||||||
jobsGorm.Redis.NewStringOperation().Del(cacheName)
|
|
||||||
}
|
|
||||||
|
|
||||||
// LockForever 永远上锁
|
|
||||||
func (jobsGorm *JobsGorm) LockForever(info Task, id any) string {
|
|
||||||
cacheName := fmt.Sprintf("cron:%v:%v", info.Type, id)
|
|
||||||
judgeCache := jobsGorm.Redis.NewStringOperation().Get(cacheName).UnwrapOr("")
|
|
||||||
if judgeCache != "" {
|
|
||||||
return judgeCache
|
|
||||||
}
|
|
||||||
jobsGorm.Redis.NewStringOperation().Set(cacheName, fmt.Sprintf("已在%v机器永远上锁成功", jobsGorm.outsideIp))
|
|
||||||
return ""
|
|
||||||
}
|
|
@ -1,158 +0,0 @@
|
|||||||
package jobs_gorm
|
|
||||||
|
|
||||||
import (
|
|
||||||
"go.dtapp.net/gojobs/jobs_common"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Task 任务
|
|
||||||
type Task struct {
|
|
||||||
Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号
|
|
||||||
Status string `gorm:"index;comment:状态码" json:"status"` // 状态码
|
|
||||||
Params string `gorm:"comment:参数" json:"params"` // 参数
|
|
||||||
ParamsType string `gorm:"comment:参数类型" json:"params_type"` // 参数类型
|
|
||||||
StatusDesc string `gorm:"comment:状态描述" json:"status_desc"` // 状态描述
|
|
||||||
Frequency int64 `gorm:"index;comment:频率(秒单位)" json:"frequency"` // 频率(秒单位)
|
|
||||||
Number int64 `gorm:"comment:当前次数" json:"number"` // 当前次数
|
|
||||||
MaxNumber int64 `gorm:"comment:最大次数" json:"max_number"` // 最大次数
|
|
||||||
RunId string `gorm:"comment:执行编号" json:"run_id"` // 执行编号
|
|
||||||
CustomId string `gorm:"index;comment:自定义编号" json:"custom_id"` // 自定义编号
|
|
||||||
CustomSequence int64 `gorm:"comment:自定义顺序" json:"custom_sequence"` // 自定义顺序
|
|
||||||
Type string `gorm:"index;comment:类型" json:"type"` // 类型
|
|
||||||
CreatedIp string `gorm:"comment:创建外网IP" json:"created_ip"` // 创建外网IP
|
|
||||||
SpecifyIp string `gorm:"comment:指定外网IP" json:"specify_ip"` // 指定外网IP
|
|
||||||
UpdatedIp string `gorm:"comment:更新外网IP" json:"updated_ip"` // 更新外网IP
|
|
||||||
Result string `gorm:"comment:结果" json:"result"` // 结果
|
|
||||||
CreatedAt string `gorm:"type:text;comment:创建时间" json:"created_at"` // 创建时间
|
|
||||||
UpdatedAt string `gorm:"type:text;comment:更新时间" json:"updated_at"` // 更新时间
|
|
||||||
DeletedAt gorm.DeletedAt `gorm:"type:text;index;comment:删除时间" json:"deleted_at"` // 删除时间
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Task) TableName() string {
|
|
||||||
return "task"
|
|
||||||
}
|
|
||||||
|
|
||||||
// TaskTake 查询单任务
|
|
||||||
func (jobsGorm *JobsGorm) TaskTake(tx *gorm.DB, customId string) (result Task) {
|
|
||||||
tx.Where("custom_id = ?", customId).Take(&result)
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
// 查询单任务
|
|
||||||
func (jobsGorm *JobsGorm) taskTake(tx *gorm.DB, customId, status string) (result Task) {
|
|
||||||
tx.Where("custom_id = ?", customId).Where("status = ?", status).Take(&result)
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
// TaskTakeIn 查询单任务 - 任务运行
|
|
||||||
func (jobsGorm *JobsGorm) TaskTakeIn(tx *gorm.DB, customId string) Task {
|
|
||||||
return jobsGorm.taskTake(tx, customId, jobs_common.TASK_IN)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TaskTakeSuccess 查询单任务 - 任务完成
|
|
||||||
func (jobsGorm *JobsGorm) TaskTakeSuccess(tx *gorm.DB, customId string) Task {
|
|
||||||
return jobsGorm.taskTake(tx, customId, jobs_common.TASK_SUCCESS)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TaskTakeError 查询单任务 - 任务异常
|
|
||||||
func (jobsGorm *JobsGorm) TaskTakeError(tx *gorm.DB, customId string) Task {
|
|
||||||
return jobsGorm.taskTake(tx, customId, jobs_common.TASK_ERROR)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TaskTakeTimeout 查询单任务 - 任务超时
|
|
||||||
func (jobsGorm *JobsGorm) TaskTakeTimeout(tx *gorm.DB, customId string) Task {
|
|
||||||
return jobsGorm.taskTake(tx, customId, jobs_common.TASK_TIMEOUT)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TaskTakeWait 查询单任务 - 任务等待
|
|
||||||
func (jobsGorm *JobsGorm) TaskTakeWait(tx *gorm.DB, customId string) Task {
|
|
||||||
return jobsGorm.taskTake(tx, customId, jobs_common.TASK_WAIT)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TaskTypeTake 查询单任务
|
|
||||||
func (jobsGorm *JobsGorm) TaskTypeTake(tx *gorm.DB, customId, Type string) (result Task) {
|
|
||||||
tx.Where("custom_id = ?", customId).Where("type = ?", Type).Take(&result)
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
// 查询单任务
|
|
||||||
func (jobsGorm *JobsGorm) taskTypeTake(tx *gorm.DB, customId, Type, status string) (result Task) {
|
|
||||||
tx.Where("custom_id = ?", customId).Where("type = ?", Type).Where("status = ?", status).Take(&result)
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
// TaskTypeTakeIn 查询单任务 - 任务运行
|
|
||||||
func (jobsGorm *JobsGorm) TaskTypeTakeIn(tx *gorm.DB, customId, Type string) Task {
|
|
||||||
return jobsGorm.taskTypeTake(tx, customId, Type, jobs_common.TASK_IN)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TaskTypeTakeSuccess 查询单任务 - 任务完成
|
|
||||||
func (jobsGorm *JobsGorm) TaskTypeTakeSuccess(tx *gorm.DB, customId, Type string) Task {
|
|
||||||
return jobsGorm.taskTypeTake(tx, customId, Type, jobs_common.TASK_SUCCESS)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TaskTypeTakeError 查询单任务 - 任务异常
|
|
||||||
func (jobsGorm *JobsGorm) TaskTypeTakeError(tx *gorm.DB, customId, Type string) Task {
|
|
||||||
return jobsGorm.taskTypeTake(tx, customId, Type, jobs_common.TASK_ERROR)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TaskTypeTakeTimeout 查询单任务 - 任务超时
|
|
||||||
func (jobsGorm *JobsGorm) TaskTypeTakeTimeout(tx *gorm.DB, customId, Type string) Task {
|
|
||||||
return jobsGorm.taskTypeTake(tx, customId, Type, jobs_common.TASK_TIMEOUT)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TaskTypeTakeWait 查询单任务 - 任务等待
|
|
||||||
func (jobsGorm *JobsGorm) TaskTypeTakeWait(tx *gorm.DB, customId, Type string) Task {
|
|
||||||
return jobsGorm.taskTypeTake(tx, customId, Type, jobs_common.TASK_WAIT)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TaskFindAll 查询多任务
|
|
||||||
func (jobsGorm *JobsGorm) TaskFindAll(tx *gorm.DB, frequency int64) (results []Task) {
|
|
||||||
tx.Where("frequency = ?", frequency).Order("id asc").Find(&results)
|
|
||||||
return results
|
|
||||||
}
|
|
||||||
|
|
||||||
// 查询多任务
|
|
||||||
func (jobsGorm *JobsGorm) taskFindAll(tx *gorm.DB, frequency int64, status string) (results []Task) {
|
|
||||||
tx.Where("frequency = ?", frequency).Where("status = ?", status).Order("id asc").Find(&results)
|
|
||||||
return results
|
|
||||||
}
|
|
||||||
|
|
||||||
// TaskFindAllIn 查询多任务 - 任务运行
|
|
||||||
func (jobsGorm *JobsGorm) TaskFindAllIn(tx *gorm.DB, frequency int64) []Task {
|
|
||||||
return jobsGorm.taskFindAll(tx, frequency, jobs_common.TASK_IN)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TaskFindAllSuccess 查询多任务 - 任务完成
|
|
||||||
func (jobsGorm *JobsGorm) TaskFindAllSuccess(tx *gorm.DB, frequency int64) []Task {
|
|
||||||
return jobsGorm.taskFindAll(tx, frequency, jobs_common.TASK_SUCCESS)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TaskFindAllError 查询多任务 - 任务异常
|
|
||||||
func (jobsGorm *JobsGorm) TaskFindAllError(tx *gorm.DB, frequency int64) []Task {
|
|
||||||
return jobsGorm.taskFindAll(tx, frequency, jobs_common.TASK_ERROR)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TaskFindAllTimeout 查询多任务 - 任务超时
|
|
||||||
func (jobsGorm *JobsGorm) TaskFindAllTimeout(tx *gorm.DB, frequency int64) []Task {
|
|
||||||
return jobsGorm.taskFindAll(tx, frequency, jobs_common.TASK_TIMEOUT)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TaskFindAllWait 查询多任务 - 任务等待
|
|
||||||
func (jobsGorm *JobsGorm) TaskFindAllWait(tx *gorm.DB, frequency int64) []Task {
|
|
||||||
return jobsGorm.taskFindAll(tx, frequency, jobs_common.TASK_WAIT)
|
|
||||||
}
|
|
||||||
|
|
||||||
// EditTask 任务修改
|
|
||||||
func (jobsGorm *JobsGorm) EditTask(tx *gorm.DB, id uint) *gorm.DB {
|
|
||||||
return tx.Model(&Task{}).Where("id = ?", id)
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateFrequency 更新任务频率
|
|
||||||
func (jobsGorm *JobsGorm) UpdateFrequency(tx *gorm.DB, id uint, frequency int64) *gorm.DB {
|
|
||||||
return jobsGorm.EditTask(tx, id).
|
|
||||||
Select("frequency").
|
|
||||||
Updates(Task{
|
|
||||||
Frequency: frequency,
|
|
||||||
})
|
|
||||||
}
|
|
@ -1,69 +0,0 @@
|
|||||||
package jobs_gorm
|
|
||||||
|
|
||||||
import (
|
|
||||||
"gorm.io/gorm"
|
|
||||||
"log"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TaskIp 任务Ip
|
|
||||||
type TaskIp struct {
|
|
||||||
Id int64 `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号
|
|
||||||
TaskType string `gorm:"comment:任务编号" json:"task_type"` // 任务编号
|
|
||||||
Ips string `gorm:"comment:任务IP" json:"ips"` // 任务IP
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *TaskIp) TableName() string {
|
|
||||||
return "task_ip"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (jobsGorm *JobsGorm) taskIpTake(tx *gorm.DB, taskType, ips string) (result TaskIp) {
|
|
||||||
tx.Where("task_type = ?", taskType).Where("ips = ?", ips).Take(&result)
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
// TaskIpUpdate 更新ip
|
|
||||||
func (jobsGorm *JobsGorm) TaskIpUpdate(tx *gorm.DB, taskType, ips string) *gorm.DB {
|
|
||||||
query := jobsGorm.taskIpTake(tx, taskType, ips)
|
|
||||||
if query.Id != 0 {
|
|
||||||
return tx
|
|
||||||
}
|
|
||||||
updateStatus := tx.Create(&TaskIp{
|
|
||||||
TaskType: taskType,
|
|
||||||
Ips: ips,
|
|
||||||
})
|
|
||||||
if updateStatus.RowsAffected == 0 {
|
|
||||||
log.Println("任务更新失败:", updateStatus.Error)
|
|
||||||
}
|
|
||||||
return updateStatus
|
|
||||||
}
|
|
||||||
|
|
||||||
// TaskIpInit 实例任务ip
|
|
||||||
func (jobsGorm *JobsGorm) TaskIpInit(tx *gorm.DB, ips map[string]string) bool {
|
|
||||||
if jobsGorm.outsideIp == "" || jobsGorm.outsideIp == "0.0.0.0" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
tx.Where("ips = ?", jobsGorm.outsideIp).Delete(&TaskIp{}) // 删除
|
|
||||||
for k, v := range ips {
|
|
||||||
if v == "" {
|
|
||||||
jobsGorm.TaskIpUpdate(tx, k, jobsGorm.outsideIp)
|
|
||||||
} else {
|
|
||||||
find := strings.Contains(v, ",")
|
|
||||||
if find == true {
|
|
||||||
// 包含
|
|
||||||
parts := strings.Split(v, ",")
|
|
||||||
for _, vv := range parts {
|
|
||||||
if vv == jobsGorm.outsideIp {
|
|
||||||
jobsGorm.TaskIpUpdate(tx, k, jobsGorm.outsideIp)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// 不包含
|
|
||||||
if v == jobsGorm.outsideIp {
|
|
||||||
jobsGorm.TaskIpUpdate(tx, k, jobsGorm.outsideIp)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
@ -1,29 +0,0 @@
|
|||||||
package jobs_gorm
|
|
||||||
|
|
||||||
var ParamsOrderType = "order"
|
|
||||||
|
|
||||||
// ParamsOrderId 订单任务
|
|
||||||
type ParamsOrderId struct {
|
|
||||||
OrderId string `json:"order_id,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var ParamsMerchantGoldenBeanType = "merchant.golden_bean"
|
|
||||||
|
|
||||||
var ParamsNewServiceType = "new_service"
|
|
||||||
|
|
||||||
// ParamsTaskId 企业自定义任务
|
|
||||||
type ParamsTaskId struct {
|
|
||||||
TaskId int64 `json:"task_id,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var ParamsNewServiceNextType = "new_service.next"
|
|
||||||
|
|
||||||
// ParamsTaskIdNext 企业自定义下一步任务
|
|
||||||
type ParamsTaskIdNext struct {
|
|
||||||
TaskId int64 `json:"task_id,omitempty"`
|
|
||||||
MerchantUserId int64 `json:"merchant_user_id,omitempty"`
|
|
||||||
CurrentNumber int `json:"current_number,omitempty"`
|
|
||||||
MaxNumber int `json:"max_number,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var ParamsTeamInvType = "team.inv"
|
|
@ -1,25 +0,0 @@
|
|||||||
package jobs_gorm
|
|
||||||
|
|
||||||
func GetTypeApiPaySubmit(Type string) string {
|
|
||||||
return "api.pay.submit." + Type
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetTypeWechatRefundsSubmit(Type string) string {
|
|
||||||
return "wechat.refunds.submit." + Type
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetTypeWechatRefundsQuery(Type string) string {
|
|
||||||
return "wechat.refunds.query." + Type
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetTypeGoldenBeansIssue(Type string) string {
|
|
||||||
return "golden_beans.issue." + Type
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetTypeGoldenBeansRefunds(Type string) string {
|
|
||||||
return "golden_beans.refunds." + Type
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetTypeCustomerAuto(Type string) string {
|
|
||||||
return "customer.auto." + Type
|
|
||||||
}
|
|
@ -0,0 +1,20 @@
|
|||||||
|
package gojobs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go.dtapp.net/goip"
|
||||||
|
"go.dtapp.net/gojobs/jobs_gorm_model"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RefreshIp 刷新Ip
|
||||||
|
func (j *jobsGorm) RefreshIp(tx *gorm.DB) {
|
||||||
|
xip := goip.GetOutsideIp()
|
||||||
|
if j.outsideIp == "" || j.outsideIp == "0.0.0.0" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if j.outsideIp == xip {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tx.Where("ips = ?", j.outsideIp).Delete(&jobs_gorm_model.TaskIp{}) // 删除
|
||||||
|
j.outsideIp = xip
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package gojobs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"go.dtapp.net/gojobs/jobs_gorm_model"
|
||||||
|
"go.dtapp.net/goredis"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Lock 上锁
|
||||||
|
func (j *jobsGorm) Lock(info jobs_gorm_model.Task, id any) string {
|
||||||
|
cacheName := fmt.Sprintf("cron:%v:%v", info.Type, id)
|
||||||
|
judgeCache := j.redis.NewStringOperation().Get(cacheName).UnwrapOr("")
|
||||||
|
if judgeCache != "" {
|
||||||
|
return judgeCache
|
||||||
|
}
|
||||||
|
j.redis.NewStringOperation().Set(cacheName, fmt.Sprintf("已在%v机器上锁成功", j.outsideIp), goredis.WithExpire(time.Millisecond*time.Duration(info.Frequency)*3))
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unlock Lock 解锁
|
||||||
|
func (j *jobsGorm) Unlock(info jobs_gorm_model.Task, id any) {
|
||||||
|
cacheName := fmt.Sprintf("cron:%v:%v", info.Type, id)
|
||||||
|
j.redis.NewStringOperation().Del(cacheName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LockForever 永远上锁
|
||||||
|
func (j *jobsGorm) LockForever(info jobs_gorm_model.Task, id any) string {
|
||||||
|
cacheName := fmt.Sprintf("cron:%v:%v", info.Type, id)
|
||||||
|
judgeCache := j.redis.NewStringOperation().Get(cacheName).UnwrapOr("")
|
||||||
|
if judgeCache != "" {
|
||||||
|
return judgeCache
|
||||||
|
}
|
||||||
|
j.redis.NewStringOperation().Set(cacheName, fmt.Sprintf("已在%v机器永远上锁成功", j.outsideIp))
|
||||||
|
return ""
|
||||||
|
}
|
@ -0,0 +1,190 @@
|
|||||||
|
package gojobs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go.dtapp.net/gojobs/jobs_gorm_model"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TaskTake 查询单任务
|
||||||
|
func (j *jobsGorm) TaskTake(tx *gorm.DB, customId string) (result jobs_gorm_model.Task) {
|
||||||
|
tx.Where("custom_id = ?", customId).Take(&result)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查询单任务
|
||||||
|
func (j *jobsGorm) taskTake(tx *gorm.DB, customId, status string) (result jobs_gorm_model.Task) {
|
||||||
|
tx.Where("custom_id = ?", customId).Where("status = ?", status).Take(&result)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// TaskTakeIn 查询单任务 - 任务运行
|
||||||
|
func (j *jobsGorm) TaskTakeIn(tx *gorm.DB, customId string) jobs_gorm_model.Task {
|
||||||
|
return j.taskTake(tx, customId, TASK_IN)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TaskTakeSuccess 查询单任务 - 任务完成
|
||||||
|
func (j *jobsGorm) TaskTakeSuccess(tx *gorm.DB, customId string) jobs_gorm_model.Task {
|
||||||
|
return j.taskTake(tx, customId, TASK_SUCCESS)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TaskTakeError 查询单任务 - 任务异常
|
||||||
|
func (j *jobsGorm) TaskTakeError(tx *gorm.DB, customId string) jobs_gorm_model.Task {
|
||||||
|
return j.taskTake(tx, customId, TASK_ERROR)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TaskTakeTimeout 查询单任务 - 任务超时
|
||||||
|
func (j *jobsGorm) TaskTakeTimeout(tx *gorm.DB, customId string) jobs_gorm_model.Task {
|
||||||
|
return j.taskTake(tx, customId, TASK_TIMEOUT)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TaskTakeWait 查询单任务 - 任务等待
|
||||||
|
func (j *jobsGorm) TaskTakeWait(tx *gorm.DB, customId string) jobs_gorm_model.Task {
|
||||||
|
return j.taskTake(tx, customId, TASK_WAIT)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TaskTypeTake 查询单任务
|
||||||
|
func (j *jobsGorm) TaskTypeTake(tx *gorm.DB, customId, Type string) (result jobs_gorm_model.Task) {
|
||||||
|
tx.Where("custom_id = ?", customId).Where("type = ?", Type).Take(&result)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查询单任务
|
||||||
|
func (j *jobsGorm) taskTypeTake(tx *gorm.DB, customId, Type, status string) (result jobs_gorm_model.Task) {
|
||||||
|
tx.Where("custom_id = ?", customId).Where("type = ?", Type).Where("status = ?", status).Take(&result)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// TaskTypeTakeIn 查询单任务 - 任务运行
|
||||||
|
func (j *jobsGorm) TaskTypeTakeIn(tx *gorm.DB, customId, Type string) jobs_gorm_model.Task {
|
||||||
|
return j.taskTypeTake(tx, customId, Type, TASK_IN)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TaskTypeTakeSuccess 查询单任务 - 任务完成
|
||||||
|
func (j *jobsGorm) TaskTypeTakeSuccess(tx *gorm.DB, customId, Type string) jobs_gorm_model.Task {
|
||||||
|
return j.taskTypeTake(tx, customId, Type, TASK_SUCCESS)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TaskTypeTakeError 查询单任务 - 任务异常
|
||||||
|
func (j *jobsGorm) TaskTypeTakeError(tx *gorm.DB, customId, Type string) jobs_gorm_model.Task {
|
||||||
|
return j.taskTypeTake(tx, customId, Type, TASK_ERROR)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TaskTypeTakeTimeout 查询单任务 - 任务超时
|
||||||
|
func (j *jobsGorm) TaskTypeTakeTimeout(tx *gorm.DB, customId, Type string) jobs_gorm_model.Task {
|
||||||
|
return j.taskTypeTake(tx, customId, Type, TASK_TIMEOUT)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TaskTypeTakeWait 查询单任务 - 任务等待
|
||||||
|
func (j *jobsGorm) TaskTypeTakeWait(tx *gorm.DB, customId, Type string) jobs_gorm_model.Task {
|
||||||
|
return j.taskTypeTake(tx, customId, Type, TASK_WAIT)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TaskFindAll 查询多任务
|
||||||
|
func (j *jobsGorm) TaskFindAll(tx *gorm.DB, frequency int64) (results []jobs_gorm_model.Task) {
|
||||||
|
tx.Where("frequency = ?", frequency).Order("id asc").Find(&results)
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查询多任务
|
||||||
|
func (j *jobsGorm) taskFindAll(tx *gorm.DB, frequency int64, status string) (results []jobs_gorm_model.Task) {
|
||||||
|
tx.Where("frequency = ?", frequency).Where("status = ?", status).Order("id asc").Find(&results)
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
// TaskFindAllIn 查询多任务 - 任务运行
|
||||||
|
func (j *jobsGorm) TaskFindAllIn(tx *gorm.DB, frequency int64) []jobs_gorm_model.Task {
|
||||||
|
return j.taskFindAll(tx, frequency, TASK_IN)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TaskFindAllSuccess 查询多任务 - 任务完成
|
||||||
|
func (j *jobsGorm) TaskFindAllSuccess(tx *gorm.DB, frequency int64) []jobs_gorm_model.Task {
|
||||||
|
return j.taskFindAll(tx, frequency, TASK_SUCCESS)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TaskFindAllError 查询多任务 - 任务异常
|
||||||
|
func (j *jobsGorm) TaskFindAllError(tx *gorm.DB, frequency int64) []jobs_gorm_model.Task {
|
||||||
|
return j.taskFindAll(tx, frequency, TASK_ERROR)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TaskFindAllTimeout 查询多任务 - 任务超时
|
||||||
|
func (j *jobsGorm) TaskFindAllTimeout(tx *gorm.DB, frequency int64) []jobs_gorm_model.Task {
|
||||||
|
return j.taskFindAll(tx, frequency, TASK_TIMEOUT)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TaskFindAllWait 查询多任务 - 任务等待
|
||||||
|
func (j *jobsGorm) TaskFindAllWait(tx *gorm.DB, frequency int64) []jobs_gorm_model.Task {
|
||||||
|
return j.taskFindAll(tx, frequency, TASK_WAIT)
|
||||||
|
}
|
||||||
|
|
||||||
|
// EditTask 任务修改
|
||||||
|
func (j *jobsGorm) EditTask(tx *gorm.DB, id uint) *gorm.DB {
|
||||||
|
return tx.Model(&jobs_gorm_model.Task{}).Where("id = ?", id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateFrequency 更新任务频率
|
||||||
|
func (j *jobsGorm) UpdateFrequency(tx *gorm.DB, id uint, frequency int64) *gorm.DB {
|
||||||
|
return j.EditTask(tx, id).
|
||||||
|
Select("frequency").
|
||||||
|
Updates(jobs_gorm_model.Task{
|
||||||
|
Frequency: frequency,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *jobsGorm) taskIpTake(tx *gorm.DB, taskType, ips string) (result jobs_gorm_model.TaskIp) {
|
||||||
|
tx.Where("task_type = ?", taskType).Where("ips = ?", ips).Take(&result)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// TaskIpUpdate 更新ip
|
||||||
|
func (j *jobsGorm) TaskIpUpdate(tx *gorm.DB, taskType, ips string) *gorm.DB {
|
||||||
|
query := j.taskIpTake(tx, taskType, ips)
|
||||||
|
if query.Id != 0 {
|
||||||
|
return tx
|
||||||
|
}
|
||||||
|
updateStatus := tx.Create(&jobs_gorm_model.TaskIp{
|
||||||
|
TaskType: taskType,
|
||||||
|
Ips: ips,
|
||||||
|
})
|
||||||
|
if updateStatus.RowsAffected == 0 {
|
||||||
|
log.Println("任务更新失败:", updateStatus.Error)
|
||||||
|
}
|
||||||
|
return updateStatus
|
||||||
|
}
|
||||||
|
|
||||||
|
// TaskIpInit 实例任务ip
|
||||||
|
func (j *jobsGorm) TaskIpInit(tx *gorm.DB, ips map[string]string) bool {
|
||||||
|
if j.outsideIp == "" || j.outsideIp == "0.0.0.0" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
tx.Where("ips = ?", j.outsideIp).Delete(&jobs_gorm_model.TaskIp{}) // 删除
|
||||||
|
for k, v := range ips {
|
||||||
|
if v == "" {
|
||||||
|
j.TaskIpUpdate(tx, k, j.outsideIp)
|
||||||
|
} else {
|
||||||
|
find := strings.Contains(v, ",")
|
||||||
|
if find == true {
|
||||||
|
// 包含
|
||||||
|
parts := strings.Split(v, ",")
|
||||||
|
for _, vv := range parts {
|
||||||
|
if vv == j.outsideIp {
|
||||||
|
j.TaskIpUpdate(tx, k, j.outsideIp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 不包含
|
||||||
|
if v == j.outsideIp {
|
||||||
|
j.TaskIpUpdate(tx, k, j.outsideIp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// TaskLogRunTake 查询任务执行日志
|
||||||
|
func (j *jobsGorm) TaskLogRunTake(tx *gorm.DB, taskId uint, runId string) (result jobs_gorm_model.TaskLogRun) {
|
||||||
|
tx.Select("id", "os", "arch", "outside_ip", "created_at").Where("task_id = ?", taskId).Where("run_id = ?", runId).Take(&result)
|
||||||
|
return result
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package jobs_gorm_model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Task 任务
|
||||||
|
type Task struct {
|
||||||
|
Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号
|
||||||
|
Status string `gorm:"index;comment:状态码" json:"status"` // 状态码
|
||||||
|
Params string `gorm:"comment:参数" json:"params"` // 参数
|
||||||
|
ParamsType string `gorm:"comment:参数类型" json:"params_type"` // 参数类型
|
||||||
|
StatusDesc string `gorm:"comment:状态描述" json:"status_desc"` // 状态描述
|
||||||
|
Frequency int64 `gorm:"index;comment:频率(秒单位)" json:"frequency"` // 频率(秒单位)
|
||||||
|
Number int64 `gorm:"comment:当前次数" json:"number"` // 当前次数
|
||||||
|
MaxNumber int64 `gorm:"comment:最大次数" json:"max_number"` // 最大次数
|
||||||
|
RunId string `gorm:"comment:执行编号" json:"run_id"` // 执行编号
|
||||||
|
CustomId string `gorm:"index;comment:自定义编号" json:"custom_id"` // 自定义编号
|
||||||
|
CustomSequence int64 `gorm:"comment:自定义顺序" json:"custom_sequence"` // 自定义顺序
|
||||||
|
Type string `gorm:"index;comment:类型" json:"type"` // 类型
|
||||||
|
CreatedIp string `gorm:"comment:创建外网IP" json:"created_ip"` // 创建外网IP
|
||||||
|
SpecifyIp string `gorm:"comment:指定外网IP" json:"specify_ip"` // 指定外网IP
|
||||||
|
UpdatedIp string `gorm:"comment:更新外网IP" json:"updated_ip"` // 更新外网IP
|
||||||
|
Result string `gorm:"comment:结果" json:"result"` // 结果
|
||||||
|
CreatedAt string `gorm:"type:text;comment:创建时间" json:"created_at"` // 创建时间
|
||||||
|
UpdatedAt string `gorm:"type:text;comment:更新时间" json:"updated_at"` // 更新时间
|
||||||
|
DeletedAt gorm.DeletedAt `gorm:"type:text;index;comment:删除时间" json:"deleted_at"` // 删除时间
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Task) TableName() string {
|
||||||
|
return "task"
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package jobs_gorm_model
|
||||||
|
|
||||||
|
// TaskIp 任务Ip
|
||||||
|
type TaskIp struct {
|
||||||
|
Id int64 `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号
|
||||||
|
TaskType string `gorm:"comment:任务编号" json:"task_type"` // 任务编号
|
||||||
|
Ips string `gorm:"comment:任务IP" json:"ips"` // 任务IP
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *TaskIp) TableName() string {
|
||||||
|
return "task_ip"
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package jobs_gorm
|
package jobs_gorm_model
|
||||||
|
|
||||||
// TaskLog 任务日志模型
|
// TaskLog 任务日志模型
|
||||||
type TaskLog struct {
|
type TaskLog struct {
|
@ -0,0 +1,25 @@
|
|||||||
|
package gojobs
|
||||||
|
|
||||||
|
//
|
||||||
|
//type JobsOption func(*JobsCron)
|
||||||
|
//
|
||||||
|
//// WithRedis 缓存服务驱动
|
||||||
|
//func WithRedis(db *goredis.Client) JobsOption {
|
||||||
|
// return func(opts *JobsCron) {
|
||||||
|
// opts.redis = db
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//// WithGorm 数据库服务驱动
|
||||||
|
//func WithGorm(db *gorm.DB) JobsOption {
|
||||||
|
// return func(opts *JobsCron) {
|
||||||
|
// opts.db = db
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//// WithMainService 是否主要服务(主要服务可删除过期服务)
|
||||||
|
//func WithMainService(status int) JobsOption {
|
||||||
|
// return func(opts *JobsCron) {
|
||||||
|
// opts.mainService = status
|
||||||
|
// }
|
||||||
|
//}
|
@ -1,3 +1,3 @@
|
|||||||
package gojobs
|
package gojobs
|
||||||
|
|
||||||
const Version = "1.0.29"
|
const Version = "1.0.30"
|
||||||
|
Loading…
Reference in new issue