2024-09-24 06:17:11 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2024-11-15 02:02:45 -05:00
|
|
|
"github.com/nitezs/pcgamedb/log"
|
|
|
|
"github.com/nitezs/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",
|
|
|
|
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() {
|
2024-11-17 00:29:04 -05:00
|
|
|
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)
|
|
|
|
}
|