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/hashicorp/serf v0.8.2 // indirect
github.com/kr/pretty v0.1.0 // indirect github.com/kr/pretty v0.1.0 // indirect
github.com/lbryio/errors.go v0.0.0-20180223142025-ad03d3cc6a5c 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/lbryio/reflector.go v1.0.6-0.20190806185326-2e4f235489f4
github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936 github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect 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 h1:BhdcWGsuKif/XoSZnqVGNqJ1iEmH0czWR5upj+AuR8M=
github.com/lbryio/errors.go v0.0.0-20180223142025-ad03d3cc6a5c/go.mod h1:muH7wpUqE8hRA3OrYYosw9+Sl681BF9cwcjzE+OCNK8= 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 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 h1:Dyxc+glT/rVWJwHfIf7vjlPYYbjzrQz5ARmJd5Hp69c=
github.com/lbryio/lbry.go v1.1.2/go.mod h1:JtyI30bU51rm0LZ/po3mQuzf++14OWb6kR/6mMRAmKU= 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= 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 StatusFinalized = "finalized" // no more changes allowed
StatusAbandoned = "abandoned" // deleted on youtube or banned StatusAbandoned = "abandoned" // deleted on youtube or banned
) )
const LatestMetadataVersion = 2
var SyncStatuses = []string{StatusPending, StatusPendingEmail, StatusPendingUpgrade, StatusQueued, StatusSyncing, StatusSynced, StatusFailed, StatusFinalized, StatusAbandoned} var SyncStatuses = []string{StatusPending, StatusPendingEmail, StatusPendingUpgrade, StatusQueued, StatusSyncing, StatusSynced, StatusFailed, StatusFinalized, StatusAbandoned}

View file

@ -7,18 +7,22 @@ import (
"github.com/lbryio/ytsync/sdk" "github.com/lbryio/ytsync/sdk"
) )
func TransferChannel(channel *Sync) error { func TransferChannelAndVideos(channel *Sync) error {
transferStatus := TransferStateComplete cleanTransfer := true
for _, video := range channel.syncedVideos { 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? //Todo - Wait for prior sync to see that the publish is confirmed in lbrycrd?
c, err := channel.daemon.ClaimSearch(nil, &video.ClaimID, nil, nil) c, err := channel.daemon.ClaimSearch(nil, &video.ClaimID, nil, nil)
if err != nil { if err != nil {
errors.Err(err) return errors.Err(err)
} }
if len(c.Claims) == 0 { 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 { } 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{ streamUpdateOptions := jsonrpc.StreamUpdateOptions{
@ -39,7 +43,7 @@ func TransferChannel(channel *Sync) error {
_, err = channel.daemon.StreamUpdate(video.ClaimID, streamUpdateOptions) _, err = channel.daemon.StreamUpdate(video.ClaimID, streamUpdateOptions)
if err != nil { if err != nil {
transferStatus = TransferStateFailed cleanTransfer = false
videoStatus.FailureReason = err.Error() videoStatus.FailureReason = err.Error()
videoStatus.Status = VideoStatusTranferFailed videoStatus.Status = VideoStatusTranferFailed
videoStatus.IsTransferred = util.PtrToBool(false) 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. // Todo - Transfer Channel as last step and post back to remote db that channel is transferred.
//Transfer channel //Transfer channel
if transferStatus < 0 { if !cleanTransfer {
return errors.Err("A video has failed to transfer for the channel...skipping channel transfer") 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) channelClaim, err := channel.daemon.ClaimSearch(nil, &channel.lbryChannelID, nil, nil)
if err != nil { if err != nil {
return errors.Err(err) return errors.Err(err)
@ -73,14 +76,5 @@ func TransferChannel(channel *Sync) error {
}, },
} }
_, err = channel.daemon.ChannelUpdate(channel.lbryChannelID, updateOptions) _, 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) return errors.Err(err)
} }

View file

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

View file

@ -90,6 +90,7 @@ type SyncedVideo struct {
ClaimID string `json:"claim_id"` ClaimID string `json:"claim_id"`
Size int64 `json:"size"` Size int64 `json:"size"`
MetadataVersion int8 `json:"metadata_version"` MetadataVersion int8 `json:"metadata_version"`
Transferred bool `json:"transferred"`
} }
func sanitizeFailureReason(s *string) { 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 { type apiChannelStatusResponse struct {
Success bool `json:"success"` Success bool `json:"success"`
Error null.String `json:"error"` Error null.String `json:"error"`
@ -142,14 +143,17 @@ func (a *APIConfig) SetChannelStatus(channelID string, status string, failureRea
endpoint := a.ApiURL + "/yt/channel_status" endpoint := a.ApiURL + "/yt/channel_status"
sanitizeFailureReason(&failureReason) sanitizeFailureReason(&failureReason)
res, _ := http.PostForm(endpoint, url.Values{ params := url.Values{
"channel_id": {channelID}, "channel_id": {channelID},
"sync_server": {a.HostName}, "sync_server": {a.HostName},
"auth_token": {a.ApiToken}, "auth_token": {a.ApiToken},
"sync_status": {status}, "sync_status": {status},
"failure_reason": {failureReason}, "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() defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body) body, _ := ioutil.ReadAll(res.Body)
var response apiChannelStatusResponse var response apiChannelStatusResponse