From 088039811a1eaf650fadd4a87c0c5baa71d94690 Mon Sep 17 00:00:00 2001 From: ibry Date: Fri, 10 Feb 2023 09:50:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=BD=BF=E7=94=A8=E5=A4=96=E9=83=A8=E9=93=BE=E6=8E=A5?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/internal/crons/init.go | 17 +++++++++++++++-- server/internal/crons/webRequest.go | 27 +++++++++++++++++++++++++++ server/utility/request/request.go | 1 + 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 server/internal/crons/webRequest.go create mode 100644 server/utility/request/request.go 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