add autocrawlcron config

This commit is contained in:
Nite07 2024-11-17 16:53:47 +08:00
parent f887f804b8
commit ebac45ccd2
4 changed files with 14 additions and 6 deletions

View File

@ -10,7 +10,8 @@ import (
) )
type taskCommandConfig struct { type taskCommandConfig struct {
Crawl bool Crawl bool
CrawlCron string
} }
var taskCmdCfg taskCommandConfig var taskCmdCfg taskCommandConfig
@ -22,7 +23,7 @@ var taskCmd = &cobra.Command{
if taskCmdCfg.Crawl { if taskCmdCfg.Crawl {
task.Crawl(log.Logger) task.Crawl(log.Logger)
c := cron.New() 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 { if err != nil {
log.Logger.Error("Failed to add task", zap.Error(err)) log.Logger.Error("Failed to add task", zap.Error(err))
} }
@ -34,5 +35,6 @@ var taskCmd = &cobra.Command{
func init() { func init() {
taskCmd.Flags().BoolVar(&taskCmdCfg.Crawl, "crawl", false, "enable auto crawl") 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) RootCmd.AddCommand(taskCmd)
} }

View File

@ -30,9 +30,10 @@ type webhooks struct {
} }
type server struct { type server struct {
Port string `env:"SERVER_PORT" json:"port"` Port string `env:"SERVER_PORT" json:"port"`
SecretKey string `env:"SERVER_SECRET_KEY" json:"secret_key"` SecretKey string `env:"SERVER_SECRET_KEY" json:"secret_key"`
AutoCrawl bool `env:"SERVER_AUTO_CRAWL" json:"auto_crawl"` AutoCrawl bool `env:"SERVER_AUTO_CRAWL" json:"auto_crawl"`
AutoCrawlCron string `env:"SERVER_AUTO_CRAWL_CRON" json:"auto_crawl_cron"`
} }
type database struct { type database struct {
@ -76,6 +77,9 @@ func init() {
Password: "password", Password: "password",
}, },
MegaAvaliable: TestMega(), MegaAvaliable: TestMega(),
Server: server{
AutoCrawlCron: "0 */3 * * *",
},
} }
if _, err := os.Stat("config.json"); err == nil { if _, err := os.Stat("config.json"); err == nil {
configData, err := os.ReadFile("config.json") configData, err := os.ReadFile("config.json")

View File

@ -21,6 +21,7 @@ type HealthCheckResponse struct {
Uptime string `json:"uptime"` Uptime string `json:"uptime"`
Alloc string `json:"alloc"` Alloc string `json:"alloc"`
AutoCrawl bool `json:"auto_crawl"` AutoCrawl bool `json:"auto_crawl"`
AutoCrawlCron string `json:"auto_crawl_cron,omitempty"`
GameItem int64 `json:"game_download,omitempty"` GameItem int64 `json:"game_download,omitempty"`
GameInfo int64 `json:"game_info,omitempty"` GameInfo int64 `json:"game_info,omitempty"`
Unorganized int64 `json:"unorganized,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"), Date: time.Now().Format("2006-01-02 15:04:05"),
Uptime: time.Since(config.Runtime.ServerStartTime).String(), Uptime: time.Since(config.Runtime.ServerStartTime).String(),
AutoCrawl: config.Config.Server.AutoCrawl, AutoCrawl: config.Config.Server.AutoCrawl,
AutoCrawlCron: config.Config.Server.AutoCrawlCron,
Alloc: fmt.Sprintf("%.2f MB", float64(m.Alloc)/1024.0/1024.0), Alloc: fmt.Sprintf("%.2f MB", float64(m.Alloc)/1024.0/1024.0),
GameItem: downloadCount, GameItem: downloadCount,
GameInfo: infoCount, GameInfo: infoCount,

View File

@ -33,7 +33,7 @@ func Run() {
if config.Config.Server.AutoCrawl { if config.Config.Server.AutoCrawl {
go func() { go func() {
c := cron.New() 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 { if err != nil {
log.Logger.Error("Error adding cron job", zap.Error(err)) log.Logger.Error("Error adding cron job", zap.Error(err))
} }