Skip to content
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

Add a SyncJobStatus result callback amid Sync retries #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -131,6 +136,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 @@ -151,6 +158,36 @@ abstract class FhirSyncWorker(appContext: Context, workerParams: WorkerParameter
}
}

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

try {
exceptions.forEach { resourceSyncException ->
if (resourceSyncException.exception is HttpException) {
val operationOutcome =
jsonParser.parseResource(
IOUtils.toString(
resourceSyncException.exception.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}",
)
}
} else {
Timber.e(resourceSyncException.exception)
}
}
} catch (e: Exception) {
Timber.e(e)
exceptions?.forEach { Timber.e(it.exception.message) }
}
}

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