添加任务模式-即时与定时

master
Administrator 2024-05-02 14:58:41 +08:00
parent 54fb0e6a18
commit d6a36c62d6
1 changed files with 24 additions and 15 deletions

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"log" "log"
"time" "time"
@ -13,10 +14,9 @@ import (
var maxRetries = 0 var maxRetries = 0
var now = time.Now() var now = time.Now()
var liveMode bool
func main() { func main() {
fmt.Println("-----------------------------------------------------------------------------------------")
fmt.Println("-----------------------------------------------------------------------------------------")
log.Println(` log.Println(`
_____ _____
_/ ____\______ ____ _____ _____ ____ __ _ _______ ____ ____ _/ ____\______ ____ _____ _____ ____ __ _ _______ ____ ____
@ -26,23 +26,32 @@ _/ ____\______ ____ _____ _____ ____ __ _ _______ ____ ____
\/ \/ \/ \//_____/ `) \/ \/ \/ \//_____/ `)
fmt.Println("-----------------------------------------------------------------------------------------") fmt.Println("-----------------------------------------------------------------------------------------")
fmt.Println("-----------------------------------------------------------------------------------------") fmt.Println("-----------------------------------------------------------------------------------------")
if maxRetries < 3 { flag.BoolVar(&liveMode, "liveMode", false, "即时模式关闭")
for { flag.Parse()
remainingTime := tools.RemainingTimeUntilNextFriday17() if !liveMode {
if remainingTime <= 0 { fmt.Println("目前时定时任务模式")
break // 当剩余时间小于等于0时跳出循环 if maxRetries < 3 {
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分钟然后更新
} }
fmt.Printf("距离执行定时任务还剩 %d小时 %d分钟 %d秒\n", int(remainingTime.Hours()), int(remainingTime.Minutes())%60, int(remainingTime.Seconds())%60) // 定义任务,每周五的五点执行
time.Sleep(time.Minute * 10) // 等待30分钟然后更新 gocron.Every(1).Friday().At("17:00").Do(job)
} // 开始定时任务
// 定义任务,每周五的五点执行 <-gocron.Start()
gocron.Every(1).Friday().At("17:00").Do(job)
// 开始定时任务
<-gocron.Start()
} else {
log.Fatalln("最大重试次数已到达3退出")
}
} else { } else {
log.Fatalln("最大重试次数已到达3退出") fmt.Println("目前时即时任务模式")
job()
} }
} }
func job() { func job() {