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

@ -11,6 +11,7 @@ import (
type taskCommandConfig struct {
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)
}

View File

@ -33,6 +33,7 @@ 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"`
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")

View File

@ -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,

View File

@ -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))
}