pcgamedb/cmd/task.go

73 lines
1.7 KiB
Go
Raw 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 {
Cron string
Now bool
2024-09-24 06:17:11 -04:00
}
var taskCommandCfg taskCommandConfig
2024-09-24 06:17:11 -04:00
var crawlTaskCmd = &cobra.Command{
Use: "crawl",
Long: "Start crawl task",
Short: "Start crawl task",
2024-09-24 06:17:11 -04:00
Run: func(cmd *cobra.Command, args []string) {
if taskCommandCfg.Now {
2024-09-24 06:17:11 -04:00
task.Crawl(log.Logger)
}
c := cron.New()
_, err := c.AddFunc(taskCommandCfg.Cron, func() { task.Crawl(log.Logger) })
if err != nil {
log.Logger.Error("Failed to add task", zap.Error(err))
}
c.Start()
select {}
},
}
var updateTaskCmd = &cobra.Command{
Use: "update",
Long: "Start update outdated game infos task",
Short: "Start update outdated game infos task",
Run: func(cmd *cobra.Command, args []string) {
if taskCommandCfg.Now {
task.UpdateOutdatedGameInfos(log.Logger)
}
c := cron.New()
_, err := c.AddFunc(taskCommandCfg.Cron, func() { task.UpdateOutdatedGameInfos(log.Logger) })
if err != nil {
log.Logger.Error("Failed to add task", zap.Error(err))
}
c.Start()
select {}
2024-09-24 06:17:11 -04:00
},
}
var taskCmd = &cobra.Command{
Use: "task",
Long: "Start task",
Short: "Start task",
}
2024-09-24 06:17:11 -04:00
func init() {
crawlTaskCmd.Flags().StringVar(&taskCommandCfg.Cron, "cron", "0 */3 * * *", "cron expression")
crawlTaskCmd.Flags().BoolVar(&taskCommandCfg.Now, "now", false, "run task immediately")
updateTaskCmd.Flags().StringVar(&taskCommandCfg.Cron, "cron", "0 */3 * * *", "cron expression")
updateTaskCmd.Flags().BoolVar(&taskCommandCfg.Now, "now", false, "run task immediately")
taskCmd.AddCommand(crawlTaskCmd)
taskCmd.AddCommand(updateTaskCmd)
2024-09-24 06:17:11 -04:00
RootCmd.AddCommand(taskCmd)
}