Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
f591f912
提交
f591f912
authored
9月 19, 2025
作者:
mooncake9527
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update
上级
6bc8686b
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
60 行增加
和
33 行删除
+60
-33
config.go
config/config.go
+8
-0
cron.go
xcron/cron.go
+32
-9
var.go
xcron/dao/var.go
+0
-20
init.go
xcron/init.go
+20
-4
没有找到文件。
config/config.go
浏览文件 @
f591f912
...
...
@@ -72,6 +72,14 @@ func GetOssConfig() *oss.AliOssConfig {
return
ossCfg
}
func
GetCron
()
*
Cron
{
cron
:=
Cron
{}
Read
(
func
(
c
*
Config
)
{
copier
.
Copy
(
&
cron
,
&
c
.
Cron
)
})
return
&
cron
}
func
Read
(
f
func
(
c
*
Config
))
{
if
Cfg
==
nil
{
panic
(
"config is nil, please call config.Init() first"
)
...
...
xcron/cron.go
浏览文件 @
f591f912
...
...
@@ -110,14 +110,22 @@ func (x *cronJobController) CallOnceByCode(c *gin.Context) {
response
.
Success
(
c
)
}
func
Init
(
schema
string
,
handlers
map
[
string
]
Fn
)
error
{
dao
.
Init
(
schema
)
func
AddHandler
(
k
string
,
h
Fn
)
{
Handlers
.
Add
(
k
,
h
)
}
ctx
:=
context
.
TODO
()
TaskRegisterMaps
=
handlers
if
!
config
.
CronOpen
()
{
func
AddHandlerMap
(
handlers
map
[
string
]
Fn
)
{
for
k
,
h
:=
range
handlers
{
AddHandler
(
k
,
h
)
}
}
func
initCron
()
error
{
cronCfg
:=
config
.
GetCron
()
if
!
cronCfg
.
Enable
{
return
nil
}
ctx
:=
context
.
TODO
()
cronJobs
,
err
:=
service
.
CronJobService
.
GetSliceByEnable
(
ctx
,
enums
.
CronJob_Enable_OPEN
,
&
biz
.
CronJobOpts
{})
if
err
!=
nil
{
return
err
...
...
@@ -130,7 +138,7 @@ func Init(schema string, handlers map[string]Fn) error {
var
tasks
[]
*
gocron
.
Task
for
_
,
job
:=
range
cronJobs
{
job
.
ParseJobInfo
()
_
,
ok
:=
handlers
[
job
.
JobCode
]
_
,
ok
:=
Handlers
.
Get
(
job
.
JobCode
)
if
ok
{
tsk
:=
gocron
.
Task
{
TimeSpec
:
job
.
TimeSpec
,
...
...
@@ -162,13 +170,28 @@ func Init(schema string, handlers map[string]Fn) error {
type
Fn
func
(
ctx
context
.
Context
,
job
*
biz
.
CronJob
)
error
var
TaskRegisterMaps
=
map
[
string
]
Fn
{}
type
handlers
struct
{
M
map
[
string
]
Fn
}
var
Handlers
=
&
handlers
{
M
:
make
(
map
[
string
]
Fn
),
}
func
(
x
*
handlers
)
Get
(
key
string
)
(
Fn
,
bool
)
{
v
,
ok
:=
x
.
M
[
key
]
return
v
,
ok
}
func
(
x
*
handlers
)
Add
(
key
string
,
h
Fn
)
{
x
.
M
[
key
]
=
h
}
func
ExecJob
(
ctx
context
.
Context
,
j
*
biz
.
CronJob
)
error
{
if
j
==
nil
{
return
errors
.
New
(
"job is nil"
)
}
_
,
ok
:=
TaskRegisterMaps
[
j
.
JobCode
]
_
,
ok
:=
Handlers
.
Get
(
j
.
JobCode
)
if
!
ok
{
return
errors
.
New
(
"handler not found"
)
}
...
...
@@ -190,7 +213,7 @@ func execTask(ctx context.Context, j *biz.CronJob) error {
if
j
==
nil
{
return
errors
.
New
(
"job not found"
)
}
fn
,
ok
:=
TaskRegisterMaps
[
j
.
JobCode
]
fn
,
ok
:=
Handlers
.
Get
(
j
.
JobCode
)
if
!
ok
{
return
errors
.
New
(
"handler not found"
)
}
...
...
xcron/dao/var.go
deleted
100644 → 0
浏览文件 @
6bc8686b
package
dao
import
(
"context"
"gitlab.wanzhuangkj.com/tush/xpkg/database"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/eventbus"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron/cache"
)
var
(
Schema
string
)
func
Init
(
schema
string
)
{
Schema
=
schema
_
=
eventbus
.
Eb
.
Subscribe
(
eventbus
.
TopicCacheInitFinish
,
func
(
ctx
context
.
Context
)
{
CronJobDao
.
ODao
.
Cache
=
cache
.
NewCronJobCache
(
database
.
GetCacheType
())
})
}
xcron/
dao/
init.go
→
xcron/init.go
浏览文件 @
f591f912
package
dao
package
xcron
import
(
"context"
...
...
@@ -6,16 +6,32 @@ import (
"gitlab.wanzhuangkj.com/tush/xpkg/config"
"gitlab.wanzhuangkj.com/tush/xpkg/database"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/eventbus"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron/cache"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron/dao"
)
func
init
()
{
_
=
eventbus
.
Eb
.
Subscribe
(
eventbus
.
TopicParseConfFinish
,
func
(
ctx
context
.
Context
)
{
cronCfg
:=
config
.
GetCron
()
if
!
cronCfg
.
Enable
{
return
}
schema
:=
""
config
.
Read
(
func
(
c
*
config
.
Config
)
{
schema
=
c
.
Cron
.
Schema
})
CronJobDao
.
ODao
.
XDB
=
&
database
.
XDB
{
Schema
:
schema
}
CronJobLogDao
.
ODao
.
XDB
=
&
database
.
XDB
{
Schema
:
schema
}
CronJobRecordDao
.
ODao
.
XDB
=
&
database
.
XDB
{
Schema
:
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
}
})
_
=
eventbus
.
Eb
.
Subscribe
(
eventbus
.
TopicCacheInitFinish
,
func
(
ctx
context
.
Context
)
{
cronCfg
:=
config
.
GetCron
()
if
!
cronCfg
.
Enable
{
return
}
dao
.
CronJobDao
.
ODao
.
Cache
=
cache
.
NewCronJobCache
(
database
.
GetCacheType
())
})
_
=
eventbus
.
Eb
.
Subscribe
(
eventbus
.
TopicCoreInitFinish
,
func
(
ctx
context
.
Context
)
{
initCron
()
})
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论