diff --git a/server/internal/crons/init.go b/server/internal/crons/init.go index a79698f..5b2bc66 100644 --- a/server/internal/crons/init.go +++ b/server/internal/crons/init.go @@ -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) diff --git a/server/internal/crons/webRequest.go b/server/internal/crons/webRequest.go new file mode 100644 index 0000000..51943d6 --- /dev/null +++ b/server/internal/crons/webRequest.go @@ -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) +} diff --git a/server/utility/request/request.go b/server/utility/request/request.go new file mode 100644 index 0000000..725b8fc --- /dev/null +++ b/server/utility/request/request.go @@ -0,0 +1 @@ +package request