aiweek-reconstruction/main.go

100 lines
2.6 KiB
Go
Raw Normal View History

2024-05-18 01:46:35 +08:00
package main
import (
"aiweek/aichat"
"aiweek/notifer"
"aiweek/tools"
"aiweek/udesk/reply"
"flag"
"fmt"
"github.com/jasonlvhit/gocron"
"log"
"os"
"time"
)
const ToNginxFilepath = "C:\\Users\\wangaocean\\Desktop\\巡检\\"
const kimiUrl = "http://43.143.245.135:8000/v1/chat/completions"
var liveMode bool
var operateTime = 1
func main() {
log.Println(`
_____
_/ ____\______ ____ _____ _____ ____ __ _ _______ ____ ____
\ __\\_ __ \/ _ \ / \ \__ \ / _ \ \ \/ \/ /\__ \ / \ / ___\
| | | | \( <_> ) Y Y \ / __ \( <_> ) \ / / __ \| | \/ /_/ >
|__| |__| \____/|__|_| / (____ /\____/ \/\_/ (____ /___| /\___ /
\/ \/ \/ \//_____/ `)
flag.BoolVar(&liveMode, "livemode", false, "即时模式默认关闭")
flag.Parse()
if !liveMode {
log.Println("目前是定时任务模式")
log.Printf("等待任务的第%v次执行...", operateTime)
// 定义任务,每周五的五点执行
gocron.Every(1).Friday().At("16:00").Do(retryJob)
// 开始定时任务
<-gocron.Start()
} else {
log.Println("目前是即时任务模式")
job()
}
}
func job() error {
e := reply.NewExcelObj()
replyObj, _ := e.SetReplyContent()
e.CreateNewExcel(replyObj)
e.SetAddress()
//fmt.Println("struct:", e.ExcelName, e.ExcelPath, e.ExcelAddress)
err := reply.CopyFile(e.ExcelPath, ToNginxFilepath+reply.ExcelName)
if err != nil {
return err
}
err = os.Remove(e.ExcelPath)
if err != nil {
return err
}
//校验文件数量超过4个删除最老的文件
fileCount, err := tools.CountFilesInDir(ToNginxFilepath)
if err != nil {
return err
}
if fileCount > 4 {
cleanFile := tools.ReadFilename(ToNginxFilepath)
err := os.Remove(ToNginxFilepath + cleanFile)
if err != nil {
return err
}
}
result := aichat.NewAiReq(e.ExcelAddress).StartChat(kimiUrl).PrepareWechatBody()
notifer.SendWechat(result)
operateTime++
return nil
}
func retryJob() {
// 设置最大重试次数
maxRetries := 3
// 设置重试间隔
retryInterval := 20 * time.Second
// 重试逻辑
for attempt := 0; attempt < maxRetries; attempt++ {
err := job()
if err == nil {
log.Println("执行成功")
return
}
fmt.Printf("尝试 %d 次失败,错误是: %s\n", attempt+1, err)
if attempt < maxRetries-1 {
// 如果不是最后一次重试,等待一段时间后重试
time.Sleep(retryInterval)
}
}
// 如果所有重试都失败了,这里可以处理失败的情况
log.Fatal("所有任务均失败!")
}