定时任务添加使用外部链接请求功能

This commit is contained in:
ibry 2023-02-10 09:50:24 +08:00
parent 7cf1b8ce8e
commit 088039811a
3 changed files with 43 additions and 2 deletions

View File

@ -3,7 +3,6 @@
// @Copyright Copyright (c) 2022 HotGo CLI // @Copyright Copyright (c) 2022 HotGo CLI
// @Author Ms <133814250@qq.com> // @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE // @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
//
package crons package crons
import ( import (
@ -16,6 +15,7 @@ import (
"hotgo/internal/consts" "hotgo/internal/consts"
"hotgo/internal/dao" "hotgo/internal/dao"
"hotgo/internal/model/entity" "hotgo/internal/model/entity"
"hotgo/utility/validate"
"strings" "strings"
"sync" "sync"
) )
@ -79,8 +79,17 @@ func StartALL(sysCron []*entity.SysCron) error {
for _, cron := range sysCron { for _, cron := range sysCron {
f := inst.Get(cron.Name) f := inst.Get(cron.Name)
if f == nil { if f == nil {
if validate.IsURL(cron.Name) {
f = &TaskItem{
Name: cron.Name,
Fun: func(ctx context.Context) {
webRequest(ctx, cron.Name)
},
}
} else {
return gerror.Newf("该任务没有加入任务列表:%v", cron.Name) return gerror.Newf("该任务没有加入任务列表:%v", cron.Name)
} }
}
// 没有则添加 // 没有则添加
if gcron.Search(cron.Name) == nil { if gcron.Search(cron.Name) == nil {
@ -159,6 +168,10 @@ func Stop(sysCron *entity.SysCron) (err error) {
// Once 立即执行一次某个任务 // Once 立即执行一次某个任务
func Once(ctx context.Context, sysCron *entity.SysCron) error { func Once(ctx context.Context, sysCron *entity.SysCron) error {
if validate.IsURL(sysCron.Name) {
go webRequest(ctx, sysCron.Name)
return nil
}
for _, v := range cronList { for _, v := range cronList {
if v.GetName() == sysCron.Name { if v.GetName() == sysCron.Name {
go v.Execute(ctx) go v.Execute(ctx)

View File

@ -0,0 +1,27 @@
package crons
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/text/gstr"
"hotgo/internal/consts"
)
func webRequest(ctx context.Context, url string) {
args, _ := ctx.Value(consts.ContextKeyCronArgs).([]string)
var (
method = "GET"
)
for _, v := range args {
if gstr.Contains(v, "method") {
method_ := gstr.Split("method", "=")
if len(method_) == 2 {
method = gstr.ToUpper(method_[1])
}
}
}
client := g.Client()
client.DoRequest(ctx, method, url, nil)
}

View File

@ -0,0 +1 @@
package request