package internal import ( "context" "log" "math" "time" "exporter/config" "github.com/pkg/errors" "github.com/spf13/cobra" "go.uber.org/zap" ) var dbChannelID 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(&dbChannelID, "channel", 0, "channel id") return cmd } func channelExportCmd(ctx context.Context) error { if dbChannelID == 0 { return errors.New("db channel id required") } channel, err := client.ChannelInfoByID(ctx, channelID) if err != nil { return err } if channel.GetID() == 0 { return errors.New("channel not found") } cfg := NewDBChannel(channel.GetID(), channel.Username, channel.Title) if err := cfg.GetOrCreate(ctx); 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++ sleepSeconds := math.Pow(2, float64(continueRetryTimes)) if sleepSeconds > 128 { return err } logger.Info("retry after", zap.Int("seconds", int(sleepSeconds))) time.Sleep(time.Second * time.Duration(sleepSeconds)) } } }