2018-07-17 18:54:22 +02:00
|
|
|
package ytsync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
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/null"
|
|
|
|
"github.com/lbryio/lbry.go/util"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SyncManager struct {
|
|
|
|
StopOnError bool
|
|
|
|
MaxTries int
|
|
|
|
TakeOverExistingChannel bool
|
|
|
|
Refill int
|
|
|
|
Limit int
|
|
|
|
SkipSpaceCheck bool
|
|
|
|
SyncUpdate bool
|
|
|
|
SyncStatus string
|
|
|
|
SyncFrom int64
|
|
|
|
SyncUntil int64
|
|
|
|
ConcurrentJobs int
|
|
|
|
ConcurrentVideos int
|
|
|
|
HostName string
|
|
|
|
YoutubeChannelID string
|
2018-07-24 02:01:35 +02:00
|
|
|
YoutubeAPIKey string
|
|
|
|
ApiURL string
|
|
|
|
ApiToken string
|
|
|
|
BlobsDir string
|
2018-08-01 14:56:04 +02:00
|
|
|
VideosLimit int
|
|
|
|
MaxVideoSize int
|
2018-08-08 23:59:59 +02:00
|
|
|
LbrycrdString string
|
2018-08-09 22:54:23 +02:00
|
|
|
AwsS3ID string
|
|
|
|
AwsS3Secret string
|
|
|
|
AwsS3Region string
|
|
|
|
AwsS3Bucket string
|
2018-08-21 19:17:52 +02:00
|
|
|
SingleRun bool
|
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
|
|
|
|
|
|
|
type apiJobsResponse struct {
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Error null.String `json:"error"`
|
|
|
|
Data []apiYoutubeChannel `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type apiYoutubeChannel struct {
|
|
|
|
ChannelId string `json:"channel_id"`
|
|
|
|
TotalVideos uint `json:"total_videos"`
|
|
|
|
DesiredChannelName string `json:"desired_channel_name"`
|
|
|
|
SyncServer null.String `json:"sync_server"`
|
|
|
|
}
|
|
|
|
|
2018-08-17 16:05:54 +02:00
|
|
|
func (s *SyncManager) fetchChannels(status string) ([]apiYoutubeChannel, error) {
|
2018-07-24 02:01:35 +02:00
|
|
|
endpoint := s.ApiURL + "/yt/jobs"
|
2018-07-17 18:54:22 +02:00
|
|
|
res, _ := http.PostForm(endpoint, url.Values{
|
2018-07-24 02:01:35 +02:00
|
|
|
"auth_token": {s.ApiToken},
|
2018-07-17 18:54:22 +02:00
|
|
|
"sync_status": {status},
|
|
|
|
"min_videos": {strconv.Itoa(1)},
|
|
|
|
"after": {strconv.Itoa(int(s.SyncFrom))},
|
|
|
|
"before": {strconv.Itoa(int(s.SyncUntil))},
|
2018-08-24 17:57:45 +02:00
|
|
|
"sync_server": {s.HostName},
|
|
|
|
"channel_id": {s.YoutubeChannelID},
|
2018-07-17 18:54:22 +02:00
|
|
|
})
|
|
|
|
defer res.Body.Close()
|
|
|
|
body, _ := ioutil.ReadAll(res.Body)
|
|
|
|
var response apiJobsResponse
|
|
|
|
err := json.Unmarshal(body, &response)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if response.Data == nil {
|
|
|
|
return nil, errors.Err(response.Error)
|
|
|
|
}
|
|
|
|
log.Printf("Fetched channels: %d", len(response.Data))
|
|
|
|
return response.Data, nil
|
|
|
|
}
|
|
|
|
|
2018-07-31 19:42:20 +02:00
|
|
|
type apiChannelStatusResponse struct {
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Error null.String `json:"error"`
|
|
|
|
Data []syncedVideo `json:"data"`
|
2018-07-17 18:54:22 +02:00
|
|
|
}
|
|
|
|
|
2018-07-31 01:19:12 +02:00
|
|
|
type syncedVideo struct {
|
2018-07-31 19:42:20 +02:00
|
|
|
VideoID string `json:"video_id"`
|
|
|
|
Published bool `json:"published"`
|
|
|
|
FailureReason string `json:"failure_reason"`
|
2018-08-23 00:28:31 +02:00
|
|
|
ClaimName string `json:"claim_name"`
|
2018-07-31 01:19:12 +02:00
|
|
|
}
|
|
|
|
|
2018-08-23 00:28:31 +02:00
|
|
|
func (s *SyncManager) setChannelStatus(channelID string, status string, failureReason string) (map[string]syncedVideo, map[string]bool, error) {
|
2018-07-25 19:08:28 +02:00
|
|
|
endpoint := s.ApiURL + "/yt/channel_status"
|
2018-08-17 16:05:54 +02:00
|
|
|
if len(failureReason) > maxReasonLength {
|
|
|
|
failureReason = failureReason[:maxReasonLength]
|
|
|
|
}
|
2018-07-17 18:54:22 +02:00
|
|
|
res, _ := http.PostForm(endpoint, url.Values{
|
2018-08-17 16:05:54 +02:00
|
|
|
"channel_id": {channelID},
|
|
|
|
"sync_server": {s.HostName},
|
|
|
|
"auth_token": {s.ApiToken},
|
|
|
|
"sync_status": {status},
|
|
|
|
"failure_reason": {failureReason},
|
2018-07-17 18:54:22 +02:00
|
|
|
})
|
|
|
|
defer res.Body.Close()
|
|
|
|
body, _ := ioutil.ReadAll(res.Body)
|
2018-07-31 19:42:20 +02:00
|
|
|
var response apiChannelStatusResponse
|
2018-07-17 18:54:22 +02:00
|
|
|
err := json.Unmarshal(body, &response)
|
|
|
|
if err != nil {
|
2018-08-23 00:28:31 +02:00
|
|
|
return nil, nil, err
|
2018-07-17 18:54:22 +02:00
|
|
|
}
|
|
|
|
if !response.Error.IsNull() {
|
2018-08-23 00:28:31 +02:00
|
|
|
return nil, nil, errors.Err(response.Error.String)
|
2018-07-17 18:54:22 +02:00
|
|
|
}
|
2018-07-31 19:42:20 +02:00
|
|
|
if response.Data != nil {
|
2018-07-31 01:19:12 +02:00
|
|
|
svs := make(map[string]syncedVideo)
|
2018-08-23 00:28:31 +02:00
|
|
|
claimNames := make(map[string]bool)
|
2018-07-31 19:42:20 +02:00
|
|
|
for _, v := range response.Data {
|
2018-07-31 01:19:12 +02:00
|
|
|
svs[v.VideoID] = v
|
2018-08-23 00:28:31 +02:00
|
|
|
claimNames[v.ClaimName] = v.Published
|
2018-07-31 01:19:12 +02:00
|
|
|
}
|
2018-08-23 00:28:31 +02:00
|
|
|
return svs, claimNames, nil
|
2018-07-17 18:54:22 +02:00
|
|
|
}
|
2018-08-23 00:28:31 +02:00
|
|
|
return nil, nil, errors.Err("invalid API response. Status code: %d", res.StatusCode)
|
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) MarkVideoStatus(channelID string, videoID string, status string, claimID string, claimName string, failureReason string, size *int64) error {
|
2018-07-25 19:08:28 +02:00
|
|
|
endpoint := s.ApiURL + "/yt/video_status"
|
2018-08-17 16:05:54 +02:00
|
|
|
if len(failureReason) > maxReasonLength {
|
|
|
|
failureReason = failureReason[:maxReasonLength]
|
|
|
|
}
|
2018-07-21 01:56:36 +02:00
|
|
|
vals := url.Values{
|
|
|
|
"youtube_channel_id": {channelID},
|
2018-07-25 19:08:28 +02:00
|
|
|
"video_id": {videoID},
|
2018-07-21 01:56:36 +02:00
|
|
|
"status": {status},
|
2018-07-24 02:01:35 +02:00
|
|
|
"auth_token": {s.ApiToken},
|
2018-07-21 01:56:36 +02:00
|
|
|
}
|
|
|
|
if status == VideoStatusPublished {
|
|
|
|
if claimID == "" || claimName == "" {
|
|
|
|
return errors.Err("claimID or claimName missing")
|
|
|
|
}
|
|
|
|
vals.Add("published_at", strconv.FormatInt(time.Now().Unix(), 10))
|
|
|
|
vals.Add("claim_id", claimID)
|
|
|
|
vals.Add("claim_name", claimName)
|
2018-08-14 17:09:23 +02:00
|
|
|
if size != nil {
|
|
|
|
vals.Add("size", strconv.FormatInt(*size, 10))
|
|
|
|
}
|
2018-07-21 01:56:36 +02:00
|
|
|
}
|
2018-07-25 19:08:28 +02:00
|
|
|
if failureReason != "" {
|
|
|
|
vals.Add("failure_reason", failureReason)
|
2018-07-21 01:56:36 +02:00
|
|
|
}
|
|
|
|
res, _ := http.PostForm(endpoint, vals)
|
|
|
|
defer res.Body.Close()
|
|
|
|
body, _ := ioutil.ReadAll(res.Body)
|
2018-08-14 16:48:55 +02:00
|
|
|
var response struct {
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Error null.String `json:"error"`
|
|
|
|
Data null.String `json:"data"`
|
|
|
|
}
|
2018-07-21 01:56:36 +02:00
|
|
|
err := json.Unmarshal(body, &response)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !response.Error.IsNull() {
|
|
|
|
return errors.Err(response.Error.String)
|
|
|
|
}
|
|
|
|
if !response.Data.IsNull() && response.Data.String == "ok" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.Err("invalid API response. Status code: %d", res.StatusCode)
|
2018-07-17 18:54:22 +02:00
|
|
|
}
|
|
|
|
|
2018-08-17 16:05:54 +02:00
|
|
|
func (s *SyncManager) Start() error {
|
2018-07-17 18:54:22 +02:00
|
|
|
syncCount := 0
|
|
|
|
for {
|
|
|
|
err := s.checkUsedSpace()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var syncs []Sync
|
|
|
|
shouldInterruptLoop := false
|
|
|
|
|
|
|
|
isSingleChannelSync := s.YoutubeChannelID != ""
|
|
|
|
if isSingleChannelSync {
|
|
|
|
channels, err := s.fetchChannels("")
|
|
|
|
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-07-24 02:01:35 +02:00
|
|
|
YoutubeAPIKey: s.YoutubeAPIKey,
|
2018-07-17 18:54:22 +02:00
|
|
|
YoutubeChannelID: s.YoutubeChannelID,
|
|
|
|
LbryChannelName: lbryChannelName,
|
|
|
|
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-08-08 23:59:59 +02:00
|
|
|
LbrycrdString: s.LbrycrdString,
|
2018-08-09 22:54:23 +02:00
|
|
|
AwsS3ID: s.AwsS3ID,
|
|
|
|
AwsS3Secret: s.AwsS3Secret,
|
|
|
|
AwsS3Region: s.AwsS3Region,
|
|
|
|
AwsS3Bucket: s.AwsS3Bucket,
|
2018-07-17 18:54:22 +02:00
|
|
|
}
|
|
|
|
shouldInterruptLoop = true
|
|
|
|
} else {
|
|
|
|
var queuesToSync []string
|
|
|
|
if s.SyncStatus != "" {
|
|
|
|
queuesToSync = append(queuesToSync, s.SyncStatus)
|
|
|
|
} else if s.SyncUpdate {
|
|
|
|
queuesToSync = append(queuesToSync, StatusSyncing, StatusSynced)
|
|
|
|
} else {
|
|
|
|
queuesToSync = append(queuesToSync, StatusSyncing, StatusQueued)
|
|
|
|
}
|
|
|
|
for _, q := range queuesToSync {
|
|
|
|
channels, err := s.fetchChannels(q)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, c := range channels {
|
|
|
|
syncs = append(syncs, Sync{
|
2018-07-24 02:01:35 +02:00
|
|
|
YoutubeAPIKey: s.YoutubeAPIKey,
|
2018-07-17 18:54:22 +02:00
|
|
|
YoutubeChannelID: c.ChannelId,
|
|
|
|
LbryChannelName: c.DesiredChannelName,
|
|
|
|
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-08-08 23:59:59 +02:00
|
|
|
LbrycrdString: s.LbrycrdString,
|
2018-08-09 22:54:23 +02:00
|
|
|
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-07-17 18:54:22 +02:00
|
|
|
if sync.IsInterrupted() || (s.Limit != 0 && syncCount >= s.Limit) {
|
|
|
|
shouldInterruptLoop = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-08-21 19:17:52 +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-07-24 02:01:35 +02:00
|
|
|
usedPctile, err := GetUsedSpace(s.BlobsDir)
|
2018-07-17 18:54:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if usedPctile >= 0.90 && !s.SkipSpaceCheck {
|
|
|
|
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
|
|
|
|
}
|