aiweek-reconstruction/cmd/main.go

102 lines
2.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package main
import (
"aiweek/aichat"
"aiweek/notifer"
"aiweek/option"
"aiweek/tools"
"aiweek/udesk/reply"
"flag"
"fmt"
"github.com/jasonlvhit/gocron"
"log"
"os"
"time"
)
var liveMode bool
var operateTime = 1
func main() {
fmt.Println(`
_____
_/ ____\______ ____ _____ _____ ____ __ _ _______ ____ ____
\ __\\_ __ \/ _ \ / \ \__ \ / _ \ \ \/ \/ /\__ \ / \ / ___\
| | | | \( <_> ) Y Y \ / __ \( <_> ) \ / / __ \| | \/ /_/ >
|__| |__| \____/|__|_| / (____ /\____/ \/\_/ (____ /___| /\___ /
\/ \/ \/ \//_____/ `)
flag.BoolVar(&liveMode, "livemode", false, "即时模式默认关闭")
flag.Parse()
if !liveMode {
log.Println("目前是定时任务模式")
log.Printf("等待任务的第%v次执行...\n", operateTime)
// 定义任务,每周五的五点执行
gocron.Every(1).Saturday().At("16:00").Do(retryJob)
// 开始定时任务
<-gocron.Start()
} else {
log.Println("目前是即时任务模式")
err := job()
if err != nil {
log.Printf("执行任务失败,错误是:%v\n", err)
return
}
}
}
func job() error {
excel := reply.NewExcelObj()
excel.SetReplyContent().CreateNewExcel()
err := reply.CopyFile(excel.ExcelPath, option.VOLUMEPATH+excel.ExcelName)
if err != nil {
return err
}
err = os.Remove(excel.ExcelPath)
if err != nil {
return err
}
//校验文件数量超过4个删除最老的文件
fileCount, err := tools.CountFilesInDir(option.VOLUMEPATH)
if err != nil {
return err
}
if fileCount > 4 {
cleanFile := tools.ReadFilename(option.VOLUMEPATH)
err := os.Remove(option.VOLUMEPATH + cleanFile)
if err != nil {
return err
}
}
result := aichat.NewAiReq(excel.ExcelAddress).StartChat(option.MODEL_API).PrepareWechatBody()
remainder := fmt.Sprintf("文件同步完成,地址是%v稍后将发送ai分析结果%v", excel.ExcelAddress, ">_<~")
notifer.SendWechat(remainder)
notifer.SendWechat(result)
return nil
}
func retryJob() {
// 设置最大重试次数
maxRetries := 3
// 设置重试间隔
retryInterval := 20 * time.Second
// 重试逻辑
for attempt := 0; attempt < maxRetries; attempt++ {
err := job()
if err == nil {
operateTime++
log.Println("执行成功")
log.Printf("等待第%v次任务执行\n", operateTime)
return
}
fmt.Printf("尝试 %d 次失败,错误是: %s\n", attempt+1, err)
if attempt < maxRetries-1 {
// 如果不是最后一次重试,等待一段时间后重试
time.Sleep(retryInterval)
}
}
// 如果所有重试都失败了,这里可以处理失败的情况
log.Fatal("所有任务均失败!")
}