mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-05-09 05:21:25 +08:00
iot init
This commit is contained in:
@@ -1,36 +1,23 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"log"
|
||||
)
|
||||
|
||||
// 需要将定义的struct 添加到字典中;
|
||||
// 字典 key 可以配置到 自动任务 调用目标 中;
|
||||
func InitJob() {
|
||||
jobList = map[string]JobsExec{
|
||||
"cronHandle": CronHandle{},
|
||||
// ...
|
||||
}
|
||||
type CronDeviceHandle struct {
|
||||
}
|
||||
|
||||
// 新添加的job 必须按照以下格式定义,并实现Exec函数
|
||||
type CronHandle struct {
|
||||
}
|
||||
|
||||
func (t CronHandle) Exec(arg any) error {
|
||||
str := time.Now().Format(timeFormat) + " [INFO] JobCore ExamplesOne exec success"
|
||||
// TODO: 这里需要注意 Examples 传入参数是 string 所以 arg.(string);请根据对应的类型进行转化;
|
||||
switch arg.(type) {
|
||||
|
||||
case string:
|
||||
if arg.(string) != "" {
|
||||
fmt.Println(str, arg.(string))
|
||||
} else {
|
||||
fmt.Println(str, "arg is nil")
|
||||
}
|
||||
break
|
||||
}
|
||||
func (t CronDeviceHandle) Exec(arg any, content any) error {
|
||||
log.Println("执行设备任务", arg, content)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type CronProductHandle struct {
|
||||
}
|
||||
|
||||
func (t CronProductHandle) Exec(arg any, content any) error {
|
||||
log.Println("执行产品任务", arg, content)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,14 +2,14 @@ package jobs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/kakuilan/kgo"
|
||||
"pandax/apps/job/entity"
|
||||
"pandax/apps/job/services"
|
||||
|
||||
logEntity "pandax/apps/log/entity"
|
||||
logServices "pandax/apps/log/services"
|
||||
|
||||
"github.com/XM-GO/PandaKit/httpclient"
|
||||
"pandax/pkg/global"
|
||||
|
||||
logEntity "pandax/apps/job/entity"
|
||||
logServices "pandax/apps/job/services"
|
||||
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -24,21 +24,24 @@ var lock sync.Mutex
|
||||
|
||||
var Crontab = new(cron.Cron)
|
||||
|
||||
func InitJob() {
|
||||
jobList = map[string]JobsExec{
|
||||
"cronDevice": CronDeviceHandle{},
|
||||
"cronProduct": CronProductHandle{},
|
||||
}
|
||||
// 启动调度任务
|
||||
Setup()
|
||||
}
|
||||
|
||||
type JobCore struct {
|
||||
InvokeTarget string
|
||||
Name string
|
||||
JobGroup string
|
||||
JobId int64
|
||||
JobId string
|
||||
EntryId int
|
||||
CronExpression string // 任务表达式
|
||||
MisfirePolicy string // 错误执行策略
|
||||
MisfirePolicy string
|
||||
Args string
|
||||
}
|
||||
|
||||
// 任务类型 http
|
||||
type HttpJob struct {
|
||||
cron *cron.Cron
|
||||
JobCore
|
||||
Content string
|
||||
}
|
||||
|
||||
type ExecJob struct {
|
||||
@@ -48,25 +51,20 @@ type ExecJob struct {
|
||||
|
||||
func (e *ExecJob) Run() {
|
||||
startTime := time.Now()
|
||||
jobLog := logEntity.JobLog{Name: e.Name, EntryId: e.EntryId, TargetInvoke: e.InvokeTarget, Status: "0"}
|
||||
jobLog.Id = kgo.KStr.Uniqid("")
|
||||
var obj = jobList[e.InvokeTarget]
|
||||
if obj == nil {
|
||||
if e.MisfirePolicy == "2" {
|
||||
Remove(e.cron, e.EntryId)
|
||||
}
|
||||
return
|
||||
}
|
||||
err := CallExec(obj.(JobsExec), e.Args)
|
||||
|
||||
err := CallExec(obj.(JobsExec), e.Args, e.Content)
|
||||
if err != nil {
|
||||
if e.MisfirePolicy == "2" {
|
||||
Remove(e.cron, e.EntryId)
|
||||
return
|
||||
}
|
||||
jobLog.LogInfo = fmt.Sprintf("任务运行错误: %s", err.Error())
|
||||
Remove(e.cron, e.EntryId)
|
||||
} else {
|
||||
latencyTime := time.Now().Sub(startTime)
|
||||
jobLog.LogInfo = fmt.Sprintf("任务运行成功,总耗时 %f", latencyTime.Seconds())
|
||||
}
|
||||
// 执行时间
|
||||
latencyTime := time.Now().Sub(startTime)
|
||||
|
||||
logInfo := fmt.Sprintf("任务运行总耗时 %f", latencyTime.Seconds())
|
||||
logServices.LogJobModelDao.Insert(logEntity.LogJob{Name: e.Name, JobGroup: e.JobGroup, EntryId: e.EntryId, InvokeTarget: e.InvokeTarget, LogInfo: logInfo, Status: "0"})
|
||||
logServices.JobLogModelDao.Insert(jobLog)
|
||||
// 执行一次
|
||||
if e.MisfirePolicy == "1" {
|
||||
Remove(e.cron, e.EntryId)
|
||||
@@ -74,73 +72,12 @@ func (e *ExecJob) Run() {
|
||||
return
|
||||
}
|
||||
|
||||
//http 任务接口
|
||||
func (h *HttpJob) Run() {
|
||||
|
||||
startTime := time.Now()
|
||||
var count = 0
|
||||
LOOP:
|
||||
if count < retryCount {
|
||||
_, err := httpclient.NewRequest(h.InvokeTarget).Get().BodyToString()
|
||||
if err != nil {
|
||||
time.Sleep(time.Duration(count+1) * 5 * time.Second)
|
||||
count = count + 1
|
||||
goto LOOP
|
||||
}
|
||||
}
|
||||
// 执行时间
|
||||
latencyTime := time.Now().Sub(startTime)
|
||||
|
||||
logInfo := fmt.Sprintf("任务运行总耗时 %f", latencyTime.Seconds())
|
||||
logServices.LogJobModelDao.Insert(logEntity.LogJob{Name: h.Name, JobGroup: h.JobGroup, EntryId: h.EntryId, InvokeTarget: h.InvokeTarget, LogInfo: logInfo, Status: "0"})
|
||||
if h.MisfirePolicy == "1" {
|
||||
Remove(h.cron, h.EntryId)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func Setup() {
|
||||
Crontab = NewWithSeconds()
|
||||
// 获取系统job SYSTEM是系统
|
||||
jl := services.JobModelDao.FindList(entity.SysJob{JobGroup: "SYSTEM"})
|
||||
jobList := *jl
|
||||
if len(jobList) == 0 {
|
||||
global.Log.Info(time.Now().Format(timeFormat), " [INFO] JobCore total:0")
|
||||
return
|
||||
}
|
||||
err := services.JobModelDao.RemoveAllEntryID()
|
||||
if err != nil {
|
||||
global.Log.Info(time.Now().Format(timeFormat), " [ERROR] JobCore remove entry_id error", err)
|
||||
}
|
||||
sysJob := entity.SysJob{}
|
||||
for i := 0; i < len(jobList); i++ {
|
||||
//去除禁用的
|
||||
if jobList[i].Status != "0" {
|
||||
continue
|
||||
}
|
||||
if jobList[i].JobType == "1" {
|
||||
j := &HttpJob{}
|
||||
j.InvokeTarget = jobList[i].InvokeTarget
|
||||
j.CronExpression = jobList[i].CronExpression
|
||||
j.JobId = jobList[i].JobId
|
||||
j.Name = jobList[i].JobName
|
||||
j.JobGroup = jobList[i].JobGroup
|
||||
j.MisfirePolicy = jobList[i].MisfirePolicy
|
||||
sysJob.EntryId, err = AddJob(Crontab, j)
|
||||
} else if jobList[i].JobType == "2" {
|
||||
j := &ExecJob{}
|
||||
j.InvokeTarget = jobList[i].InvokeTarget
|
||||
j.CronExpression = jobList[i].CronExpression
|
||||
j.JobId = jobList[i].JobId
|
||||
j.Name = jobList[i].JobName
|
||||
j.JobGroup = jobList[i].JobGroup
|
||||
j.Args = jobList[i].Args
|
||||
j.MisfirePolicy = jobList[i].MisfirePolicy
|
||||
sysJob.EntryId, err = AddJob(Crontab, j)
|
||||
}
|
||||
sysJob.JobId = jobList[i].JobId
|
||||
services.JobModelDao.Update(sysJob)
|
||||
}
|
||||
// 其中任务
|
||||
Crontab.Start()
|
||||
global.Log.Info(time.Now().Format(timeFormat), " [INFO] JobCore start success.")
|
||||
@@ -149,7 +86,7 @@ func Setup() {
|
||||
select {}
|
||||
}
|
||||
|
||||
// 添加任务 AddJob(invokeTarget string, jobId int, jobName string, cronExpression string)
|
||||
// AddJob 添加任务
|
||||
func AddJob(c *cron.Cron, job Job) (int, error) {
|
||||
if job == nil {
|
||||
return 0, nil
|
||||
@@ -157,17 +94,6 @@ func AddJob(c *cron.Cron, job Job) (int, error) {
|
||||
return job.addJob(c)
|
||||
}
|
||||
|
||||
func (h *HttpJob) addJob(c *cron.Cron) (int, error) {
|
||||
id, err := c.AddJob(h.CronExpression, h)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
h.cron = c
|
||||
EntryId := int(id)
|
||||
h.EntryId = EntryId
|
||||
return EntryId, nil
|
||||
}
|
||||
|
||||
func (h *ExecJob) addJob(c *cron.Cron) (int, error) {
|
||||
id, err := c.AddJob(h.CronExpression, h)
|
||||
if err != nil {
|
||||
|
||||
@@ -8,9 +8,9 @@ type Job interface {
|
||||
}
|
||||
|
||||
type JobsExec interface {
|
||||
Exec(arg any) error
|
||||
Exec(arg any, content any) error
|
||||
}
|
||||
|
||||
func CallExec(e JobsExec, arg any) error {
|
||||
return e.Exec(arg)
|
||||
func CallExec(e JobsExec, arg any, content any) error {
|
||||
return e.Exec(arg, content)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user