pcgamedb/cmd/task.go

39 lines
758 B
Go
Raw Normal View History

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 {
Crawl bool
}
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()
_, err := c.AddFunc("0 */3 * * *", 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().BoolVarP(&taskCmdCfg.Crawl, "crawl", "c", false, "enable auto crawl")
RootCmd.AddCommand(taskCmd)
}