74 lines
2.2 KiB
Go
74 lines
2.2 KiB
Go
package main
|
||
|
||
import (
|
||
"flag"
|
||
"fmt"
|
||
"github.com/jasonlvhit/gocron"
|
||
"log"
|
||
"time"
|
||
"week/moonshot"
|
||
"week/qywechat"
|
||
"week/tools"
|
||
)
|
||
|
||
var maxRetries = 0
|
||
var now = time.Now()
|
||
var liveMode bool
|
||
var operateTime = 1
|
||
|
||
func main() {
|
||
log.Println(`
|
||
_____
|
||
_/ ____\______ ____ _____ _____ ____ __ _ _______ ____ ____
|
||
\ __\\_ __ \/ _ \ / \ \__ \ / _ \ \ \/ \/ /\__ \ / \ / ___\
|
||
| | | | \( <_> ) Y Y \ / __ \( <_> ) \ / / __ \| | \/ /_/ >
|
||
|__| |__| \____/|__|_| / (____ /\____/ \/\_/ (____ /___| /\___ /
|
||
\/ \/ \/ \//_____/ `)
|
||
fmt.Println("-----------------------------------------------------------------------------------------")
|
||
fmt.Println("-----------------------------------------------------------------------------------------")
|
||
flag.BoolVar(&liveMode, "livemode", false, "即时模式默认关闭")
|
||
flag.Parse()
|
||
if !liveMode {
|
||
log.Println("目前是定时任务模式")
|
||
log.Printf("等待任务的第%v次执行...", operateTime)
|
||
// 定义任务,每周五的五点执行
|
||
gocron.Every(1).Friday().At("17:00").Do(job)
|
||
// 开始定时任务
|
||
<-gocron.Start()
|
||
} else {
|
||
log.Println("目前是即时任务模式")
|
||
job()
|
||
}
|
||
|
||
}
|
||
|
||
func job() {
|
||
//最多重试3次
|
||
if maxRetries < 3 {
|
||
nowDate := now.Format("2006-01-02 15:04:05") + " " + now.Weekday().String()
|
||
log.Printf("开始执行任务,当前时间是:%v", nowDate)
|
||
|
||
err := moonshot.CreateExcel()
|
||
if err != nil {
|
||
maxRetries++
|
||
log.Printf("执行失败,开始第%v次重试...", maxRetries)
|
||
log.Println("----------------------------------------------------------------------")
|
||
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,退出")
|
||
}
|
||
}
|