Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
67a69fea
提交
67a69fea
authored
9月 29, 2025
作者:
mooncake9527
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update
上级
7815ac1e
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
97 行增加
和
77 行删除
+97
-77
cron.go
xcron/cron.go
+72
-55
init.go
xcron/init.go
+25
-16
xcron.go
xcron/xcron.go
+0
-6
没有找到文件。
xcron/cron.go
浏览文件 @
67a69fea
package
xcron
package
xcron
// func AddHandler(k string, h controller.Fn) {
import
(
// controller.Handlers.Add(k, h)
"context"
// }
"fmt"
// func AddHandlerMap(handlers map[string]controller.Fn) {
"gitlab.wanzhuangkj.com/tush/xpkg/config"
// for k, h := range handlers {
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/eventbus"
// AddHandler(k, h)
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/gocron"
// }
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/logger"
// }
"gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/sliceUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron/biz"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron/controller"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron/enums"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron/service"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron/types"
)
// func initCron() error {
func
AddHandler
(
k
string
,
h
controller
.
Fn
)
{
// cronCfg := config.GetCron()
controller
.
Handlers
.
Add
(
k
,
h
)
// if !cronCfg.Enable {
}
// return nil
// }
func
AddHandlerMap
(
handlers
map
[
string
]
controller
.
Fn
)
{
// ctx := context.TODO()
for
k
,
h
:=
range
handlers
{
// cronJobs, err := service.CronJobService.GetSliceByEnable(ctx, enums.CronJob_Enable_OPEN, &biz.CronJobOpts{})
AddHandler
(
k
,
h
)
// if err != nil {
}
// return err
}
// }
// if len(cronJobs) == 0 {
func
initCron
()
error
{
// return nil
cronCfg
:=
config
.
GetCron
()
// }
if
!
cronCfg
.
Enable
{
// cronJobIDs := sliceUtils.GetIDs(cronJobs)
return
nil
// _ = service.CronJobService.UpdateByIDs(ctx, &types.CronJobUpdateByIDsReq{IDs: cronJobIDs, State: enums.CronJob_State_NOT_RUNNING}, nil)
}
// var tasks []*gocron.Task
ctx
:=
context
.
TODO
()
// for _, job := range cronJobs {
cronJobs
,
err
:=
service
.
CronJobService
.
GetSliceByEnable
(
ctx
,
enums
.
CronJob_Enable_OPEN
,
&
biz
.
CronJobOpts
{})
// job.ParseJobInfo()
if
err
!=
nil
{
// _, ok := controller.Handlers.Get(job.JobCode)
return
err
// if ok {
}
// tsk := gocron.Task{
if
len
(
cronJobs
)
==
0
{
// TimeSpec: job.TimeSpec,
return
nil
// Name: job.JobCode,
}
// Fn: func() {
cronJobIDs
:=
sliceUtils
.
GetIDs
(
cronJobs
)
// ctx := context.WithValue(ctx, ctxUtils.HeaderXRequestIDKey, ctxUtils.GenerateTid())
_
=
service
.
CronJobService
.
UpdateByIDs
(
ctx
,
&
types
.
CronJobUpdateByIDsReq
{
IDs
:
cronJobIDs
,
State
:
enums
.
CronJob_State_NOT_RUNNING
},
nil
)
// if err := controller.ExecJob(ctx, job); err != nil {
var
tasks
[]
*
gocron
.
Task
// logger.Error(fmt.Sprintf("[cron]exec err: %s", err.Error()), logger.Any("name", job.JobName), logger.Any("timeSpec", job.TimeSpec), logger.Any("args", job.Info), ctxUtils.CtxTraceIDField(ctx))
for
_
,
job
:=
range
cronJobs
{
// }
job
.
ParseJobInfo
()
// },
_
,
ok
:=
controller
.
Handlers
.
Get
(
job
.
JobCode
)
// }
if
ok
{
// logger.Info("[cron]add cron job", logger.Any("name", job.JobName), logger.Any("timeSpec", job.TimeSpec), logger.Any("args", job.Info), ctxUtils.CtxTraceIDField(ctx))
tsk
:=
gocron
.
Task
{
// tasks = append(tasks, &tsk)
TimeSpec
:
job
.
TimeSpec
,
// }
Name
:
job
.
JobCode
,
// }
Fn
:
func
()
{
// if len(tasks) == 0 {
ctx
:=
context
.
WithValue
(
ctx
,
ctxUtils
.
HeaderXRequestIDKey
,
ctxUtils
.
GenerateTid
())
// return nil
if
err
:=
controller
.
ExecJob
(
ctx
,
job
);
err
!=
nil
{
// }
logger
.
Error
(
fmt
.
Sprintf
(
"[cron]exec err: %s"
,
err
.
Error
()),
logger
.
Any
(
"name"
,
job
.
JobName
),
logger
.
Any
(
"timeSpec"
,
job
.
TimeSpec
),
logger
.
Any
(
"args"
,
job
.
Info
),
ctxUtils
.
CtxTraceIDField
(
ctx
))
// if err = gocron.Init(gocron.WithLog(logger.Get(), false), gocron.WithGranularity(gocron.SecondType)); err != nil {
}
// return err
},
// }
}
// if err = gocron.Add(tasks...); err != nil {
logger
.
Info
(
"[cron]add cron job"
,
logger
.
Any
(
"name"
,
job
.
JobName
),
logger
.
Any
(
"timeSpec"
,
job
.
TimeSpec
),
logger
.
Any
(
"args"
,
job
.
Info
),
ctxUtils
.
CtxTraceIDField
(
ctx
))
// return err
tasks
=
append
(
tasks
,
&
tsk
)
// }
}
// eventbus.Eb.Publish(ctx, eventbus.TopicCronInitFinish)
}
// logger.Info("[cron] initialized", ctxUtils.CtxTraceIDField(ctx))
if
len
(
tasks
)
==
0
{
// return nil
return
nil
// }
}
if
err
=
gocron
.
Init
(
gocron
.
WithLog
(
logger
.
Get
(),
false
),
gocron
.
WithGranularity
(
gocron
.
SecondType
));
err
!=
nil
{
return
err
}
if
err
=
gocron
.
Add
(
tasks
...
);
err
!=
nil
{
return
err
}
eventbus
.
Eb
.
Publish
(
ctx
,
eventbus
.
TopicCronInitFinish
)
logger
.
Info
(
"[cron] initialized"
,
ctxUtils
.
CtxTraceIDField
(
ctx
))
return
nil
}
xcron/init.go
浏览文件 @
67a69fea
...
@@ -3,27 +3,36 @@ package xcron
...
@@ -3,27 +3,36 @@ package xcron
import
(
import
(
"context"
"context"
"github.com/jinzhu/copier"
"gitlab.wanzhuangkj.com/tush/xpkg/config"
"gitlab.wanzhuangkj.com/tush/xpkg/database"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/eventbus"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/eventbus"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron/cache"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron/dao"
)
)
// 使用xxl
// func init() {
// _ = eventbus.Eb.Subscribe(eventbus.TopicCoreInitFinish, func(ctx context.Context) {
// cronCfg := config.GetCron()
// if !cronCfg.Enable {
// return
// }
// schema := cronCfg.Schema
// dao.CronJobDao.ODao.XDB = &database.XDB{Schema: schema}
// dao.CronJobLogDao.ODao.XDB = &database.XDB{Schema: schema}
// dao.CronJobRecordDao.ODao.XDB = &database.XDB{Schema: schema}
// dao.CronJobDao.ODao.Cache = cache.NewCronJobCache(database.GetCacheType())
// initCron()
// })
// }
func
init
()
{
func
init
()
{
eventbus
.
Eb
.
Subscribe
(
eventbus
.
TopicGinEngineCreated
,
func
(
ctx
context
.
Context
)
{
eventbus
.
Eb
.
Subscribe
(
eventbus
.
TopicGinEngineCreated
,
func
(
ctx
context
.
Context
)
{
initXCron
()
initXCron
()
initLocalCron
()
})
}
func
initLocalCron
()
{
var
cron
config
.
Cron
config
.
Read
(
func
(
c
*
config
.
Config
)
{
_
=
copier
.
Copy
(
&
cron
,
c
.
Cron
)
})
})
if
!
cron
.
Enable
{
return
}
if
cron
.
Type
!=
"db"
{
return
}
schema
:=
cron
.
Schema
dao
.
CronJobDao
.
ODao
.
XDB
=
&
database
.
XDB
{
Schema
:
schema
}
dao
.
CronJobLogDao
.
ODao
.
XDB
=
&
database
.
XDB
{
Schema
:
schema
}
dao
.
CronJobRecordDao
.
ODao
.
XDB
=
&
database
.
XDB
{
Schema
:
schema
}
dao
.
CronJobDao
.
ODao
.
Cache
=
cache
.
NewCronJobCache
(
database
.
GetCacheType
())
initCron
()
}
}
xcron/xcron.go
浏览文件 @
67a69fea
...
@@ -33,12 +33,6 @@ func initXCron() {
...
@@ -33,12 +33,6 @@ func initXCron() {
if
!
cron
.
Enable
{
if
!
cron
.
Enable
{
return
return
}
}
// if cron.Type == "" {
// cron.Type = "xxl"
// config.Write(func(c *config.Config) {
// c.Cron.Type = "xxl"
// })
// }
if
cron
.
Type
!=
"xxl"
{
if
cron
.
Type
!=
"xxl"
{
return
return
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论