2018-07-17 18:54:22 +02:00
|
|
|
package ytsync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-08-21 19:17:52 +02:00
|
|
|
"strings"
|
2018-07-17 20:58:47 +02:00
|
|
|
"syscall"
|
2018-08-21 19:17:52 +02:00
|
|
|
"time"
|
2018-07-17 20:58:47 +02:00
|
|
|
|
2018-07-17 18:54:22 +02:00
|
|
|
"github.com/lbryio/lbry.go/errors"
|
|
|
|
"github.com/lbryio/lbry.go/util"
|
2018-09-18 22:57:25 +02:00
|
|
|
"github.com/lbryio/lbry.go/ytsync/namer"
|
2018-09-05 23:35:59 +02:00
|
|
|
"github.com/lbryio/lbry.go/ytsync/sdk"
|
2018-07-17 18:54:22 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SyncManager struct {
|
2018-09-26 06:08:18 +02:00
|
|
|
stopOnError bool
|
|
|
|
maxTries int
|
|
|
|
takeOverExistingChannel bool
|
|
|
|
refill int
|
|
|
|
limit int
|
|
|
|
skipSpaceCheck bool
|
|
|
|
syncUpdate bool
|
|
|
|
concurrentJobs int
|
|
|
|
concurrentVideos int
|
|
|
|
blobsDir string
|
|
|
|
videosLimit int
|
|
|
|
maxVideoSize int
|
|
|
|
lbrycrdString string
|
|
|
|
awsS3ID string
|
|
|
|
awsS3Secret string
|
|
|
|
awsS3Region string
|
|
|
|
syncStatus string
|
|
|
|
awsS3Bucket string
|
|
|
|
singleRun bool
|
|
|
|
syncProperties *sdk.SyncProperties
|
|
|
|
apiConfig *sdk.APIConfig
|
2018-09-18 22:57:25 +02:00
|
|
|
namer *namer.Namer
|
2018-09-18 21:20:34 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 06:08:18 +02:00
|
|
|
func NewSyncManager(stopOnError bool, maxTries int, takeOverExistingChannel bool, refill int, limit int,
|
|
|
|
skipSpaceCheck bool, syncUpdate bool, concurrentJobs int, concurrentVideos int, blobsDir string, videosLimit int,
|
|
|
|
maxVideoSize int, lbrycrdString string, awsS3ID string, awsS3Secret string, awsS3Region string, awsS3Bucket string,
|
|
|
|
syncStatus string, singleRun bool, syncProperties *sdk.SyncProperties, apiConfig *sdk.APIConfig) *SyncManager {
|
2018-09-18 21:20:34 +02:00
|
|
|
return &SyncManager{
|
2018-09-26 06:08:18 +02:00
|
|
|
stopOnError: stopOnError,
|
|
|
|
maxTries: maxTries,
|
|
|
|
takeOverExistingChannel: takeOverExistingChannel,
|
|
|
|
refill: refill,
|
|
|
|
limit: limit,
|
|
|
|
skipSpaceCheck: skipSpaceCheck,
|
|
|
|
syncUpdate: syncUpdate,
|
|
|
|
concurrentJobs: concurrentJobs,
|
|
|
|
concurrentVideos: concurrentVideos,
|
|
|
|
blobsDir: blobsDir,
|
|
|
|
videosLimit: videosLimit,
|
|
|
|
maxVideoSize: maxVideoSize,
|
|
|
|
lbrycrdString: lbrycrdString,
|
|
|
|
awsS3ID: awsS3ID,
|
|
|
|
awsS3Secret: awsS3Secret,
|
|
|
|
awsS3Region: awsS3Region,
|
|
|
|
awsS3Bucket: awsS3Bucket,
|
|
|
|
syncStatus: syncStatus,
|
|
|
|
singleRun: singleRun,
|
|
|
|
syncProperties: syncProperties,
|
|
|
|
apiConfig: apiConfig,
|
|
|
|
namer: namer.NewNamer(),
|
2018-09-18 21:20:34 +02:00
|
|
|
}
|
2018-07-17 18:54:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2018-08-03 13:02:44 +02:00
|
|
|
StatusPending = "pending" // waiting for permission to sync
|
|
|
|
StatusQueued = "queued" // in sync queue. will be synced soon
|
|
|
|
StatusSyncing = "syncing" // syncing now
|
|
|
|
StatusSynced = "synced" // done
|
|
|
|
StatusFailed = "failed"
|
|
|
|
StatusFinalized = "finalized" // no more changes allowed
|
2018-07-17 18:54:22 +02:00
|
|
|
)
|
|
|
|
|
2018-08-03 13:02:44 +02:00
|
|
|
var SyncStatuses = []string{StatusPending, StatusQueued, StatusSyncing, StatusSynced, StatusFailed, StatusFinalized}
|
2018-07-17 18:54:22 +02:00
|
|
|
|
2018-07-21 01:56:36 +02:00
|
|
|
const (
|
|
|
|
VideoStatusPublished = "published"
|
2018-08-02 16:37:22 +02:00
|
|
|
VideoStatusFailed = "failed"
|
2018-07-21 01:56:36 +02:00
|
|
|
)
|
|
|
|
|
2018-08-17 16:05:54 +02:00
|
|
|
func (s *SyncManager) Start() error {
|
2018-09-18 21:20:34 +02:00
|
|
|
|
2018-07-17 18:54:22 +02:00
|
|
|
syncCount := 0
|
|
|
|
for {
|
|
|
|
err := s.checkUsedSpace()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var syncs []Sync
|
|
|
|
shouldInterruptLoop := false
|
|
|
|
|
2018-09-26 06:08:18 +02:00
|
|
|
isSingleChannelSync := s.syncProperties.YoutubeChannelID != ""
|
2018-07-17 18:54:22 +02:00
|
|
|
if isSingleChannelSync {
|
2018-09-26 06:08:18 +02:00
|
|
|
channels, err := s.apiConfig.FetchChannels("", s.syncProperties)
|
2018-07-17 18:54:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(channels) != 1 {
|
|
|
|
return errors.Err("Expected 1 channel, %d returned", len(channels))
|
|
|
|
}
|
|
|
|
lbryChannelName := channels[0].DesiredChannelName
|
|
|
|
syncs = make([]Sync, 1)
|
|
|
|
syncs[0] = Sync{
|
2018-09-26 06:08:18 +02:00
|
|
|
APIConfig: s.apiConfig,
|
|
|
|
YoutubeChannelID: s.syncProperties.YoutubeChannelID,
|
2018-07-17 18:54:22 +02:00
|
|
|
LbryChannelName: lbryChannelName,
|
2018-09-26 06:08:18 +02:00
|
|
|
StopOnError: s.stopOnError,
|
|
|
|
MaxTries: s.maxTries,
|
|
|
|
ConcurrentVideos: s.concurrentVideos,
|
|
|
|
TakeOverExistingChannel: s.takeOverExistingChannel,
|
|
|
|
Refill: s.refill,
|
2018-08-17 16:05:54 +02:00
|
|
|
Manager: s,
|
2018-09-26 06:08:18 +02:00
|
|
|
LbrycrdString: s.lbrycrdString,
|
|
|
|
AwsS3ID: s.awsS3ID,
|
|
|
|
AwsS3Secret: s.awsS3Secret,
|
|
|
|
AwsS3Region: s.awsS3Region,
|
|
|
|
AwsS3Bucket: s.awsS3Bucket,
|
2018-09-18 21:20:34 +02:00
|
|
|
namer: s.namer,
|
2018-07-17 18:54:22 +02:00
|
|
|
}
|
|
|
|
shouldInterruptLoop = true
|
|
|
|
} else {
|
|
|
|
var queuesToSync []string
|
2018-09-26 06:08:18 +02:00
|
|
|
if s.syncStatus != "" {
|
|
|
|
queuesToSync = append(queuesToSync, s.syncStatus)
|
|
|
|
} else if s.syncUpdate {
|
2018-07-17 18:54:22 +02:00
|
|
|
queuesToSync = append(queuesToSync, StatusSyncing, StatusSynced)
|
|
|
|
} else {
|
|
|
|
queuesToSync = append(queuesToSync, StatusSyncing, StatusQueued)
|
|
|
|
}
|
|
|
|
for _, q := range queuesToSync {
|
2018-09-26 06:08:18 +02:00
|
|
|
channels, err := s.apiConfig.FetchChannels(q, s.syncProperties)
|
2018-07-17 18:54:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, c := range channels {
|
|
|
|
syncs = append(syncs, Sync{
|
2018-09-26 06:08:18 +02:00
|
|
|
APIConfig: s.apiConfig,
|
2018-07-17 18:54:22 +02:00
|
|
|
YoutubeChannelID: c.ChannelId,
|
|
|
|
LbryChannelName: c.DesiredChannelName,
|
2018-09-26 06:08:18 +02:00
|
|
|
StopOnError: s.stopOnError,
|
|
|
|
MaxTries: s.maxTries,
|
|
|
|
ConcurrentVideos: s.concurrentVideos,
|
|
|
|
TakeOverExistingChannel: s.takeOverExistingChannel,
|
|
|
|
Refill: s.refill,
|
2018-08-17 16:05:54 +02:00
|
|
|
Manager: s,
|
2018-09-26 06:08:18 +02:00
|
|
|
LbrycrdString: s.lbrycrdString,
|
|
|
|
AwsS3ID: s.awsS3ID,
|
|
|
|
AwsS3Secret: s.awsS3Secret,
|
|
|
|
AwsS3Region: s.awsS3Region,
|
|
|
|
AwsS3Bucket: s.awsS3Bucket,
|
2018-07-17 18:54:22 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(syncs) == 0 {
|
|
|
|
log.Infoln("No channels to sync. Pausing 5 minutes!")
|
|
|
|
time.Sleep(5 * time.Minute)
|
|
|
|
}
|
|
|
|
for i, sync := range syncs {
|
2018-08-21 19:17:52 +02:00
|
|
|
shouldNotCount := false
|
2018-08-22 16:56:02 +02:00
|
|
|
SendInfoToSlack("Syncing %s (%s) to LBRY! (iteration %d/%d - total processed channels: %d)", sync.LbryChannelName, sync.YoutubeChannelID, i+1, len(syncs), syncCount+1)
|
2018-07-17 18:54:22 +02:00
|
|
|
err := sync.FullCycle()
|
|
|
|
if err != nil {
|
|
|
|
fatalErrors := []string{
|
|
|
|
"default_wallet already exists",
|
|
|
|
"WALLET HAS NOT BEEN MOVED TO THE WALLET BACKUP DIR",
|
|
|
|
"NotEnoughFunds",
|
|
|
|
"no space left on device",
|
2018-08-21 19:17:52 +02:00
|
|
|
"failure uploading wallet",
|
2018-07-17 18:54:22 +02:00
|
|
|
}
|
2018-07-24 02:01:35 +02:00
|
|
|
if util.SubstringInSlice(err.Error(), fatalErrors) {
|
2018-07-17 18:54:22 +02:00
|
|
|
return errors.Prefix("@Nikooo777 this requires manual intervention! Exiting...", err)
|
|
|
|
}
|
2018-08-21 19:17:52 +02:00
|
|
|
shouldNotCount = strings.Contains(err.Error(), "this youtube channel is being managed by another server")
|
2018-08-22 16:56:02 +02:00
|
|
|
if !shouldNotCount {
|
|
|
|
SendInfoToSlack("A non fatal error was reported by the sync process. %s\nContinuing...", err.Error())
|
|
|
|
}
|
2018-07-17 18:54:22 +02:00
|
|
|
}
|
2018-08-22 16:56:02 +02:00
|
|
|
SendInfoToSlack("Syncing %s (%s) reached an end. (iteration %d/%d - total processed channels: %d)", sync.LbryChannelName, sync.YoutubeChannelID, i+1, len(syncs), syncCount+1)
|
2018-08-21 19:17:52 +02:00
|
|
|
if !shouldNotCount {
|
|
|
|
syncCount++
|
|
|
|
}
|
2018-09-26 06:08:18 +02:00
|
|
|
if sync.IsInterrupted() || (s.limit != 0 && syncCount >= s.limit) {
|
2018-07-17 18:54:22 +02:00
|
|
|
shouldInterruptLoop = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-09-26 06:08:18 +02:00
|
|
|
if shouldInterruptLoop || s.singleRun {
|
2018-07-17 18:54:22 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-17 16:05:54 +02:00
|
|
|
func (s *SyncManager) checkUsedSpace() error {
|
2018-09-26 06:08:18 +02:00
|
|
|
usedPctile, err := GetUsedSpace(s.blobsDir)
|
2018-07-17 18:54:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-26 06:08:18 +02:00
|
|
|
if usedPctile >= 0.90 && !s.skipSpaceCheck {
|
2018-07-17 18:54:22 +02:00
|
|
|
return errors.Err(fmt.Sprintf("more than 90%% of the space has been used. use --skip-space-check to ignore. Used: %.1f%%", usedPctile*100))
|
|
|
|
}
|
2018-08-21 20:07:30 +02:00
|
|
|
log.Infof("disk usage: %.1f%%", usedPctile*100)
|
2018-07-17 18:54:22 +02:00
|
|
|
return nil
|
|
|
|
}
|
2018-07-17 20:58:47 +02:00
|
|
|
|
|
|
|
// GetUsedSpace returns a value between 0 and 1, with 0 being completely empty and 1 being full, for the disk that holds the provided path
|
|
|
|
func GetUsedSpace(path string) (float32, error) {
|
|
|
|
var stat syscall.Statfs_t
|
|
|
|
err := syscall.Statfs(path, &stat)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
// Available blocks * size per block = available space in bytes
|
|
|
|
all := stat.Blocks * uint64(stat.Bsize)
|
|
|
|
free := stat.Bfree * uint64(stat.Bsize)
|
|
|
|
used := all - free
|
|
|
|
|
|
|
|
return float32(used) / float32(all), nil
|
|
|
|
}
|