mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-10-08 11:06:39 +08:00
定时任务添加使用外部链接请求功能
This commit is contained in:
parent
7cf1b8ce8e
commit
088039811a
@ -3,7 +3,6 @@
|
||||
// @Copyright Copyright (c) 2022 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package crons
|
||||
|
||||
import (
|
||||
@ -16,6 +15,7 @@ import (
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/dao"
|
||||
"hotgo/internal/model/entity"
|
||||
"hotgo/utility/validate"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
@ -79,7 +79,16 @@ func StartALL(sysCron []*entity.SysCron) error {
|
||||
for _, cron := range sysCron {
|
||||
f := inst.Get(cron.Name)
|
||||
if f == nil {
|
||||
return gerror.Newf("该任务没有加入任务列表:%v", cron.Name)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
// 没有则添加
|
||||
@ -159,6 +168,10 @@ func Stop(sysCron *entity.SysCron) (err error) {
|
||||
|
||||
// Once 立即执行一次某个任务
|
||||
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 {
|
||||
if v.GetName() == sysCron.Name {
|
||||
go v.Execute(ctx)
|
||||
|
27
server/internal/crons/webRequest.go
Normal file
27
server/internal/crons/webRequest.go
Normal 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)
|
||||
}
|
1
server/utility/request/request.go
Normal file
1
server/utility/request/request.go
Normal file
@ -0,0 +1 @@
|
||||
package request
|
Loading…
Reference in New Issue
Block a user