package internal import ( "context" "log" "time" "exporter/config" "github.com/pkg/errors" "github.com/spf13/cobra" "go.uber.org/zap" ) var pkID int64 func ChannelExportCmd() *cobra.Command { cmd := &cobra.Command{ Use: "export", Aliases: []string{"e"}, Short: "export channels", PreRunE: func(cmd *cobra.Command, args []string) error { log.Println("init client") defer log.Println("init client done") return InitClient(config.C) }, RunE: wrapE(channelExportCmd), } cmd.Flags().Int64Var(&pkID, "pk", 0, "channel pk id") return cmd } func channelExportCmd(ctx context.Context) error { if pkID == 0 { return errors.New("db channel id required") } cfg, err := NewDBChannelFromDB(pkID) if err != nil { return err } channel, err := client.ChannelInfoByID(ctx, cfg.UUID) if err != nil { return err } var continueRetryTimes int for { if err := client.Channel(ctx, channel, cfg, false); err != nil { logger.Error("failed to export channel", zap.Error(err), zap.Int64("uuid", channel.GetID())) continueRetryTimes++ if continueRetryTimes > 6 { return err } logger.Info("retry after 1 min") time.Sleep(time.Minute) continue } continueRetryTimes = 0 } }