修改job为接口类型
This commit is contained in:
1 parent
68eb8f1d51
commit
ee9f823bdd
2 files changed
+41
-30
No files matched your search
+35
-30
@@ -15,7 +15,7 @@ import (
|
|||||||
var timeFormat = "2006-01-02 15:04:05"
|
var timeFormat = "2006-01-02 15:04:05"
|
||||||
var retryCount = 3
|
var retryCount = 3
|
||||||
|
|
||||||
type Job struct {
|
type JobCore struct {
|
||||||
InvokeTarget string
|
InvokeTarget string
|
||||||
Name string
|
Name string
|
||||||
JobId int
|
JobId int
|
||||||
@@ -25,11 +25,11 @@ type Job struct {
|
|||||||
|
|
||||||
// 任务类型 http
|
// 任务类型 http
|
||||||
type HttpJob struct {
|
type HttpJob struct {
|
||||||
Job
|
JobCore
|
||||||
}
|
}
|
||||||
|
|
||||||
type ExecJob struct {
|
type ExecJob struct {
|
||||||
Job
|
JobCore
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e ExecJob) Run() {
|
func (e ExecJob) Run() {
|
||||||
@@ -46,9 +46,9 @@ func (e ExecJob) Run() {
|
|||||||
|
|
||||||
// 执行时间
|
// 执行时间
|
||||||
latencyTime := endTime.Sub(startTime)
|
latencyTime := endTime.Sub(startTime)
|
||||||
str := time.Now().Format(timeFormat) + " [INFO] Job " + string(e.EntryId) + "exec success , spend :" + latencyTime.String()
|
str := time.Now().Format(timeFormat) + " [INFO] JobCore " + string(e.EntryId) + "exec success , spend :" + latencyTime.String()
|
||||||
ws.SendAll(str)
|
ws.SendAll(str)
|
||||||
global.JobLogger.Info(time.Now().Format(timeFormat), " [INFO] Job ", e, "exec success , spend :", latencyTime)
|
global.JobLogger.Info(time.Now().Format(timeFormat), " [INFO] JobCore ", e, "exec success , spend :", latencyTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
//http 任务接口
|
//http 任务接口
|
||||||
@@ -75,30 +75,30 @@ LOOP:
|
|||||||
|
|
||||||
// 执行时间
|
// 执行时间
|
||||||
latencyTime := endTime.Sub(startTime)
|
latencyTime := endTime.Sub(startTime)
|
||||||
str := time.Now().Format(timeFormat) + " [INFO] Job " + string(h.EntryId) + "exec success , spend :" + latencyTime.String()
|
str := time.Now().Format(timeFormat) + " [INFO] JobCore " + string(h.EntryId) + "exec success , spend :" + latencyTime.String()
|
||||||
ws.SendAll(str)
|
ws.SendAll(str)
|
||||||
global.JobLogger.Info(time.Now().Format(timeFormat), " [INFO] Job ", h, "exec success , spend :", latencyTime)
|
global.JobLogger.Info(time.Now().Format(timeFormat), " [INFO] JobCore ", h, "exec success , spend :", latencyTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化
|
// 初始化
|
||||||
func Setup() {
|
func Setup() {
|
||||||
|
|
||||||
fmt.Println(time.Now().Format(timeFormat), " [INFO] Job Starting...")
|
fmt.Println(time.Now().Format(timeFormat), " [INFO] JobCore Starting...")
|
||||||
|
|
||||||
global.GADMCron = cronjob.NewWithSeconds()
|
global.GADMCron = cronjob.NewWithSeconds()
|
||||||
|
|
||||||
sysJob := models.SysJob{}
|
sysJob := models.SysJob{}
|
||||||
jobList, err := sysJob.GetList()
|
jobList, err := sysJob.GetList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(time.Now().Format(timeFormat), " [ERROR] Job init error", err)
|
fmt.Println(time.Now().Format(timeFormat), " [ERROR] JobCore init error", err)
|
||||||
}
|
}
|
||||||
if len(jobList) == 0 {
|
if len(jobList) == 0 {
|
||||||
fmt.Println(time.Now().Format(timeFormat), " [INFO] Job total:0")
|
fmt.Println(time.Now().Format(timeFormat), " [INFO] JobCore total:0")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = sysJob.RemoveAllEntryID()
|
_, err = sysJob.RemoveAllEntryID()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(time.Now().Format(timeFormat), " [ERROR] Job remove entry_id error", err)
|
fmt.Println(time.Now().Format(timeFormat), " [ERROR] JobCore remove entry_id error", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < len(jobList); i++ {
|
for i := 0; i < len(jobList); i++ {
|
||||||
@@ -123,37 +123,42 @@ func Setup() {
|
|||||||
|
|
||||||
// 其中任务
|
// 其中任务
|
||||||
global.GADMCron.Start()
|
global.GADMCron.Start()
|
||||||
fmt.Println(time.Now().Format(timeFormat), " [INFO] Job start success.")
|
fmt.Println(time.Now().Format(timeFormat), " [INFO] JobCore start success.")
|
||||||
// 关闭任务
|
// 关闭任务
|
||||||
defer global.GADMCron.Stop()
|
defer global.GADMCron.Stop()
|
||||||
select {}
|
select {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加任务 AddJob(invokeTarget string, jobId int, jobName string, cronExpression string)
|
// 添加任务 AddJob(invokeTarget string, jobId int, jobName string, cronExpression string)
|
||||||
func AddJob(job interface{}) (int, error) {
|
func AddJob(job Job) (int, error) {
|
||||||
switch job.(type) {
|
if job == nil {
|
||||||
case HttpJob:
|
|
||||||
op, ok := job.(HttpJob)
|
|
||||||
if ok {
|
|
||||||
return op.addJob()
|
|
||||||
}
|
|
||||||
case ExecJob:
|
|
||||||
op, ok := job.(ExecJob)
|
|
||||||
if ok {
|
|
||||||
return op.addJob()
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
fmt.Println("unknown")
|
fmt.Println("unknown")
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
fmt.Println("job error")
|
return job.addJob()
|
||||||
return 0, nil
|
//switch job.(type) {
|
||||||
|
//case HttpJob:
|
||||||
|
// op, ok := job.(HttpJob)
|
||||||
|
// if ok {
|
||||||
|
// return op.addJob()
|
||||||
|
// }
|
||||||
|
//case ExecJob:
|
||||||
|
// op, ok := job.(ExecJob)
|
||||||
|
// if ok {
|
||||||
|
// return op.addJob()
|
||||||
|
// }
|
||||||
|
//default:
|
||||||
|
// fmt.Println("unknown")
|
||||||
|
// return 0, nil
|
||||||
|
//}
|
||||||
|
//fmt.Println("job error")
|
||||||
|
//return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h HttpJob) addJob() (int, error) {
|
func (h HttpJob) addJob() (int, error) {
|
||||||
id, err := global.GADMCron.AddJob(h.CronExpression, h)
|
id, err := global.GADMCron.AddJob(h.CronExpression, h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(time.Now().Format(timeFormat), " [ERROR] Job AddJob error", err)
|
fmt.Println(time.Now().Format(timeFormat), " [ERROR] JobCore AddJob error", err)
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
EntryId := int(id)
|
EntryId := int(id)
|
||||||
@@ -163,7 +168,7 @@ func (h HttpJob) addJob() (int, error) {
|
|||||||
func (h ExecJob) addJob() (int, error) {
|
func (h ExecJob) addJob() (int, error) {
|
||||||
id, err := global.GADMCron.AddJob(h.CronExpression, h)
|
id, err := global.GADMCron.AddJob(h.CronExpression, h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(time.Now().Format(timeFormat), " [ERROR] Job AddJob error", err)
|
fmt.Println(time.Now().Format(timeFormat), " [ERROR] JobCore AddJob error", err)
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
EntryId := int(id)
|
EntryId := int(id)
|
||||||
@@ -175,7 +180,7 @@ func Remove(entryID int) chan bool {
|
|||||||
ch := make(chan bool)
|
ch := make(chan bool)
|
||||||
go func() {
|
go func() {
|
||||||
global.GADMCron.Remove(cron.EntryID(entryID))
|
global.GADMCron.Remove(cron.EntryID(entryID))
|
||||||
fmt.Println(time.Now().Format(timeFormat), " [INFO] Job Remove success ,info entryID :", entryID)
|
fmt.Println(time.Now().Format(timeFormat), " [INFO] JobCore Remove success ,info entryID :", entryID)
|
||||||
ch <- true
|
ch <- true
|
||||||
}()
|
}()
|
||||||
return ch
|
return ch
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package jobs
|
||||||
|
|
||||||
|
type Job interface {
|
||||||
|
Run()
|
||||||
|
addJob() (int, error)
|
||||||
|
}
|
||||||
Reference in new issue
Block a user