feat: Task scheduling supports function with parameters
注意:参数资源sql; ALTER TABLE `sys_job` ADD COLUMN `args` varchar(255) NULL COMMENT '目标参数' AFTER `invoke_target`;
This commit is contained in:
1 parent
242902d2bc
commit
b26258c8ab
5 files changed
+24
-6
No files matched your search
+16
-2
@@ -18,7 +18,21 @@ func InitJob() {
|
||||
type ExamplesOne struct {
|
||||
}
|
||||
|
||||
func (t ExamplesOne) Exec() {
|
||||
func (t ExamplesOne) Exec(arg interface{}) error {
|
||||
str := time.Now().Format(timeFormat) + " [INFO] JobCore ExamplesOne exec success"
|
||||
fmt.Println(str)
|
||||
// TODO: 这里需要注意 Examples 传入参数是 string 所以 arg.(string);请根据对应的类型进行转化;
|
||||
switch arg.(type) {
|
||||
|
||||
case string:
|
||||
if arg.(string) != "" {
|
||||
fmt.Println("string", arg.(string))
|
||||
fmt.Println(str, arg.(string))
|
||||
} else {
|
||||
fmt.Println("arg is nil")
|
||||
fmt.Println(str, "arg is nil")
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
+3
-1
@@ -23,6 +23,7 @@ type JobCore struct {
|
||||
JobId int
|
||||
EntryId int
|
||||
CronExpression string
|
||||
Args string
|
||||
}
|
||||
|
||||
// 任务类型 http
|
||||
@@ -41,7 +42,7 @@ func (e *ExecJob) Run() {
|
||||
global.JobLogger.Warning(" ExecJob Run job nil", e)
|
||||
return
|
||||
}
|
||||
CallExec(obj.(JobsExec))
|
||||
CallExec(obj.(JobsExec), e.Args)
|
||||
// 结束时间
|
||||
endTime := time.Now()
|
||||
|
||||
@@ -119,6 +120,7 @@ func Setup() {
|
||||
j.CronExpression = jobList[i].CronExpression
|
||||
j.JobId = jobList[i].JobId
|
||||
j.Name = jobList[i].JobName
|
||||
j.Args = jobList[i].Args
|
||||
sysJob.EntryId, err = AddJob(j)
|
||||
}
|
||||
_, err = sysJob.Update(jobList[i].JobId)
|
||||
|
||||
+3
-3
@@ -6,9 +6,9 @@ type Job interface {
|
||||
}
|
||||
|
||||
type JobsExec interface {
|
||||
Exec()
|
||||
Exec(arg interface{}) error
|
||||
}
|
||||
|
||||
func CallExec(e JobsExec) {
|
||||
e.Exec()
|
||||
func CallExec(e JobsExec, arg interface{}) error {
|
||||
return e.Exec(arg)
|
||||
}
|
||||
Reference in new issue
Block a user