From 7e71ad52a42e116e879dbfa42c72992456d1753b Mon Sep 17 00:00:00 2001 From: fuzhongyun <15339891972@163.com> Date: Mon, 2 Feb 2026 17:27:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:=201.=E5=A2=9E=E5=8A=A0env=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=202.=E5=A2=9E=E5=8A=A0=E4=BB=BB=E5=8A=A1=E5=90=8E?= =?UTF-8?q?=E9=97=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/server/main.go | 6 ++++++ config/config_env.yaml | 17 ++++++++++++++++- internal/server/cron.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index 806c43d..a589006 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -13,6 +13,7 @@ func main() { configPath := flag.String("config", "./config/config_test.yaml", "Path to configuration file") onBot := flag.String("bot", "", "bot start") cron := flag.String("cron", "", "close") + runJob := flag.String("runJob", "", "run single job and exit") flag.Parse() ctx := context.Background() bc, err := config.LoadConfig(*configPath) @@ -33,6 +34,11 @@ func main() { if *cron == "start" { app.Cron.Run(ctx) } + // 运行指定任务并退出 + if *runJob != "" { + app.Cron.RunOnce(ctx, *runJob) + return + } log.Fatal(app.HttpServer.Listen(fmt.Sprintf(":%d", bc.Server.Port))) } diff --git a/config/config_env.yaml b/config/config_env.yaml index 9d07c06..b0f5953 100644 --- a/config/config_env.yaml +++ b/config/config_env.yaml @@ -88,7 +88,22 @@ tools: zltxOrderAfterSaleResellerBatch: enabled: true base_url: "https://gateway.dev.cdlsxd.cn/zltx_api/admin/afterSales/reseller_pre_ai" - + weather: + enabled: true + base_url: "https://restapi.amap.com/v3/weather/weatherInfo" + api_key: "12afbde5ab78cb7e575ff76bd0bdef2b" + cozeExpress: + enabled: true + base_url: "https://api.coze.cn" + api_key: "7582477438102552616" + api_secret: "pat_eEN0BdLNDughEtABjJJRYTW71olvDU0qUbfQUeaPc2NnYWO8HeyNoui5aR9z0sSZ" + cozeCompany: + enabled: true + base_url: "https://api.coze.cn" + api_key: "7583905168607100978" + api_secret: "pat_eEN0BdLNDughEtABjJJRYTW71olvDU0qUbfQUeaPc2NnYWO8HeyNoui5aR9z0sSZ" + zltxResellerAuthProductToManagerAndDefaultLossReason: + base_url: "https://revcl.1688sup.com/api/admin/reseller/resellerAuthProduct/getManagerAndDefaultLossReason" # eino tool 配置 eino_tools: # == 货易通 hyt == diff --git a/internal/server/cron.go b/internal/server/cron.go index 066f357..2bd1c8d 100644 --- a/internal/server/cron.go +++ b/internal/server/cron.go @@ -3,6 +3,7 @@ package server import ( "ai_scheduler/internal/services" "context" + "fmt" "github.com/gofiber/fiber/v2/log" "github.com/robfig/cron/v3" @@ -20,6 +21,7 @@ type cronJob struct { EntryId int32 Func func(context.Context) error Name string + Key string Schedule string } @@ -42,11 +44,13 @@ func (c *CronServer) InitJobs(ctx context.Context) { { Func: c.cronService.CronReportSendDingTalk, Name: "直连天下报表推送(钉钉)", + Key: "ding_report_dingtalk", Schedule: "20 12,18,23 * * *", }, { Func: c.cronService.CronReportSendQywx, Name: "直连天下报表推送(微信)", + Key: "ding_report_qywx", Schedule: "20 12,18,23 * * *", }, } @@ -96,3 +100,39 @@ func (c *CronServer) Stop() { c.log.Info("Cron调度器已停止") } } + +func (c *CronServer) RunOnce(ctx context.Context, key string) error { + + if c.jobs == nil { + c.InitJobs(ctx) + } + + // 获取key对应的任务 + var job *cronJob + for _, j := range c.jobs { + if j.Key == key { + job = j + break + } + } + if job == nil { + return fmt.Errorf("unknown job key: %s\n", key) + } + + defer func() { + if r := recover(); r != nil { + fmt.Printf("任务[once]:%s执行时发生panic: %v\n", job.Name, r) + } + fmt.Printf("任务[once]:%s执行结束\n", job.Name) + }() + + fmt.Printf("任务[once]:%s开始执行\n", job.Name) + + err := job.Func(ctx) + if err != nil { + return fmt.Errorf("任务[once]:%s执行失败: %s\n", job.Name, err.Error()) + } + + fmt.Printf("任务[once]:%s执行成功\n", job.Name) + return nil +}