|
| 1 | +package syncer |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/l3uddz/crop/cache" |
| 6 | + "github.com/l3uddz/crop/pathutils" |
| 7 | + "github.com/l3uddz/crop/rclone" |
| 8 | + "github.com/l3uddz/crop/stringutils" |
| 9 | + "github.com/pkg/errors" |
| 10 | + "github.com/sirupsen/logrus" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +func (s *Syncer) Copy(additionalRcloneParams []string) error { |
| 15 | + // set variables |
| 16 | + copyParams := s.Config.RcloneParams.Copy |
| 17 | + if additionalRcloneParams != nil { |
| 18 | + copyParams = append(copyParams, additionalRcloneParams...) |
| 19 | + } |
| 20 | + |
| 21 | + // iterate all remotes and run copy |
| 22 | + for _, remotePath := range s.Config.Remotes.Copy { |
| 23 | + // set variables |
| 24 | + attempts := 1 |
| 25 | + rLog := s.Log.WithFields(logrus.Fields{ |
| 26 | + "copy_remote": remotePath, |
| 27 | + "source_remote": s.Config.SourceRemote, |
| 28 | + "attempts": attempts, |
| 29 | + }) |
| 30 | + |
| 31 | + // copy to remote |
| 32 | + for { |
| 33 | + // get service account file |
| 34 | + var serviceAccount *pathutils.Path |
| 35 | + var err error |
| 36 | + |
| 37 | + if s.ServiceAccountCount > 0 { |
| 38 | + serviceAccount, err = rclone.GetAvailableServiceAccount(s.ServiceAccountFiles) |
| 39 | + if err != nil { |
| 40 | + return errors.WithMessagef(err, |
| 41 | + "aborting further copy attempts of %q due to serviceAccount exhaustion", |
| 42 | + s.Config.SourceRemote) |
| 43 | + } |
| 44 | + |
| 45 | + // reset log |
| 46 | + rLog = s.Log.WithFields(logrus.Fields{ |
| 47 | + "copy_remote": remotePath, |
| 48 | + "source_remote": s.Config.SourceRemote, |
| 49 | + "attempts": attempts, |
| 50 | + "service_account": serviceAccount.RealPath, |
| 51 | + }) |
| 52 | + } |
| 53 | + |
| 54 | + // copy |
| 55 | + rLog.Info("Copying...") |
| 56 | + success, exitCode, err := rclone.Copy(s.Config.SourceRemote, remotePath, serviceAccount, copyParams) |
| 57 | + |
| 58 | + // check result |
| 59 | + if err != nil { |
| 60 | + rLog.WithError(err).Errorf("Failed unexpectedly...") |
| 61 | + return errors.WithMessagef(err, "copy failed unexpectedly with exit code: %v", exitCode) |
| 62 | + } else if success { |
| 63 | + // successful exit code |
| 64 | + break |
| 65 | + } |
| 66 | + |
| 67 | + // is this an exit code we can retry? |
| 68 | + switch exitCode { |
| 69 | + case rclone.EXIT_FATAL_ERROR: |
| 70 | + // are we using service accounts? |
| 71 | + if s.ServiceAccountCount == 0 { |
| 72 | + // we are not using service accounts, so mark this remote as banned |
| 73 | + if err := cache.Set(stringutils.FromLeftUntil(remotePath, ":"), |
| 74 | + time.Now().UTC().Add(25*time.Hour)); err != nil { |
| 75 | + rLog.WithError(err).Errorf("Failed banning remote") |
| 76 | + } |
| 77 | + |
| 78 | + return fmt.Errorf("copy failed with exit code: %v", exitCode) |
| 79 | + } |
| 80 | + |
| 81 | + // ban this service account |
| 82 | + if err := cache.Set(serviceAccount.RealPath, time.Now().UTC().Add(25*time.Hour)); err != nil { |
| 83 | + rLog.WithError(err).Error("Failed banning service account, cannot try again...") |
| 84 | + return fmt.Errorf("failed banning service account: %v", serviceAccount.RealPath) |
| 85 | + } |
| 86 | + |
| 87 | + // attempt copy again |
| 88 | + rLog.Warnf("Copy failed with retryable exit code %v, trying again...", exitCode) |
| 89 | + attempts++ |
| 90 | + continue |
| 91 | + default: |
| 92 | + return fmt.Errorf("failed and cannot proceed with exit code: %v", exitCode) |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return nil |
| 98 | +} |
0 commit comments