Compare commits

..

No commits in common. "cd0aa7b357ed4e82f02e32e37e4c1e2226fdd589" and "19a6453dd4441b284a0dfce0508b892f6cc3fa7d" have entirely different histories.

4 changed files with 65 additions and 74 deletions

View File

@ -3,18 +3,18 @@ package main
import (
"flag"
"fmt"
"github.com/jasonlvhit/gocron"
"log"
"time"
"week/moonshot"
"week/qywechat"
"week/tools"
"github.com/jasonlvhit/gocron"
)
var maxRetries = 0
var now = time.Now()
var liveMode bool
var operateTime = 1
func main() {
log.Println(`
@ -29,22 +29,32 @@ _/ ____\______ ____ _____ _____ ____ __ _ _______ ____ ____
flag.BoolVar(&liveMode, "livemode", false, "即时模式默认关闭")
flag.Parse()
if !liveMode {
log.Println("目前是定时任务模式")
log.Printf("等待任务的第%v次执行...", operateTime)
fmt.Println("目前是定时任务模式")
if maxRetries < 3 {
for {
remainingTime := tools.RemainingTimeUntilNextFriday17()
if remainingTime <= 0 {
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.Println("目前是即时任务模式")
log.Fatalln("最大重试次数已到达3退出")
}
} else {
fmt.Println("目前是即时任务模式")
job()
}
}
func job() {
//最多重试3次
if maxRetries < 3 {
nowDate := now.Format("2006-01-02 15:04:05") + " " + now.Weekday().String()
log.Printf("开始执行任务,当前时间是:%v", nowDate)
@ -64,10 +74,5 @@ func job() {
job()
}
qywechat.Send(repContent)
operateTime++
log.Printf("任务执行成功,当前时间是:%v", nowDate)
log.Printf("等待%v次任务执行还剩:%v", operateTime, tools.TimeUntilFriday())
} else {
log.Fatalln("最大重试次数已到达3退出")
}
}

View File

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

View File

@ -1,40 +1,29 @@
package tools
import "time"
import (
"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()+6, 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.