From 5ce46cb64346c0372dcd4eadee3a680021657327 Mon Sep 17 00:00:00 2001 From: winspain Date: Thu, 7 Dec 2023 15:03:24 +0800 Subject: [PATCH] feat:timing --- lotteryGo/Dockerfile | 14 ++++++++++++++ lotteryGo/go.mod | 5 ++++- lotteryGo/main.go | 27 ++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 lotteryGo/Dockerfile diff --git a/lotteryGo/Dockerfile b/lotteryGo/Dockerfile new file mode 100644 index 0000000..d0d36e3 --- /dev/null +++ b/lotteryGo/Dockerfile @@ -0,0 +1,14 @@ +# 使用官方 Golang 镜像作为基础镜像 +FROM golang:latest + +# 将当前目录的文件复制到容器的 /app 目录下 +COPY . /app + +# 设置工作目录 +WORKDIR /app + +# 编译 Go 应用 +RUN go build -o main . + +# 启动应用 +CMD ["./main"] \ No newline at end of file diff --git a/lotteryGo/go.mod b/lotteryGo/go.mod index fcdf2a4..b2c08f6 100644 --- a/lotteryGo/go.mod +++ b/lotteryGo/go.mod @@ -2,7 +2,10 @@ module lotteryGo go 1.21 -require github.com/PuerkitoBio/goquery v1.8.1 +require ( + github.com/PuerkitoBio/goquery v1.8.1 + github.com/robfig/cron/v3 v3.0.1 +) require ( github.com/andybalholm/cascadia v1.3.1 // indirect diff --git a/lotteryGo/main.go b/lotteryGo/main.go index a8c81bc..b297f83 100644 --- a/lotteryGo/main.go +++ b/lotteryGo/main.go @@ -6,9 +6,11 @@ import ( "encoding/json" "fmt" "github.com/PuerkitoBio/goquery" + "github.com/robfig/cron/v3" "net/http" "os" "strings" + "time" ) var prizeMap = map[string][]int{ @@ -217,7 +219,7 @@ func sendDingDingNotification(hookUrl string, drawPeriod, drawTime string, hitPr return nil } -func main() { +func run() { latestNum, err := getLatestNumberBy500() if err != nil { fmt.Println("Error:", err) @@ -250,3 +252,26 @@ func main() { fmt.Println("Error:", err1) } } + +func main() { + // 创建一个cron调度器 + c := cron.New() + + // 添加定时任务 + _, err := c.AddFunc("0 */1 20-23 * 1,3,6", run) + if err != nil { + fmt.Println("添加定时任务失败:", err) + return + } + + // 启动定时任务 + c.Start() + + // 运行一段时间,让定时任务执行 + // 这里可以根据你的实际需求设置运行时间 + // 这里只是一个示例,你可能需要让程序一直运行或者使用其他方法来阻止程序退出 + for { + run() + time.Sleep(1 * time.Minute) + } +}