pcgamedb/cmd/task.go
nite07 543210a4ae
All checks were successful
docker / prepare-and-build (push) Successful in 2m49s
release / goreleaser (push) Successful in 24m5s
reorganized game infos
change ranking route to popular route
2024-11-22 23:50:36 +08:00

42 lines
873 B
Go

package cmd
import (
"pcgamedb/log"
"pcgamedb/task"
"github.com/robfig/cron/v3"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
type taskCommandConfig struct {
Crawl bool
CrawlCron string
}
var taskCmdCfg taskCommandConfig
var taskCmd = &cobra.Command{
Use: "task",
Long: "Start task",
Short: "Start task",
Run: func(cmd *cobra.Command, args []string) {
if taskCmdCfg.Crawl {
task.Crawl(log.Logger)
c := cron.New()
_, err := c.AddFunc(taskCmdCfg.CrawlCron, func() { task.Crawl(log.Logger) })
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")
taskCmd.Flags().StringVar(&taskCmdCfg.CrawlCron, "crawl-cron", "0 */3 * * *", "crawl cron expression")
RootCmd.AddCommand(taskCmd)
}