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 Sep 24, 2024
1 parent 8301d62 commit 4b41745
Showing 1 changed file with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.work.CoroutineWorker
import androidx.work.Data
import androidx.work.WorkerParameters
import androidx.work.workDataOf
import ca.uhn.fhir.context.FhirContext
import com.google.android.fhir.FhirEngine
import com.google.android.fhir.FhirEngineProvider
import com.google.android.fhir.OffsetDateTimeTypeAdapter
Expand All @@ -32,11 +33,15 @@ import com.google.android.fhir.sync.upload.request.UploadRequestGeneratorFactory
import com.google.gson.ExclusionStrategy
import com.google.gson.FieldAttributes
import com.google.gson.GsonBuilder
import java.nio.charset.StandardCharsets
import java.time.OffsetDateTime
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import org.apache.commons.io.IOUtils
import org.hl7.fhir.r4.model.OperationOutcome
import retrofit2.HttpException
import timber.log.Timber

/**
Expand Down Expand Up @@ -135,6 +140,8 @@ abstract class FhirSyncWorker(appContext: Context, workerParams: WorkerParameter
}

val result = synchronizer.synchronize()
if (result is SyncJobStatus.Failed) onFailedSyncJobResult(result)

val output = buildWorkData(result)

// await/join is needed to collect states completely
Expand All @@ -155,6 +162,34 @@ abstract class FhirSyncWorker(appContext: Context, workerParams: WorkerParameter
}
}

open fun onFailedSyncJobResult(failedSyncJobStatus: SyncJobStatus.Failed) {
try {
val jsonParser = FhirContext.forR4().newJsonParser()
val exceptions = (failedSyncJobStatus).exceptions

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

operationOutcome.issue.forEach { operationOutcomeIssueComponent ->
Timber.e(
"SERVER ${operationOutcomeIssueComponent.severity} - HTTP ${resourceSyncException.exception.code()} | Code - ${operationOutcomeIssueComponent.code} | Diagnostics - ${operationOutcomeIssueComponent.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 4b41745

Please sign in to comment.