pcgamedb/cmd/task.go

42 lines
873 B
Go
Raw Permalink Normal View History

2024-09-24 06:17:11 -04:00
package cmd
import (
2024-11-20 06:09:04 -05:00
"pcgamedb/log"
"pcgamedb/task"
2024-09-24 06:17:11 -04:00
"github.com/robfig/cron/v3"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
type taskCommandConfig struct {
2024-11-17 03:53:47 -05:00
Crawl bool
CrawlCron string
2024-09-24 06:17:11 -04:00
}
var taskCmdCfg taskCommandConfig
var taskCmd = &cobra.Command{
Use: "task",
Long: "Start task",
Short: "Start task",
2024-09-24 06:17:11 -04:00
Run: func(cmd *cobra.Command, args []string) {
if taskCmdCfg.Crawl {
task.Crawl(log.Logger)
c := cron.New()
2024-11-17 03:53:47 -05:00
_, err := c.AddFunc(taskCmdCfg.CrawlCron, func() { task.Crawl(log.Logger) })
2024-09-24 06:17:11 -04:00
if err != nil {
log.Logger.Error("Failed to add task", zap.Error(err))
}
c.Start()
select {}
}
},
}
func init() {
taskCmd.Flags().BoolVar(&taskCmdCfg.Crawl, "crawl", false, "enable auto crawl")
2024-11-17 03:53:47 -05:00
taskCmd.Flags().StringVar(&taskCmdCfg.CrawlCron, "crawl-cron", "0 */3 * * *", "crawl cron expression")
2024-09-24 06:17:11 -04:00
RootCmd.AddCommand(taskCmd)
}