修复了bug:1.发送逻辑之后未对返回体做校验,添加状态码输出。2.去掉循环定时器导致的bug 3.每次执行完成后输出距离下次执行的时间

master
Administrator 2024-05-04 00:59:14 +08:00
parent 19a6453dd4
commit 8a68269c8f
4 changed files with 71 additions and 65 deletions

View File

@ -3,13 +3,12 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/jasonlvhit/gocron"
"log" "log"
"time" "time"
"week/moonshot" "week/moonshot"
"week/qywechat" "week/qywechat"
"week/tools" "week/tools"
"github.com/jasonlvhit/gocron"
) )
var maxRetries = 0 var maxRetries = 0
@ -29,50 +28,43 @@ _/ ____\______ ____ _____ _____ ____ __ _ _______ ____ ____
flag.BoolVar(&liveMode, "livemode", false, "即时模式默认关闭") flag.BoolVar(&liveMode, "livemode", false, "即时模式默认关闭")
flag.Parse() flag.Parse()
if !liveMode { if !liveMode {
fmt.Println("目前是定时任务模式") log.Println("目前是定时任务模式")
if maxRetries < 3 { // 定义任务,每周五的五点执行
for { gocron.Every(1).Friday().At("17:00").Do(job)
remainingTime := tools.RemainingTimeUntilNextFriday17() // 开始定时任务
if remainingTime <= 0 { <-gocron.Start()
break // 当剩余时间小于等于0时跳出循环
}
fmt.Printf("距离执行定时任务还剩 %d小时 %d分钟 %d秒\n", int(remainingTime.Hours()), int(remainingTime.Minutes())%60, int(remainingTime.Seconds())%60)
time.Sleep(time.Minute * 10) // 等待10分钟然后更新
}
// 定义任务,每周五的五点执行
gocron.Every(1).Friday().At("17:00").Do(job)
// 开始定时任务
<-gocron.Start()
} else {
log.Fatalln("最大重试次数已到达3退出")
}
} else { } else {
fmt.Println("目前是即时任务模式") log.Println("目前是即时任务模式")
job() job()
} }
} }
func job() { func job() {
nowDate := now.Format("2006-01-02 15:04:05") + " " + now.Weekday().String() //最多重试3次
log.Printf("开始执行任务,当前时间是:%v", nowDate) if maxRetries < 3 {
nowDate := now.Format("2006-01-02 15:04:05") + " " + now.Weekday().String()
log.Printf("开始执行任务,当前时间是:%v", nowDate)
err := moonshot.CreateExcel() err := moonshot.CreateExcel()
if err != nil { if err != nil {
maxRetries++ maxRetries++
log.Printf("执行失败,开始第%v次重试...", maxRetries) log.Printf("执行失败,开始第%v次重试...", maxRetries)
log.Println("----------------------------------------------------------------------") log.Println("----------------------------------------------------------------------")
job() job()
}
repContent, err := moonshot.AiChat()
if err != nil {
maxRetries++
log.Printf("执行失败,开始第%v次重试...", maxRetries)
log.Println("----------------------------------------------------------------------")
job()
}
qywechat.Send(repContent)
log.Printf("任务执行成功,当前时间是:%v", nowDate)
log.Printf("等待下次任务执行:%v", tools.TimeUntilFriday())
} else {
log.Fatalln("最大重试次数已到达3退出")
} }
repContent, err := moonshot.AiChat()
if err != nil {
maxRetries++
log.Printf("执行失败,开始第%v次重试...", maxRetries)
log.Println("----------------------------------------------------------------------")
job()
}
qywechat.Send(repContent)
} }

View File

@ -37,11 +37,14 @@ func Send(repContent string) {
fmt.Println(repContent) fmt.Println(repContent)
resp, err := http.Post(webhookURL, "application/json", strings.NewReader(repContent)) resp, err := http.Post(webhookURL, "application/json", strings.NewReader(repContent))
if err != nil { if err != nil {
log.Fatalf("发送企业微信机器人失败,错误是: %v", err) log.Printf("发送企业微信机器人失败,错误是: %v", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
fmt.Println(resp.StatusCode, string(body)) if err != nil {
log.Println("发送企业微信机器人成功") log.Printf("发送企业微信机器人失败,错误是: %v", err)
}
log.Printf("发送企业微信机器人完成,状态码为:%v返回体为%v", resp.StatusCode, string(body))
} }

View File

@ -1,29 +1,40 @@
package tools package tools
import ( import "time"
"time"
)
// 计算时间差值 func TimeUntilFriday() time.Duration {
today := time.Now()
weekday := today.Weekday()
switch weekday {
case time.Monday:
friday := time.Date(today.Year(), today.Month(), today.Day()+4, 17, 0, 0, 0, today.Location())
return friday.Sub(today)
case time.Tuesday:
friday := time.Date(today.Year(), today.Month(), today.Day()+3, 17, 0, 0, 0, today.Location())
return friday.Sub(today)
case time.Wednesday:
friday := time.Date(today.Year(), today.Month(), today.Day()+2, 17, 0, 0, 0, today.Location())
return friday.Sub(today)
case time.Thursday:
friday := time.Date(today.Year(), today.Month(), today.Day()+1, 17, 0, 0, 0, today.Location())
return friday.Sub(today)
case time.Friday:
friday := time.Date(today.Year(), today.Month(), today.Day(), 17, 0, 0, 0, today.Location())
if friday.Sub(today) > 0 {
return friday.Sub(today)
} else if friday.Sub(today) < 0 {
friday = time.Date(today.Year(), today.Month(), today.Day()+7, 17, 0, 0, 0, today.Location())
return friday.Sub(today)
} else {
return 0
}
case time.Saturday:
friday := time.Date(today.Year(), today.Month(), today.Day(), 17, 0, 0, 0, today.Location())
return friday.Sub(today)
case time.Sunday:
friday := time.Date(today.Year(), today.Month(), today.Day()+5, 17, 0, 0, 0, today.Location())
return friday.Sub(today)
func RemainingTimeUntilNextFriday17() time.Duration {
// 获取当前时间
now := time.Now()
// 计算下一个周五的时间
nextFriday := now.AddDate(0, 0, (int(time.Friday)-int(now.Weekday())+7)%7)
// 如果今天是周五则直接返回距离今天17点的时间
if now.Weekday() == time.Friday {
today17 := time.Date(now.Year(), now.Month(), now.Day(), 17, 0, 0, 0, now.Location())
return today17.Sub(now)
} }
return 0
// 构造下一个周五 17 点的时间
nextFriday17 := time.Date(nextFriday.Year(), nextFriday.Month(), nextFriday.Day(), 17, 0, 0, 0, nextFriday.Location())
// 计算时间差
duration := nextFriday17.Sub(now)
return duration
} }

Binary file not shown.