Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
86f7a266
提交
86f7a266
authored
9月 26, 2025
作者:
mooncake9527
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add xxl-job
上级
8abe1bac
全部展开
显示空白字符变更
内嵌
并排
正在显示
11 个修改的文件
包含
257 行增加
和
110 行删除
+257
-110
config.go
config/config.go
+8
-0
local.go
config/local.go
+1
-1
xpkg.yaml
config/xpkg.yaml
+5
-0
xgin.go
gin/xgin/xgin.go
+4
-0
global.go
global/global.go
+5
-1
go.mod
go.mod
+20
-21
go.sum
go.sum
+0
-0
init.go
pkg/eventbus/init.go
+1
-0
cron.go
xcron/cron.go
+55
-72
init.go
xcron/init.go
+18
-15
xcron.go
xcron/xcron.go
+140
-0
没有找到文件。
config/config.go
浏览文件 @
86f7a266
...
...
@@ -393,8 +393,16 @@ type UpgradeLimit struct {
}
type
Cron
struct
{
Type
string
`yaml:"type" json:"type"`
// xxl
Enable
bool
`yaml:"enable" json:"enable"`
Schema
string
`yaml:"schema" json:"schema"`
XXL
XXL
`yaml:"xxl" json:"xxl"`
}
type
XXL
struct
{
Addr
string
`yaml:"addr" json:"addr"`
// http://xxl-job-admin.xxl-job:8080/xxl-job-admin
Token
string
`yaml:"token" json:"token"`
// 12345
Executor
string
`yaml:"executor" json:"executor"`
//
}
type
Leaf
struct
{
...
...
config/local.go
浏览文件 @
86f7a266
...
...
@@ -113,7 +113,7 @@ func show(obj ILock, fields ...string) string {
if
err
!=
nil
{
break
}
fields
=
append
(
fields
,
`“SignKey”`
,
`"dsn"`
,
`"password"`
,
`"pwd"`
,
`"signKey"`
,
`"access-key-id"`
,
`"access-key-secret"`
,
`"app-secret"`
,
`"smtpPassword"`
)
fields
=
append
(
fields
,
`“SignKey”`
,
`"dsn"`
,
`"password"`
,
`"pwd"`
,
`"signKey"`
,
`"access-key-id"`
,
`"access-key-secret"`
,
`"app-secret"`
,
`"smtpPassword"`
,
`"token"`
)
out
+=
HideSensitiveFields
(
line
,
fields
...
)
}
return
out
...
...
config/xpkg.yaml
浏览文件 @
86f7a266
...
...
@@ -111,8 +111,13 @@ auth:
signKey
:
6m2KywHIPEaWWIxffGpp8Dfl3z5SjE5WeeDStc1T64klnpoAqHrHN01vneteCAGE
cron
:
type
:
xxl
enable
:
true
schema
:
operator
xxl
:
addr
:
# http://xxl-job-admin.xxl-job:8080/xxl-job-admin
token
:
#12345
oss
:
endpoint
:
https://oss-cn-hangzhou.aliyuncs.com
...
...
gin/xgin/xgin.go
浏览文件 @
86f7a266
...
...
@@ -18,6 +18,7 @@ import (
metrics
"gitlab.wanzhuangkj.com/tush/xpkg/gin/middleware/metrics_wz"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/prof"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/validator"
"gitlab.wanzhuangkj.com/tush/xpkg/global"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/errcode"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/eventbus"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/goredis"
...
...
@@ -133,6 +134,9 @@ func New() *gin.Engine {
r
.
GET
(
"/ping"
,
handlerfunc
.
Ping
)
r
.
GET
(
"/codes"
,
handlerfunc
.
ListCodes
)
global
.
G
.
Engine
=
r
eventbus
.
Eb
.
Publish
(
context
.
TODO
(),
eventbus
.
TopicGinEngineCreated
)
return
r
}
...
...
global/global.go
浏览文件 @
86f7a266
package
global
import
"gitlab.wanzhuangkj.com/tush/xpkg/rd/nacos"
import
(
"github.com/gin-gonic/gin"
"gitlab.wanzhuangkj.com/tush/xpkg/rd/nacos"
)
type
g
struct
{
Engine
*
gin
.
Engine
NacosCli
*
nacos
.
NacosServiceClient
}
...
...
go.mod
浏览文件 @
86f7a266
...
...
@@ -13,7 +13,7 @@ require (
github.com/fsnotify/fsnotify v1.5.4
github.com/gin-contrib/cors v1.7.2
github.com/gin-gonic/gin v1.9.1
github.com/go-playground/validator/v10 v10.2
0
.0
github.com/go-playground/validator/v10 v10.2
7
.0
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/golang-jwt/jwt/v5 v5.0.0
github.com/golang/snappy v0.0.4
...
...
@@ -35,6 +35,7 @@ require (
github.com/swaggo/swag v1.8.12
github.com/uptrace/opentelemetry-go-extra/otelgorm v0.2.3
github.com/vmihailenco/msgpack v4.0.4+incompatible
gitlab.wanzhuangkj.com/tush/xxl-job-executor-go v1.0.2
go.opentelemetry.io/contrib v1.24.0
go.opentelemetry.io/otel v1.24.0
go.opentelemetry.io/otel/exporters/jaeger v1.17.0
...
...
@@ -42,10 +43,10 @@ require (
go.opentelemetry.io/otel/sdk v1.24.0
go.opentelemetry.io/otel/trace v1.24.0
go.uber.org/zap v1.24.0
golang.org/x/crypto v0.
36
.0
golang.org/x/sync v0.1
2
.0
golang.org/x/crypto v0.
40
.0
golang.org/x/sync v0.1
6
.0
google.golang.org/grpc v1.67.1
google.golang.org/protobuf v1.36.
5
google.golang.org/protobuf v1.36.
9
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/mysql v1.5.2
gorm.io/driver/postgres v1.5.4
...
...
@@ -56,7 +57,6 @@ require (
require (
github.com/bwmarrin/snowflake v0.3.0
github.com/dlclark/regexp2 v1.11.5
github.com/duke-git/lancet/v2 v2.3.4
github.com/go-redsync/redsync/v4 v4.13.0
github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1
...
...
@@ -71,10 +71,10 @@ require (
github.com/armon/go-metrics v0.3.10 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/go-basic/ipv4 v1.0.0 // indirect
github.com/go-errors/errors v1.0.1 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v1.2.0 // indirect
...
...
@@ -99,16 +99,15 @@ require (
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bytedance/sonic v1.1
1.6
// indirect
github.com/bytedance/sonic/loader v0.
1.1
// indirect
github.com/bytedance/sonic v1.1
4.0
// indirect
github.com/bytedance/sonic/loader v0.
3.0
// indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/cloudwego/base64x v0.1.6 // indirect
github.com/davecgh/go-spew v1.1.1
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.
3
// indirect
github.com/gin-contrib/sse v
0
.1.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.
8
// indirect
github.com/gin-contrib/sse v
1
.1.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
...
...
@@ -116,7 +115,7 @@ require (
github.com/go-openapi/jsonreference v0.19.6 // indirect
github.com/go-openapi/spec v0.20.4 // indirect
github.com/go-openapi/swag v0.19.15 // indirect
github.com/go-playground/locales v0.14.1
github.com/go-playground/locales v0.14.1
// indirect
github.com/go-playground/universal-translator v0.18.1
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/glog v1.2.2 // indirect
...
...
@@ -131,7 +130,7 @@ require (
github.com/jinzhu/now v1.1.5 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.
2.7
// indirect
github.com/klauspost/cpuid/v2 v2.
3.0
// indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
...
...
@@ -143,7 +142,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.2.
1
// indirect
github.com/pelletier/go-toml/v2 v2.2.
4
// indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/prometheus/client_model v0.6.0 // indirect
...
...
@@ -158,19 +157,19 @@ require (
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.
2.12
// indirect
github.com/ugorji/go/codec v1.
3.0
// indirect
github.com/uptrace/opentelemetry-go-extra/otelsql v0.2.3 // indirect
github.com/yuin/gopher-lua v0.0.0-20210529063254-f4c35e4016d9 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/arch v0.
7
.0 // indirect
golang.org/x/net v0.
37
.0 // indirect
golang.org/x/sys v0.3
1
.0 // indirect
golang.org/x/text v0.2
3
.0 // indirect
golang.org/x/arch v0.
20
.0 // indirect
golang.org/x/net v0.
42
.0 // indirect
golang.org/x/sys v0.3
5
.0 // indirect
golang.org/x/text v0.2
7
.0 // indirect
golang.org/x/time v0.1.0 // indirect
golang.org/x/tools v0.3
0
.0 // indirect
golang.org/x/tools v0.3
4
.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
...
...
go.sum
浏览文件 @
86f7a266
差异被折叠。
点击展开。
pkg/eventbus/init.go
浏览文件 @
86f7a266
...
...
@@ -9,6 +9,7 @@ const (
TopicCacheInitFinish
string
=
"event:application:cache:init:finish"
// cache初始化完成
TopicCronInitFinish
string
=
"event:application:cron:init:finish"
// 定时任务初始化完成
TopicCoreInitFinish
string
=
"event:application:core:init:finish"
// 核心服务初始化完成
TopicGinEngineCreated
string
=
"event:application:gin:engine:created:finish"
// gin engine 创建完毕
TopicPrintSwagger
string
=
"event:swagger:print"
// 打印swagger doc info
TopicApplicationClose
string
=
"event:application:close"
// 应用关闭
)
xcron/cron.go
浏览文件 @
86f7a266
package
xcron
import
(
"context"
"fmt"
// func AddHandler(k string, h controller.Fn) {
// controller.Handlers.Add(k, h)
// }
"gitlab.wanzhuangkj.com/tush/xpkg/config"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/eventbus"
"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 AddHandlerMap(handlers map[string]controller.Fn) {
// for k, h := range handlers {
// AddHandler(k, h)
// }
// }
func
AddHandler
(
k
string
,
h
controller
.
Fn
)
{
controller
.
Handlers
.
Add
(
k
,
h
)
}
func
AddHandlerMap
(
handlers
map
[
string
]
controller
.
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
}
if
len
(
cronJobs
)
==
0
{
return
nil
}
cronJobIDs
:=
sliceUtils
.
GetIDs
(
cronJobs
)
_
=
service
.
CronJobService
.
UpdateByIDs
(
ctx
,
&
types
.
CronJobUpdateByIDsReq
{
IDs
:
cronJobIDs
,
State
:
enums
.
CronJob_State_NOT_RUNNING
},
nil
)
var
tasks
[]
*
gocron
.
Task
for
_
,
job
:=
range
cronJobs
{
job
.
ParseJobInfo
()
_
,
ok
:=
controller
.
Handlers
.
Get
(
job
.
JobCode
)
if
ok
{
tsk
:=
gocron
.
Task
{
TimeSpec
:
job
.
TimeSpec
,
Name
:
job
.
JobCode
,
Fn
:
func
()
{
ctx
:=
context
.
WithValue
(
ctx
,
ctxUtils
.
HeaderXRequestIDKey
,
ctxUtils
.
GenerateTid
())
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
))
}
},
}
logger
.
Info
(
"[cron]add cron job"
,
logger
.
Any
(
"name"
,
job
.
JobName
),
logger
.
Any
(
"timeSpec"
,
job
.
TimeSpec
),
logger
.
Any
(
"args"
,
job
.
Info
),
ctxUtils
.
CtxTraceIDField
(
ctx
))
tasks
=
append
(
tasks
,
&
tsk
)
}
}
if
len
(
tasks
)
==
0
{
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
}
// 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
// }
// if len(cronJobs) == 0 {
// return nil
// }
// cronJobIDs := sliceUtils.GetIDs(cronJobs)
// _ = service.CronJobService.UpdateByIDs(ctx, &types.CronJobUpdateByIDsReq{IDs: cronJobIDs, State: enums.CronJob_State_NOT_RUNNING}, nil)
// var tasks []*gocron.Task
// for _, job := range cronJobs {
// job.ParseJobInfo()
// _, ok := controller.Handlers.Get(job.JobCode)
// if ok {
// tsk := gocron.Task{
// TimeSpec: job.TimeSpec,
// Name: job.JobCode,
// Fn: func() {
// ctx := context.WithValue(ctx, ctxUtils.HeaderXRequestIDKey, ctxUtils.GenerateTid())
// 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))
// }
// },
// }
// logger.Info("[cron]add cron job", logger.Any("name", job.JobName), logger.Any("timeSpec", job.TimeSpec), logger.Any("args", job.Info), ctxUtils.CtxTraceIDField(ctx))
// tasks = append(tasks, &tsk)
// }
// }
// if len(tasks) == 0 {
// 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
浏览文件 @
86f7a266
...
...
@@ -3,24 +3,27 @@ package xcron
import
(
"context"
"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"
)
// 使用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
()
{
_
=
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
()
eventbus
.
Eb
.
Subscribe
(
eventbus
.
TopicGinEngineCreated
,
func
(
ctx
context
.
Context
)
{
initXCron
()
})
}
xcron/xcron.go
0 → 100644
浏览文件 @
86f7a266
package
xcron
import
(
"context"
"github.com/gin-gonic/gin"
"github.com/jinzhu/copier"
"github.com/spf13/cast"
"gitlab.wanzhuangkj.com/tush/xpkg/config"
"gitlab.wanzhuangkj.com/tush/xpkg/global"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/eventbus"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/ips"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/logger"
xxl
"gitlab.wanzhuangkj.com/tush/xxl-job-executor-go"
)
var
(
xxlExecutor
xxl
.
Executor
)
func
init
()
{
_
=
eventbus
.
Eb
.
Subscribe
(
eventbus
.
TopicApplicationClose
,
func
(
ctx
context
.
Context
)
{
xxlExecutor
.
Stop
()
})
}
func
initXCron
()
{
var
cron
config
.
Cron
config
.
Read
(
func
(
c
*
config
.
Config
)
{
_
=
copier
.
Copy
(
&
cron
,
c
.
Cron
)
})
if
!
cron
.
Enable
{
return
}
if
cron
.
Type
==
""
{
cron
.
Type
=
"xxl"
config
.
Write
(
func
(
c
*
config
.
Config
)
{
c
.
Cron
.
Type
=
"xxl"
})
}
if
cron
.
Type
!=
"xxl"
{
return
}
xxlJob
(
global
.
G
.
Engine
)
}
type
jobMap
struct
{
m
map
[
string
]
xxl
.
TaskFunc
}
var
JobMap
=
&
jobMap
{
m
:
make
(
map
[
string
]
xxl
.
TaskFunc
),
}
func
(
x
*
jobMap
)
add
(
k
string
,
h
xxl
.
TaskFunc
)
{
x
.
m
[
k
]
=
h
}
func
(
x
*
jobMap
)
isEmpty
()
bool
{
return
len
(
x
.
m
)
==
0
}
func
(
x
*
jobMap
)
ForEach
(
f
func
(
k
string
,
h
xxl
.
TaskFunc
))
{
for
k
,
v
:=
range
x
.
m
{
f
(
k
,
v
)
}
}
func
Add
(
k
string
,
h
xxl
.
TaskFunc
)
{
JobMap
.
add
(
k
,
h
)
}
func
xxlJob
(
r
*
gin
.
Engine
)
{
if
r
==
nil
{
panic
(
"gin engine not created"
)
}
if
JobMap
.
isEmpty
()
{
return
}
ip
:=
ips
.
GetLocalHost
()
var
(
port
int
add
string
token
string
executor
string
)
config
.
Read
(
func
(
c
*
config
.
Config
)
{
port
=
config
.
Cfg
.
HTTP
.
Port
add
=
config
.
Cfg
.
Cron
.
XXL
.
Addr
token
=
config
.
Cfg
.
Cron
.
XXL
.
Token
executor
=
config
.
Cfg
.
Cron
.
XXL
.
Executor
// appName = config.Cfg.App.Name
})
xxlExecutor
=
xxl
.
NewExecutor
(
xxl
.
ServerAddr
(
add
),
xxl
.
AccessToken
(
token
),
//请求令牌(默认为空)
xxl
.
ExecutorIp
(
ip
),
//可自动获取
xxl
.
ExecutorPort
(
cast
.
ToString
(
port
)),
//默认9999(此处要与gin服务启动port必需一至)
xxl
.
RegistryKey
(
executor
),
//执行器名称
xxl
.
SetLogger
(
&
log
{}),
)
xxlExecutor
.
Init
()
xxlExecutor
.
Use
(
customMiddleware
)
xxl
.
XxlJobMux
(
r
,
xxlExecutor
)
JobMap
.
ForEach
(
func
(
k
string
,
h
xxl
.
TaskFunc
)
{
xxlExecutor
.
RegTask
(
k
,
h
)
})
}
// func customLogHandler(req *xxl.LogReq) *xxl.LogRes {
// return &xxl.LogRes{Code: xxl.SuccessCode, Msg: "", Content: xxl.LogResContent{
// FromLineNum: req.FromLineNum,
// ToLineNum: 2,
// LogContent: "这个是自定义日志handler",
// IsEnd: true,
// }}
// }
type
log
struct
{}
func
(
l
*
log
)
Info
(
format
string
,
a
...
interface
{})
{
logger
.
Infof
(
format
,
a
...
)
}
func
(
l
*
log
)
Error
(
format
string
,
a
...
interface
{})
{
logger
.
Errorf
(
format
,
a
...
)
}
func
customMiddleware
(
tf
xxl
.
TaskFunc
)
xxl
.
TaskFunc
{
return
func
(
cxt
context
.
Context
,
req
*
xxl
.
RunReq
)
string
{
logger
.
Info
(
"[xxl][middleware]=====req====="
,
logger
.
Any
(
"req"
,
req
))
res
:=
tf
(
cxt
,
req
)
logger
.
Info
(
"[xxl][middleware]=====rsp====="
,
logger
.
Any
(
"res"
,
res
))
return
res
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论