move channel status call to proper region

fix shadowed errors
refactor code to be more readable and consistent
get video transfer state
This commit is contained in:
Niko Storni 2019-08-21 19:40:35 +02:00
parent eb2f6273e4
commit 1b9ed266e0
6 changed files with 40 additions and 28 deletions

2
go.mod
View file

@ -16,7 +16,7 @@ require (
github.com/hashicorp/serf v0.8.2 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/lbryio/errors.go v0.0.0-20180223142025-ad03d3cc6a5c
github.com/lbryio/lbry.go v1.1.2
github.com/lbryio/lbry.go v1.1.1-0.20190820035946-9ac18d083579
github.com/lbryio/reflector.go v1.0.6-0.20190806185326-2e4f235489f4
github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect

2
go.sum
View file

@ -177,6 +177,8 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lbryio/errors.go v0.0.0-20180223142025-ad03d3cc6a5c h1:BhdcWGsuKif/XoSZnqVGNqJ1iEmH0czWR5upj+AuR8M=
github.com/lbryio/errors.go v0.0.0-20180223142025-ad03d3cc6a5c/go.mod h1:muH7wpUqE8hRA3OrYYosw9+Sl681BF9cwcjzE+OCNK8=
github.com/lbryio/lbry.go v0.0.0-20190109223729-30c312501602/go.mod h1:YEuFJD/oHNra6BFy+NfuvS84Wg6RMWJFGtiCCCc6MmQ=
github.com/lbryio/lbry.go v1.1.1-0.20190820035946-9ac18d083579 h1:3FKVv1m/J8mVQJTwIcF5BFv2E87y+nYqBqofJs6F720=
github.com/lbryio/lbry.go v1.1.1-0.20190820035946-9ac18d083579/go.mod h1:JtyI30bU51rm0LZ/po3mQuzf++14OWb6kR/6mMRAmKU=
github.com/lbryio/lbry.go v1.1.2 h1:Dyxc+glT/rVWJwHfIf7vjlPYYbjzrQz5ARmJd5Hp69c=
github.com/lbryio/lbry.go v1.1.2/go.mod h1:JtyI30bU51rm0LZ/po3mQuzf++14OWb6kR/6mMRAmKU=
github.com/lbryio/lbryschema.go v0.0.0-20190428231007-c54836bca002 h1:urfYK5ElpUrAv90auPLldoVC60LwiGAcY0OE6HJB9KI=

View file

@ -89,6 +89,7 @@ const (
StatusFinalized = "finalized" // no more changes allowed
StatusAbandoned = "abandoned" // deleted on youtube or banned
)
const LatestMetadataVersion = 2
var SyncStatuses = []string{StatusPending, StatusPendingEmail, StatusPendingUpgrade, StatusQueued, StatusSyncing, StatusSynced, StatusFailed, StatusFinalized, StatusAbandoned}

View file

@ -7,18 +7,22 @@ import (
"github.com/lbryio/ytsync/sdk"
)
func TransferChannel(channel *Sync) error {
transferStatus := TransferStateComplete
func TransferChannelAndVideos(channel *Sync) error {
cleanTransfer := true
for _, video := range channel.syncedVideos {
if !channel.syncedVideos[video.ClaimID].Published || channel.syncedVideos[video.ClaimID].Transferred || channel.syncedVideos[video.ClaimID].MetadataVersion != LatestMetadataVersion {
continue
}
//Todo - Wait for prior sync to see that the publish is confirmed in lbrycrd?
c, err := channel.daemon.ClaimSearch(nil, &video.ClaimID, nil, nil)
if err != nil {
errors.Err(err)
return errors.Err(err)
}
if len(c.Claims) == 0 {
errors.Err("cannot transfer: no claim found for this video")
return errors.Err("cannot transfer: no claim found for this video")
} else if len(c.Claims) > 1 {
errors.Err("cannot transfer: too many claims. claimID: %s", video.ClaimID)
return errors.Err("cannot transfer: too many claims. claimID: %s", video.ClaimID)
}
streamUpdateOptions := jsonrpc.StreamUpdateOptions{
@ -39,7 +43,7 @@ func TransferChannel(channel *Sync) error {
_, err = channel.daemon.StreamUpdate(video.ClaimID, streamUpdateOptions)
if err != nil {
transferStatus = TransferStateFailed
cleanTransfer = false
videoStatus.FailureReason = err.Error()
videoStatus.Status = VideoStatusTranferFailed
videoStatus.IsTransferred = util.PtrToBool(false)
@ -54,10 +58,9 @@ func TransferChannel(channel *Sync) error {
}
// Todo - Transfer Channel as last step and post back to remote db that channel is transferred.
//Transfer channel
if transferStatus < 0 {
if !cleanTransfer {
return errors.Err("A video has failed to transfer for the channel...skipping channel transfer")
}
failureReason := ""
channelClaim, err := channel.daemon.ClaimSearch(nil, &channel.lbryChannelID, nil, nil)
if err != nil {
return errors.Err(err)
@ -73,14 +76,5 @@ func TransferChannel(channel *Sync) error {
},
}
_, err = channel.daemon.ChannelUpdate(channel.lbryChannelID, updateOptions)
if err != nil {
transferStatus = TransferStateFailed
failureReason = err.Error()
}
_, _, channelStatusErr := channel.APIConfig.SetChannelStatus(channel.lbryChannelID, VideoStatusPublished, failureReason, transferStatus)
if channelStatusErr != nil {
return errors.Err(err)
}
return errors.Err(err)
}

View file

@ -227,7 +227,7 @@ func (s *Sync) uploadWallet() error {
}
func (s *Sync) setStatusSyncing() error {
syncedVideos, claimNames, err := s.Manager.apiConfig.SetChannelStatus(s.YoutubeChannelID, StatusSyncing, "")
syncedVideos, claimNames, err := s.Manager.apiConfig.SetChannelStatus(s.YoutubeChannelID, StatusSyncing, "", nil)
if err != nil {
return err
}
@ -315,8 +315,8 @@ func (s *Sync) FullCycle() (e error) {
return err
}
if s.transferState == 1 && s.publishAddress != "" { // Channel needs transfer
return TransferChannel(s)
if s.shouldTransfer() {
return TransferChannelAndVideos(s)
}
return nil
@ -331,8 +331,19 @@ func deleteSyncFolder(videoDirectory string) {
_ = util.SendToSlack(err.Error())
}
}
func (s *Sync) shouldTransfer() bool {
return s.transferState == 1 && s.publishAddress != ""
}
func (s *Sync) setChannelTerminationStatus(e *error) {
var transferState *int
if s.shouldTransfer() {
if *e != nil {
transferState = util.PtrToInt(TransferStateComplete)
} else {
transferState = util.PtrToInt(TransferStateFailed)
}
}
if *e != nil {
//conditions for which a channel shouldn't be marked as failed
noFailConditions := []string{
@ -343,13 +354,13 @@ func (s *Sync) setChannelTerminationStatus(e *error) {
return
}
failureReason := (*e).Error()
_, _, err := s.Manager.apiConfig.SetChannelStatus(s.YoutubeChannelID, StatusFailed, failureReason)
_, _, err := s.Manager.apiConfig.SetChannelStatus(s.YoutubeChannelID, StatusFailed, failureReason, transferState)
if err != nil {
msg := fmt.Sprintf("Failed setting failed state for channel %s", s.LbryChannelName)
*e = errors.Prefix(msg+err.Error(), *e)
}
} else if !s.IsInterrupted() {
_, _, err := s.Manager.apiConfig.SetChannelStatus(s.YoutubeChannelID, StatusSynced, "")
_, _, err := s.Manager.apiConfig.SetChannelStatus(s.YoutubeChannelID, StatusSynced, "", transferState)
if err != nil {
*e = err
}

View file

@ -90,6 +90,7 @@ type SyncedVideo struct {
ClaimID string `json:"claim_id"`
Size int64 `json:"size"`
MetadataVersion int8 `json:"metadata_version"`
Transferred bool `json:"transferred"`
}
func sanitizeFailureReason(s *string) {
@ -133,7 +134,7 @@ func (a *APIConfig) SetChannelCert(certHex string, channelID string) error {
}
func (a *APIConfig) SetChannelStatus(channelID string, status string, failureReason string, transferState int) (map[string]SyncedVideo, map[string]bool, error) {
func (a *APIConfig) SetChannelStatus(channelID string, status string, failureReason string, transferState *int) (map[string]SyncedVideo, map[string]bool, error) {
type apiChannelStatusResponse struct {
Success bool `json:"success"`
Error null.String `json:"error"`
@ -142,14 +143,17 @@ func (a *APIConfig) SetChannelStatus(channelID string, status string, failureRea
endpoint := a.ApiURL + "/yt/channel_status"
sanitizeFailureReason(&failureReason)
res, _ := http.PostForm(endpoint, url.Values{
params := url.Values{
"channel_id": {channelID},
"sync_server": {a.HostName},
"auth_token": {a.ApiToken},
"sync_status": {status},
"failure_reason": {failureReason},
"transferred": {strconv.Itoa(transferState)},
})
}
if transferState != nil {
params.Add("transferred", strconv.Itoa(*transferState))
}
res, _ := http.PostForm(endpoint, params)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
var response apiChannelStatusResponse