Skip to content

Commit

Permalink
Add a SyncJobStatus result callback amid Sync retries
Browse files Browse the repository at this point in the history
  • Loading branch information
ndegwamartin committed Aug 23, 2023
1 parent e3a524c commit a92e93c
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ abstract class FhirSyncWorker(appContext: Context, workerParams: WorkerParameter
)
.apply { subscribe(flow) }
.synchronize()

if (result is SyncJobStatus.Failed) onFailedSyncJobResult(result)

val output = buildWorkData(result)

// await/join is needed to collect states completely
Expand Down Expand Up @@ -118,6 +121,32 @@ abstract class FhirSyncWorker(appContext: Context, workerParams: WorkerParameter
}
}

open fun onFailedSyncJobResult(failedSyncJobStatus: SyncJobStatus.Failed) {
try {
CoroutineScope(Dispatchers.IO).launch {
val jsonParser = FhirContext.forR4().newJsonParser()

(result).exceptions.filterIsInstance<HttpException>().forEach { resourceSyncHTTPException ->
val operationOutcome =
jsonParser.parseResource(
IOUtils.toString(
resourceSyncHTTPException.response()?.errorBody()?.byteStream(),
StandardCharsets.UTF_8
)
) as OperationOutcome

operationOutcome.issue.forEach { operationOutcome ->
Timber.e(
"SERVER ${operationOutcome.severity} - HTTP ${resourceSyncHTTPException.code()} | Code - ${operationOutcome.code} | Diagnostics - ${operationOutcome.diagnostics}"
)
}
}
}
} catch (e: Exception) {
Timber.e(e)
}
}

private fun buildWorkData(state: SyncJobStatus): Data {
return workDataOf(
// send serialized state and type so that consumer can convert it back
Expand Down

0 comments on commit a92e93c

Please sign in to comment.