Compare commits
No commits in common. "cd0aa7b357ed4e82f02e32e37e4c1e2226fdd589" and "19a6453dd4441b284a0dfce0508b892f6cc3fa7d" have entirely different histories.
cd0aa7b357
...
19a6453dd4
71
cmd/main.go
71
cmd/main.go
|
@ -3,18 +3,18 @@ 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
|
||||||
var now = time.Now()
|
var now = time.Now()
|
||||||
var liveMode bool
|
var liveMode bool
|
||||||
var operateTime = 1
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.Println(`
|
log.Println(`
|
||||||
|
@ -29,45 +29,50 @@ _/ ____\______ ____ _____ _____ ____ __ _ _______ ____ ____
|
||||||
flag.BoolVar(&liveMode, "livemode", false, "即时模式默认关闭")
|
flag.BoolVar(&liveMode, "livemode", false, "即时模式默认关闭")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if !liveMode {
|
if !liveMode {
|
||||||
log.Println("目前是定时任务模式")
|
fmt.Println("目前是定时任务模式")
|
||||||
log.Printf("等待任务的第%v次执行...", operateTime)
|
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 {
|
||||||
log.Println("目前是即时任务模式")
|
fmt.Println("目前是即时任务模式")
|
||||||
job()
|
job()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func job() {
|
func job() {
|
||||||
//最多重试3次
|
nowDate := now.Format("2006-01-02 15:04:05") + " " + now.Weekday().String()
|
||||||
if maxRetries < 3 {
|
log.Printf("开始执行任务,当前时间是:%v", nowDate)
|
||||||
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)
|
|
||||||
operateTime++
|
|
||||||
log.Printf("任务执行成功,当前时间是:%v", nowDate)
|
|
||||||
log.Printf("等待%v次任务执行还剩:%v", operateTime, tools.TimeUntilFriday())
|
|
||||||
} else {
|
|
||||||
log.Fatalln("最大重试次数已到达3,退出")
|
|
||||||
}
|
}
|
||||||
|
repContent, err := moonshot.AiChat()
|
||||||
|
if err != nil {
|
||||||
|
maxRetries++
|
||||||
|
log.Printf("执行失败,开始第%v次重试...", maxRetries)
|
||||||
|
log.Println("----------------------------------------------------------------------")
|
||||||
|
job()
|
||||||
|
}
|
||||||
|
qywechat.Send(repContent)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,14 +37,11 @@ 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.Printf("发送企业微信机器人失败,错误是: %v", err)
|
log.Fatalf("发送企业微信机器人失败,错误是: %v", err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, _ := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
fmt.Println(resp.StatusCode, string(body))
|
||||||
log.Printf("发送企业微信机器人失败,错误是: %v", err)
|
log.Println("发送企业微信机器人成功")
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("发送企业微信机器人完成,状态码为:%v,返回体为:%v", resp.StatusCode, string(body))
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,40 +1,29 @@
|
||||||
package tools
|
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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue