Compare commits
No commits in common. "e9601931519587f43361a5e5eeabf2c6ec47b68f" and "main" have entirely different histories.
e960193151
...
main
41
cmd/main.go
41
cmd/main.go
|
@ -1,40 +1,21 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
"week/moonshot"
|
"week/moonshot"
|
||||||
"week/qywechat"
|
"week/qywechat"
|
||||||
"week/tools"
|
|
||||||
|
|
||||||
"github.com/jasonlvhit/gocron"
|
"github.com/jasonlvhit/gocron"
|
||||||
)
|
)
|
||||||
|
|
||||||
var maxRetries = 0
|
var maxRetries = 3
|
||||||
var now = time.Now()
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("-----------------------------------------------------------------------------------------")
|
now := time.Now()
|
||||||
fmt.Println("-----------------------------------------------------------------------------------------")
|
nowDate := now.Format("2006-01-02 15:04:05") + " " + now.Weekday().String()
|
||||||
log.Println(`
|
log.Printf("等待执行定时任务中...当前时间是:%s", nowDate)
|
||||||
_____
|
if maxRetries != 0 {
|
||||||
_/ ____\______ ____ _____ _____ ____ __ _ _______ ____ ____
|
|
||||||
\ __\\_ __ \/ _ \ / \ \__ \ / _ \ \ \/ \/ /\__ \ / \ / ___\
|
|
||||||
| | | | \( <_> ) Y Y \ / __ \( <_> ) \ / / __ \| | \/ /_/ >
|
|
||||||
|__| |__| \____/|__|_| / (____ /\____/ \/\_/ (____ /___| /\___ /
|
|
||||||
\/ \/ \/ \//_____/`)
|
|
||||||
fmt.Println("-----------------------------------------------------------------------------------------")
|
|
||||||
fmt.Println("-----------------------------------------------------------------------------------------")
|
|
||||||
if maxRetries < 3 {
|
|
||||||
for {
|
|
||||||
remainingTime := tools.RemainingTimeUntilNextFriday17()
|
|
||||||
if remainingTime <= 0 {
|
|
||||||
break // 当剩余时间小于等于0时跳出循环
|
|
||||||
}
|
|
||||||
fmt.Printf("\r距离执行定时任务还剩 %d小时 %d分钟 %d秒", int(remainingTime.Hours()), int(remainingTime.Minutes())%60, int(remainingTime.Seconds())%60)
|
|
||||||
time.Sleep(time.Second) // 等待一秒钟,然后更新
|
|
||||||
}
|
|
||||||
// 定义任务,每周五的五点执行
|
// 定义任务,每周五的五点执行
|
||||||
gocron.Every(1).Friday().At("17:00").Do(job)
|
gocron.Every(1).Friday().At("17:00").Do(job)
|
||||||
// 开始定时任务
|
// 开始定时任务
|
||||||
|
@ -46,23 +27,15 @@ _/ ____\______ ____ _____ _____ ____ __ _ _______ ____ ____
|
||||||
}
|
}
|
||||||
|
|
||||||
func job() {
|
func job() {
|
||||||
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++
|
|
||||||
log.Printf("执行失败,开始第%v次重试...", maxRetries)
|
|
||||||
log.Println("----------------------------------------------------------------------")
|
|
||||||
job()
|
job()
|
||||||
|
maxRetries--
|
||||||
}
|
}
|
||||||
repContent, err := moonshot.AiChat()
|
repContent, err := moonshot.AiChat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
maxRetries++
|
|
||||||
log.Printf("执行失败,开始第%v次重试...", maxRetries)
|
|
||||||
log.Println("----------------------------------------------------------------------")
|
|
||||||
job()
|
job()
|
||||||
|
maxRetries--
|
||||||
}
|
}
|
||||||
qywechat.Send(repContent)
|
qywechat.Send(repContent)
|
||||||
|
|
||||||
|
|
|
@ -24,10 +24,10 @@ func Send(repContent string) {
|
||||||
// 发送HTTP POST请求到企业微信机器人Webhook
|
// 发送HTTP POST请求到企业微信机器人Webhook
|
||||||
resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(requestBody))
|
resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(requestBody))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("发送企业微信机器人失败,错误是: %v", err)
|
log.Fatalf("Failed to send message: %v", err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
log.Println("发送企业微信机器人成功")
|
log.Println("Message sent successfully!")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,33 +1,18 @@
|
||||||
package tools
|
package tools
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 计算时间差值
|
// 当前时间是否为周五5点
|
||||||
|
func CheckTime() bool {
|
||||||
func RemainingTimeUntilNextFriday17() time.Duration {
|
nowTime := time.Now()
|
||||||
// 获取当前时间
|
if nowTime.Weekday().String() == "Friday" && nowTime.Format("15:04:05") == "17:00:00" {
|
||||||
now := time.Now()
|
fmt.Println("当前时间是周五5点")
|
||||||
|
return true
|
||||||
// 计算下一个周五的时间
|
} else {
|
||||||
nextFriday := GetNextWeekday(now, time.Friday)
|
fmt.Println("当前时间不是周五5点")
|
||||||
|
return false
|
||||||
// 构造下一个周五 17 点的时间
|
|
||||||
nextFriday17 := time.Date(nextFriday.Year(), nextFriday.Month(), nextFriday.Day(), 17, 0, 0, 0, nextFriday.Location())
|
|
||||||
|
|
||||||
// 计算时间差
|
|
||||||
duration := nextFriday17.Sub(now)
|
|
||||||
|
|
||||||
return duration
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取下一个指定星期几的时间
|
|
||||||
|
|
||||||
func GetNextWeekday(t time.Time, weekday time.Weekday) time.Time {
|
|
||||||
daysUntil := (int(weekday) - int(t.Weekday()) + 7) % 7
|
|
||||||
if daysUntil == 0 {
|
|
||||||
daysUntil = 7
|
|
||||||
}
|
}
|
||||||
return t.AddDate(0, 0, daysUntil)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue