refactor selfsync process

This commit is contained in:
Niko Storni 2018-05-25 13:57:03 -04:00
parent 5a1ba06f4c
commit 0df643a550
No known key found for this signature in database
GPG key ID: F37FE63398800368

View file

@ -149,77 +149,48 @@ func selfSync(cmd *cobra.Command, args []string) {
} }
syncCount := 0 syncCount := 0
if syncStatus == StatusQueued { if syncStatus == StatusQueued {
mainLoop:
for { for {
//before processing the queued ones first clear the pending ones (if any) //before processing the queued ones first clear the pending ones (if any)
//TODO: extract method //TODO: extract method
syncingChannels, err := fetchChannels(authToken, StatusSyncing) queuesToSync := []string{
if err != nil { StatusSyncing,
util.SendToSlackError("failed to fetch channels: %v", err) StatusQueued,
break
} }
interruptedByUser, err := syncChannels(syncingChannels, authToken, ytAPIKey, &syncCount)
if err != nil {
util.SendToSlackError("%v", err)
break
}
if interruptedByUser {
break
}
util.SendToSlackInfo("Finished syncing pending channels")
//process queued channels
queuedChannels, err := fetchChannels(authToken, StatusQueued)
if err != nil {
util.SendToSlackError("failed to fetch channels: %v", err)
break
}
interruptedByUser, err = syncChannels(queuedChannels, authToken, ytAPIKey, &syncCount)
if err != nil {
util.SendToSlackError("%v", err)
break
}
if interruptedByUser {
break
}
util.SendToSlackInfo("Finished syncing queued channels")
if syncUpdate { if syncUpdate {
//update synced channels queuesToSync = append(queuesToSync, StatusSynced)
syncedChannels, err := fetchChannels(authToken, StatusSynced)
if err != nil {
util.SendToSlackError("failed to fetch channels: %v", err)
break
} }
interruptedByUser, err = syncChannels(syncedChannels, authToken, ytAPIKey, &syncCount) for _, v := range queuesToSync {
interruptedByUser, err := processQueue(v, authToken, ytAPIKey, &syncCount)
if err != nil { if err != nil {
util.SendToSlackError("%v", err) util.SendToSlackError(err.Error())
break break mainLoop
} }
if interruptedByUser { if interruptedByUser {
break break mainLoop
} }
util.SendToSlackInfo("Finished updating channels")
} }
} }
} else { } else {
// sync whatever was specified // sync whatever was specified
channelsToSync, err := fetchChannels(authToken, syncStatus) _, err := processQueue(syncStatus, authToken, ytAPIKey, &syncCount)
if err != nil { if err != nil {
util.SendToSlackError("failed to fetch channels: %v", err) util.SendToSlackError(err.Error())
return
}
interruptedByUser, err := syncChannels(channelsToSync, authToken, ytAPIKey, &syncCount)
if err != nil {
util.SendToSlackError("%v", err)
return
}
if interruptedByUser {
return return
} }
} }
util.SendToSlackInfo("Syncing process terminated!") util.SendToSlackInfo("Syncing process terminated!")
} }
func processQueue(queueStatus string, authToken string, ytAPIKey string, syncCount *int) (bool, error) {
syncingChannels, err := fetchChannels(authToken, queueStatus)
if err != nil {
return false, errors.Prefix("failed to fetch channels", err)
}
util.SendToSlackInfo("Finished syncing %s channels", queueStatus)
return syncChannels(syncingChannels, authToken, ytAPIKey, syncCount)
}
// syncChannels processes a slice of youtube channels (channelsToSync) and returns a bool that indicates whether // syncChannels processes a slice of youtube channels (channelsToSync) and returns a bool that indicates whether
// the execution finished by itself or was interrupted by the user and an error if anything happened // the execution finished by itself or was interrupted by the user and an error if anything happened
func syncChannels(channelsToSync []APIYoutubeChannel, authToken string, ytAPIKey string, syncCount *int) (bool, error) { func syncChannels(channelsToSync []APIYoutubeChannel, authToken string, ytAPIKey string, syncCount *int) (bool, error) {