-
Notifications
You must be signed in to change notification settings - Fork 303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix sync failure state exception handling #1656
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,9 @@ import com.google.android.fhir.sync.Resolved | |
import com.google.android.fhir.toTimeZoneString | ||
import java.time.OffsetDateTime | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.flow.onEach | ||
import org.hl7.fhir.r4.model.Bundle | ||
import org.hl7.fhir.r4.model.Resource | ||
import org.hl7.fhir.r4.model.ResourceType | ||
|
@@ -87,12 +89,13 @@ internal class FhirEngineImpl(private val database: Database, private val contex | |
conflictResolver: ConflictResolver, | ||
download: suspend (SyncDownloadContext) -> Flow<List<Resource>> | ||
) { | ||
|
||
download( | ||
object : SyncDownloadContext { | ||
override suspend fun getLatestTimestampFor(type: ResourceType) = database.lastUpdate(type) | ||
} | ||
) | ||
.collect { resources -> | ||
object : SyncDownloadContext { | ||
override suspend fun getLatestTimestampFor(type: ResourceType) = database.lastUpdate(type) | ||
} | ||
) | ||
.onEach { resources -> | ||
database.withTransaction { | ||
val resolved = | ||
resolveConflictingResources( | ||
|
@@ -104,6 +107,10 @@ internal class FhirEngineImpl(private val database: Database, private val contex | |
saveResolvedResourcesToDatabase(resolved) | ||
} | ||
} | ||
.catch { throwable: Throwable -> | ||
Timber.e(throwable, "Error saving remote resource to database") | ||
} | ||
.collect() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we are using on each and doing the processing above. do we need collect now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes |
||
} | ||
|
||
private suspend fun saveResolvedResourcesToDatabase(resolved: List<Resource>?) { | ||
|
@@ -115,9 +122,11 @@ internal class FhirEngineImpl(private val database: Database, private val contex | |
|
||
private suspend fun saveRemoteResourcesToDatabase(resources: List<Resource>) { | ||
val timeStamps = | ||
resources.groupBy { it.resourceType }.entries.map { | ||
SyncedResourceEntity(it.key, it.value.maxOf { it.meta.lastUpdated }.toTimeZoneString()) | ||
} | ||
resources | ||
.groupBy { it.resourceType } | ||
.entries.map { | ||
SyncedResourceEntity(it.key, it.value.maxOf { it.meta.lastUpdated }.toTimeZoneString()) | ||
} | ||
database.insertSyncedResources(timeStamps, resources) | ||
} | ||
|
||
|
@@ -207,7 +216,8 @@ internal class FhirEngineImpl(private val database: Database, private val contex | |
*/ | ||
private val Bundle.BundleEntryResponseComponent.resourceIdAndType: Pair<String, ResourceType>? | ||
get() = | ||
location?.split("/")?.takeIf { it.size > 3 }?.let { | ||
it[it.size - 3] to ResourceType.fromCode(it[it.size - 4]) | ||
} | ||
location | ||
?.split("/") | ||
?.takeIf { it.size > 3 } | ||
?.let { it[it.size - 3] to ResourceType.fromCode(it[it.size - 4]) } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we make it private. ideally this should not be modifiable outside this class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. This was exposed to be used in
com.google.android.fhir.sync.download.DownloaderImpl#download
to get the resource type that is currently being synced. Thecatch
operator can only access theflow
instance and not the internal states within theflow { }
closure.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need the resource type in
ResourceSyncException
data class? Most instance creation of that class haveResourceType.Bundle
provided as the resource type.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it does emit the failed resource type. the test DownloaderImplTest.
downloader with patient and observations should continue to download observations if patient download fail
has an assertion for this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Investigating the failing tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figured the cause of the filing tests. So apparently the catch operator completes the flow thus the remaining states are not emitted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test was verifying whether emissions still happens even when there is a failure. Which I think should be the correct behavior. I will revert and remove the emission in the catch section of the try-catch block.