Skip to content

Commit

Permalink
Fixed resource usage during sync. It might be slower now though.
Browse files Browse the repository at this point in the history
Removed some unnecessary work in sync
  • Loading branch information
spacecowboy committed Apr 7, 2024
1 parent 3d9d027 commit 475ec21
Showing 1 changed file with 36 additions and 49 deletions.
85 changes: 36 additions & 49 deletions app/src/main/java/com/nononsenseapps/feeder/model/RssLocalSync.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ import com.nononsenseapps.feeder.util.flatMap
import com.nononsenseapps.feeder.util.left
import com.nononsenseapps.feeder.util.logDebug
import com.nononsenseapps.feeder.util.sloppyLinkToStrictURLNoThrows
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.supervisorScope
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
Expand All @@ -38,6 +34,9 @@ import kotlin.system.measureTimeMillis
val singleThreadedSync = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
val syncMutex = Mutex()

/**
* WARNING. DO NOT CHANGE THE DISPATCHER WITHIN THE LOGIC!
*/
class RssLocalSync(override val di: DI) : DIAware {
private val repository: Repository by instance()
private val syncClient: SyncRestClient by instance()
Expand Down Expand Up @@ -103,47 +102,35 @@ class RssLocalSync(override val di: DI) : DIAware {

logDebug(LOG_TAG, "Syncing ${feedsToFetch.size} feeds")

val coroutineContext =
this.coroutineContext +
CoroutineExceptionHandler { _, throwable ->
Log.e(LOG_TAG, "Error during sync", throwable)
}

val jobs =
feedsToFetch.map {
needFullTextSync = needFullTextSync || it.fullTextByDefault
launch(coroutineContext) {
try {
// Want unique sync times so UI gets updated state
repository.setCurrentlySyncingOn(
feedId = it.id,
syncing = true,
lastSync = Instant.now(),
)
syncFeed(
feedSql = it,
maxFeedItemCount = maxFeedItemCount,
forceNetwork = forceNetwork,
downloadTime = downloadTime,
).onLeft { feedParserError ->
Log.e(
LOG_TAG,
"Failed to sync ${it.displayTitle}: ${it.url} because:\n${feedParserError.description}",
)
}
} catch (e: Throwable) {
Log.e(
LOG_TAG,
"Failed to sync ${it.displayTitle}: ${it.url}",
e,
)
} finally {
repository.setCurrentlySyncingOn(feedId = it.id, syncing = false)
}
for (feed in feedsToFetch) {
needFullTextSync = needFullTextSync || feed.fullTextByDefault
try {
// Want unique sync times. UI doesn't use syncing flag anymore
repository.setCurrentlySyncingOn(
feedId = feed.id,
syncing = false,
lastSync = Instant.now(),
)
syncFeed(
feedSql = feed,
maxFeedItemCount = maxFeedItemCount,
forceNetwork = forceNetwork,
downloadTime = downloadTime,
).onLeft { feedParserError ->
Log.e(
LOG_TAG,
"Failed to sync ${feed.displayTitle}: ${feed.url} because:\n${feedParserError.description}",
)
}
} catch (e: Throwable) {
Log.e(
LOG_TAG,
"Failed to sync ${feed.displayTitle}: ${feed.url}",
e,
)
}
}

jobs.joinAll()
try {
repository.applyRemoteReadMarks()
} catch (e: Exception) {
Expand Down Expand Up @@ -265,13 +252,11 @@ class RssLocalSync(override val di: DI) : DIAware {
} ?: emptyList()

repository.upsertFeedItems(feedItemSqls) { feedItem, text ->
withContext(Dispatchers.IO) {
filePathProvider.articleDir.mkdirs()
blobOutputStream(feedItem.id, filePathProvider.articleDir).bufferedWriter()
.use {
it.write(text)
}
}
filePathProvider.articleDir.mkdirs()
blobOutputStream(feedItem.id, filePathProvider.articleDir).bufferedWriter()
.use {
it.write(text)
}
}
// Try to look for image if not done before
if (feedSql.imageUrl == null && feedSql.siteFetched == Instant.EPOCH) {
Expand Down Expand Up @@ -346,6 +331,8 @@ class RssLocalSync(override val di: DI) : DIAware {

repository.deleteFeedItems(ids)
repository.deleteStaleRemoteReadMarks()

logDebug(LOG_TAG, "Fetched ${feedSql.displayTitle}")
}
}
}
Expand Down

0 comments on commit 475ec21

Please sign in to comment.