Compare commits
3 Commits
19a6453dd4
...
cd0aa7b357
Author | SHA1 | Date |
---|---|---|
Administrator | cd0aa7b357 | |
Administrator | 5dbd22f1f6 | |
Administrator | 8a68269c8f |
31
cmd/main.go
31
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,32 +29,22 @@ _/ ____\______ ____ _____ _____ ____ __ _ _______ ____ ____
|
||||||
flag.BoolVar(&liveMode, "livemode", false, "即时模式默认关闭")
|
flag.BoolVar(&liveMode, "livemode", false, "即时模式默认关闭")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if !liveMode {
|
if !liveMode {
|
||||||
fmt.Println("目前是定时任务模式")
|
log.Println("目前是定时任务模式")
|
||||||
if maxRetries < 3 {
|
log.Printf("等待任务的第%v次执行...", operateTime)
|
||||||
for {
|
|
||||||
remainingTime := tools.RemainingTimeUntilNextFriday17()
|
|
||||||
if remainingTime <= 0 {
|
|
||||||
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.Every(1).Friday().At("17:00").Do(job)
|
||||||
// 开始定时任务
|
// 开始定时任务
|
||||||
<-gocron.Start()
|
<-gocron.Start()
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
log.Fatalln("最大重试次数已到达3,退出")
|
log.Println("目前是即时任务模式")
|
||||||
}
|
|
||||||
} else {
|
|
||||||
fmt.Println("目前是即时任务模式")
|
|
||||||
job()
|
job()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func job() {
|
func job() {
|
||||||
|
//最多重试3次
|
||||||
|
if maxRetries < 3 {
|
||||||
nowDate := now.Format("2006-01-02 15:04:05") + " " + now.Weekday().String()
|
nowDate := now.Format("2006-01-02 15:04:05") + " " + now.Weekday().String()
|
||||||
log.Printf("开始执行任务,当前时间是:%v", nowDate)
|
log.Printf("开始执行任务,当前时间是:%v", nowDate)
|
||||||
|
|
||||||
|
@ -74,5 +64,10 @@ func job() {
|
||||||
job()
|
job()
|
||||||
}
|
}
|
||||||
qywechat.Send(repContent)
|
qywechat.Send(repContent)
|
||||||
|
operateTime++
|
||||||
|
log.Printf("任务执行成功,当前时间是:%v", nowDate)
|
||||||
|
log.Printf("等待%v次任务执行还剩:%v", operateTime, tools.TimeUntilFriday())
|
||||||
|
} else {
|
||||||
|
log.Fatalln("最大重试次数已到达3,退出")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,11 +37,14 @@ 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.Fatalf("发送企业微信机器人失败,错误是: %v", err)
|
log.Printf("发送企业微信机器人失败,错误是: %v", err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
body, _ := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
fmt.Println(resp.StatusCode, string(body))
|
if err != nil {
|
||||||
log.Println("发送企业微信机器人成功")
|
log.Printf("发送企业微信机器人失败,错误是: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("发送企业微信机器人完成,状态码为:%v,返回体为:%v", resp.StatusCode, string(body))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,29 +1,40 @@
|
||||||
package tools
|
package tools
|
||||||
|
|
||||||
import (
|
import "time"
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 计算时间差值
|
func TimeUntilFriday() time.Duration {
|
||||||
|
today := time.Now()
|
||||||
func RemainingTimeUntilNextFriday17() time.Duration {
|
weekday := today.Weekday()
|
||||||
// 获取当前时间
|
switch weekday {
|
||||||
now := time.Now()
|
case time.Monday:
|
||||||
|
friday := time.Date(today.Year(), today.Month(), today.Day()+4, 17, 0, 0, 0, today.Location())
|
||||||
// 计算下一个周五的时间
|
return friday.Sub(today)
|
||||||
nextFriday := now.AddDate(0, 0, (int(time.Friday)-int(now.Weekday())+7)%7)
|
case time.Tuesday:
|
||||||
|
friday := time.Date(today.Year(), today.Month(), today.Day()+3, 17, 0, 0, 0, today.Location())
|
||||||
// 如果今天是周五,则直接返回距离今天17点的时间
|
return friday.Sub(today)
|
||||||
if now.Weekday() == time.Friday {
|
case time.Wednesday:
|
||||||
today17 := time.Date(now.Year(), now.Month(), now.Day(), 17, 0, 0, 0, now.Location())
|
friday := time.Date(today.Year(), today.Month(), today.Day()+2, 17, 0, 0, 0, today.Location())
|
||||||
return today17.Sub(now)
|
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)
|
||||||
|
|
||||||
// 构造下一个周五 17 点的时间
|
}
|
||||||
nextFriday17 := time.Date(nextFriday.Year(), nextFriday.Month(), nextFriday.Day(), 17, 0, 0, 0, nextFriday.Location())
|
return 0
|
||||||
|
|
||||||
// 计算时间差
|
|
||||||
duration := nextFriday17.Sub(now)
|
|
||||||
|
|
||||||
return duration
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue