Skip to content

Commit

Permalink
HexagonShowSync: ensure uploading more than 500 shows works reliably.
Browse files Browse the repository at this point in the history
Add note about batch size.
  • Loading branch information
UweTrottmann committed Dec 7, 2023
1 parent 353452d commit 5ae7b44
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Version 71
----------
*in development*

* 🔨 Cloud: do not fail when uploading more than 500 new shows.

#### 71.0.0 🧪
*2023-11-17*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

public class HexagonEpisodeSync {

// See API documentation on list size limit.
public static final int MAX_BATCH_SIZE = 500;

private final Context context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import android.text.TextUtils
import androidx.preference.PreferenceManager
import com.battlelancer.seriesguide.backend.HexagonTools
import com.battlelancer.seriesguide.backend.settings.HexagonSettings
import com.battlelancer.seriesguide.movies.tools.MovieTools
import com.battlelancer.seriesguide.provider.SeriesGuideContract
import com.battlelancer.seriesguide.provider.SgRoomDatabase
import com.battlelancer.seriesguide.movies.tools.MovieTools
import com.battlelancer.seriesguide.util.DBUtils
import com.battlelancer.seriesguide.util.Errors
import com.google.api.client.util.DateTime
Expand All @@ -22,7 +22,6 @@ import com.uwetrottmann.seriesguide.backend.movies.model.Movie
import com.uwetrottmann.seriesguide.backend.movies.model.MovieList
import timber.log.Timber
import java.io.IOException
import java.util.ArrayList

internal class HexagonMovieSync(
private val context: Context,
Expand Down Expand Up @@ -222,14 +221,10 @@ internal class HexagonMovieSync(
return true
}

// Upload in small batches
val wrapper = MovieList()
while (movies.isNotEmpty()) {
wrapper.movies = ArrayList()
while (movies.isNotEmpty() && wrapper.movies.size < MAX_BATCH_SIZE) {
wrapper.movies.add(movies.removeFirst())
}

// Upload in batches
movies.chunked(MAX_BATCH_SIZE).forEach { chunk ->
val wrapper = MovieList()
wrapper.movies = chunk
try {
// get service each time to check if auth was removed
val moviesService = hexagonTools.moviesService ?: return false
Expand Down Expand Up @@ -265,6 +260,7 @@ internal class HexagonMovieSync(
}

companion object {
// See API documentation on list size limit.
private const val MAX_BATCH_SIZE = 500
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,19 +409,20 @@ class HexagonShowSync @Inject constructor(
// https://github.com/UweTrottmann/SeriesGuide/issues/781
Timber.i("upload: %d shows", shows.size)

// wrap into helper object
val showList = SgCloudShowList()
showList.shows = shows

// upload shows
try {
// get service each time to check if auth was removed
val showsService = hexagonTools.showsService ?: return false
showsService.saveSgShows(showList).execute()
} catch (e: IOException) {
logAndReportHexagon("save shows", e)
return false
// Upload in batches
shows.chunked(MAX_BATCH_SIZE).forEach { chunk ->
val wrapper = SgCloudShowList()
wrapper.shows = chunk
try {
// get service each time to check if auth was removed
val showsService = hexagonTools.showsService ?: return false
showsService.saveSgShows(wrapper).execute()
} catch (e: IOException) {
logAndReportHexagon("save shows", e)
return false
}
}

return true
}

Expand All @@ -431,4 +432,9 @@ class HexagonShowSync @Inject constructor(
}
}

companion object {
// See API documentation on list size limit.
const val MAX_BATCH_SIZE = 500
}

}

0 comments on commit 5ae7b44

Please sign in to comment.