From efd6a4b620fb4cfb6b24a66d66d1803bfd0becdf Mon Sep 17 00:00:00 2001 From: Niko Storni Date: Thu, 26 Apr 2018 06:12:54 -0400 Subject: [PATCH] fix wallet refill handler add slack logging add PoC for channel fetching --- cmd/ytsync.go | 25 +++++++++++++++++++++++++ util/slack_test.go | 15 +++++++++++++++ ytsync/ytsync.go | 28 ++++++++++++++++++++-------- 3 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 util/slack_test.go diff --git a/cmd/ytsync.go b/cmd/ytsync.go index 7d83618..2187bc9 100644 --- a/cmd/ytsync.go +++ b/cmd/ytsync.go @@ -4,8 +4,14 @@ import ( "github.com/lbryio/lbry.go/errors" sync "github.com/lbryio/lbry.go/ytsync" + "fmt" + "github.com/lbryio/lbry.go/util" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "io/ioutil" + "net/http" + "os" + "strings" ) func init() { @@ -20,6 +26,12 @@ func init() { ytSyncCmd.Flags().BoolVar(&takeOverExistingChannel, "takeover-existing-channel", false, "If channel exists and we don't own it, take over the channel") ytSyncCmd.Flags().IntVar(&refill, "refill", 0, "Also add this many credits to the wallet") RootCmd.AddCommand(ytSyncCmd) + slackToken := os.Getenv("SLACK_TOKEN") + if slackToken == "" { + log.Error("A slack token was not present in env vars! Slack messages disabled!") + } else { + util.InitSlack(os.Getenv("SLACK_TOKEN")) + } } const defaultMaxTries = 3 @@ -31,6 +43,19 @@ var ( refill int ) +//PoC +func fetchChannels() { + url := "http://localhost:8080/yt/jobs" + payload := strings.NewReader("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"auth_token\"\r\n\r\n620280\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--") + req, _ := http.NewRequest("POST", url, payload) + req.Header.Add("content-type", "multipart/form-data") + res, _ := http.DefaultClient.Do(req) + defer res.Body.Close() + body, _ := ioutil.ReadAll(res.Body) + fmt.Println(res) + fmt.Println(string(body)) +} + func ytsync(cmd *cobra.Command, args []string) { ytAPIKey := args[0] lbryChannelName := args[1] diff --git a/util/slack_test.go b/util/slack_test.go new file mode 100644 index 0000000..b045ca2 --- /dev/null +++ b/util/slack_test.go @@ -0,0 +1,15 @@ +package util + +import ( + "os" + "testing" +) + +func TestSendToSlack(t *testing.T) { + slackToken := os.Getenv("SLACK_TOKEN") + if slackToken == "" { + t.Error("A slack token was not provided") + } + InitSlack(slackToken) + SendToSlack("This is a test :)") +} diff --git a/ytsync/ytsync.go b/ytsync/ytsync.go index 3dc9cee..7afec62 100644 --- a/ytsync/ytsync.go +++ b/ytsync/ytsync.go @@ -22,6 +22,8 @@ import ( "github.com/lbryio/lbry.go/ytsync/redisdb" "github.com/lbryio/lbry.go/ytsync/sources" + "fmt" + "github.com/lbryio/lbry.go/util" "github.com/mitchellh/go-ps" log "github.com/sirupsen/logrus" "google.golang.org/api/googleapi/transport" @@ -103,16 +105,14 @@ func (s *Sync) FullCycle() error { log.Printf("Stopping daemon") shutdownErr := stopDaemonViaSystemd() if shutdownErr != nil { - log.Errorf("error shutting down daemon: %v", shutdownErr) - log.Errorf("WALLET HAS NOT BEEN MOVED TO THE WALLET BACKUP DIR") + logShutdownError(shutdownErr) } else { // the cli will return long before the daemon effectively stops. we must observe the processes running // before moving the wallet var waitTimeout time.Duration = 60 * 6 processDeathError := waitForDaemonProcess(waitTimeout) if processDeathError != nil { - log.Errorf("error shutdown down daemon: %v", processDeathError) - log.Errorf("WALLET HAS NOT BEEN MOVED TO THE WALLET BACKUP DIR") + logShutdownError(processDeathError) } else { walletErr := os.Rename(defaultWalletDir, walletBackupDir) if walletErr != nil { @@ -175,6 +175,13 @@ WaitForDaemonStart: return nil } +func logShutdownError(shutdownErr error) { + errMsg := fmt.Sprintf("error shutting down daemon: %v", shutdownErr) + log.Errorf(errMsg) + util.SendToSlack(errMsg) + log.Errorf("WALLET HAS NOT BEEN MOVED TO THE WALLET BACKUP DIR") + util.SendToSlack("WALLET HAS NOT BEEN MOVED TO THE WALLET BACKUP DIR") +} func (s *Sync) doSync() error { var err error @@ -235,7 +242,9 @@ func (s *Sync) startWorker(workerNum int) { err := s.processVideo(v) if err != nil { - log.Errorln("error processing video: " + err.Error()) + logMsg := fmt.Sprintf("error processing video: " + err.Error()) + log.Errorln(logMsg) + if s.StopOnError { s.grp.Stop() } else if s.MaxTries > 1 { @@ -247,9 +256,10 @@ func (s *Sync) startWorker(workerNum int) { strings.Contains(err.Error(), "Playback on other websites has been disabled by the video owner") { log.Println("This error should not be retried at all") } else if tryCount < s.MaxTries { - if strings.Contains(err.Error(), "The transaction was rejected by network rules.(258: txn-mempool-conflict)") { + if strings.Contains(err.Error(), "The transaction was rejected by network rules.(258: txn-mempool-conflict)") || + strings.Contains(err.Error(), "failed: Not enough funds") { log.Println("waiting for a block and refilling addresses before retrying") - err = s.ensureEnoughUTXOs() + err = s.walletSetup() if err != nil { log.Println(err.Error()) } @@ -257,7 +267,9 @@ func (s *Sync) startWorker(workerNum int) { log.Println("Retrying") continue } - log.Printf("Video failed after %d retries, skipping", s.MaxTries) + logMsg = fmt.Sprintf("Video failed after %d retries, skipping. Stack: %s", s.MaxTries, logMsg) + log.Printf(logMsg) + util.SendToSlack(logMsg) } } break