From ebac45ccd274723e27149e5bb5dda9d3cf09d355 Mon Sep 17 00:00:00 2001 From: nite07 Date: Sun, 17 Nov 2024 16:53:47 +0800 Subject: [PATCH] add autocrawlcron config --- cmd/task.go | 6 ++++-- config/config.go | 10 +++++++--- server/handler/healthcheck.go | 2 ++ server/server.go | 2 +- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/cmd/task.go b/cmd/task.go index dd0295f..7eb2597 100644 --- a/cmd/task.go +++ b/cmd/task.go @@ -10,7 +10,8 @@ import ( ) type taskCommandConfig struct { - Crawl bool + Crawl bool + CrawlCron string } var taskCmdCfg taskCommandConfig @@ -22,7 +23,7 @@ var taskCmd = &cobra.Command{ if taskCmdCfg.Crawl { task.Crawl(log.Logger) c := cron.New() - _, err := c.AddFunc("0 */3 * * *", func() { task.Crawl(log.Logger) }) + _, err := c.AddFunc(taskCmdCfg.CrawlCron, func() { task.Crawl(log.Logger) }) if err != nil { log.Logger.Error("Failed to add task", zap.Error(err)) } @@ -34,5 +35,6 @@ var taskCmd = &cobra.Command{ func init() { taskCmd.Flags().BoolVar(&taskCmdCfg.Crawl, "crawl", false, "enable auto crawl") + taskCmd.Flags().StringVar(&taskCmdCfg.CrawlCron, "crawl-cron", "0 */3 * * *", "crawl cron expression") RootCmd.AddCommand(taskCmd) } diff --git a/config/config.go b/config/config.go index 985777d..6d90844 100644 --- a/config/config.go +++ b/config/config.go @@ -30,9 +30,10 @@ type webhooks struct { } type server struct { - Port string `env:"SERVER_PORT" json:"port"` - SecretKey string `env:"SERVER_SECRET_KEY" json:"secret_key"` - AutoCrawl bool `env:"SERVER_AUTO_CRAWL" json:"auto_crawl"` + Port string `env:"SERVER_PORT" json:"port"` + SecretKey string `env:"SERVER_SECRET_KEY" json:"secret_key"` + AutoCrawl bool `env:"SERVER_AUTO_CRAWL" json:"auto_crawl"` + AutoCrawlCron string `env:"SERVER_AUTO_CRAWL_CRON" json:"auto_crawl_cron"` } type database struct { @@ -76,6 +77,9 @@ func init() { Password: "password", }, MegaAvaliable: TestMega(), + Server: server{ + AutoCrawlCron: "0 */3 * * *", + }, } if _, err := os.Stat("config.json"); err == nil { configData, err := os.ReadFile("config.json") diff --git a/server/handler/healthcheck.go b/server/handler/healthcheck.go index 67f00a8..d66b2b2 100644 --- a/server/handler/healthcheck.go +++ b/server/handler/healthcheck.go @@ -21,6 +21,7 @@ type HealthCheckResponse struct { Uptime string `json:"uptime"` Alloc string `json:"alloc"` AutoCrawl bool `json:"auto_crawl"` + AutoCrawlCron string `json:"auto_crawl_cron,omitempty"` GameItem int64 `json:"game_download,omitempty"` GameInfo int64 `json:"game_info,omitempty"` Unorganized int64 `json:"unorganized,omitempty"` @@ -54,6 +55,7 @@ func HealthCheckHandler(c *gin.Context) { Date: time.Now().Format("2006-01-02 15:04:05"), Uptime: time.Since(config.Runtime.ServerStartTime).String(), AutoCrawl: config.Config.Server.AutoCrawl, + AutoCrawlCron: config.Config.Server.AutoCrawlCron, Alloc: fmt.Sprintf("%.2f MB", float64(m.Alloc)/1024.0/1024.0), GameItem: downloadCount, GameInfo: infoCount, diff --git a/server/server.go b/server/server.go index a199d75..fa56af8 100644 --- a/server/server.go +++ b/server/server.go @@ -33,7 +33,7 @@ func Run() { if config.Config.Server.AutoCrawl { go func() { c := cron.New() - _, err := c.AddFunc("0 */3 * * *", func() { task.Crawl(log.TaskLogger) }) + _, err := c.AddFunc(config.Config.Server.AutoCrawlCron, func() { task.Crawl(log.TaskLogger) }) if err != nil { log.Logger.Error("Error adding cron job", zap.Error(err)) }