Skip to content

Commit

Permalink
chore:update call to fetch descriptor to handle errors better
Browse files Browse the repository at this point in the history
  • Loading branch information
aanorbel committed May 14, 2024
1 parent fdcbe62 commit d112c90
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,37 +49,47 @@ class TestDescriptorManager @Inject constructor(
* @param runId the run id of the descriptor to fetch
* @param context the context to use for the request
*/
fun fetchDescriptorFromRunId(runId: Long, context: Context): TestDescriptor {
fun fetchDescriptorFromRunId(runId: Long, context: Context): TestDescriptor? {
val session = EngineProvider.get().newSession(
EngineProvider.get().getDefaultSessionConfig(
context,
BuildConfig.SOFTWARE_NAME,
BuildConfig.VERSION_NAME,
LoggerArray(),
(context.applicationContext as Application).preferenceManager.proxyURL
)
EngineProvider.get().getDefaultSessionConfig(
context,
BuildConfig.SOFTWARE_NAME,
BuildConfig.VERSION_NAME,
LoggerArray(),
(context.applicationContext as Application).preferenceManager.proxyURL
)
)
val ooniContext = session.newContextWithTimeout(300)

val response: OONIRunDescriptor =
runCatching {
// Fetch the descriptor from the ooni server
session.getLatestOONIRunLink(ooniContext, BuildConfig.OONI_API_BASE_URL, runId)

var revisions: OONIRunRevisions? = null

try {
if (Integer.parseInt(response.revision) > 1) {
revisions = session.getOONIRunLinkRevisions(
ooniContext,
BuildConfig.OONI_API_BASE_URL,
runId
)
}.fold(
onSuccess = { ooniRunDescriptor ->

var revisions: OONIRunRevisions? = null

try {
if (ooniRunDescriptor.revision.toInt() > 1) {
revisions = session.getOONIRunLinkRevisions(
ooniContext,
BuildConfig.OONI_API_BASE_URL,
runId
)
}
} catch (e: Exception) {
ThirdPartyServices.logException(e)
}
return ooniRunDescriptor.toTestDescriptor().apply {
previousRevision = Gson().toJson(revisions)
}
},
onFailure = { throwable ->
// Log the error or handle it appropriately
ThirdPartyServices.logException(Exception(throwable))
return null
}
} catch (e: Exception) {
ThirdPartyServices.logException(e)
}
return response.toTestDescriptor().apply {
previousRevision = Gson().toJson(revisions)
}
)
}

fun addDescriptor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ var d: UpdateDescriptorsWorkerDependencies = UpdateDescriptorsWorkerDependencies
const val PROGRESS = "PROGRESS"

class AutoUpdateDescriptorsWorker(
context: Context,
workerParams: WorkerParameters
context: Context,
workerParams: WorkerParameters
) : Worker(context, workerParams) {

override fun doWork(): Result {
Expand All @@ -30,22 +30,22 @@ class AutoUpdateDescriptorsWorker(
val updatedDescriptors: ArrayList<TestDescriptor> = ArrayList()

for (descriptor in d.testDescriptorManager.getDescriptorWithAutoUpdateEnabled()) {

Log.d(TAG, "Fetching updates for ${descriptor.runId}")

val updatedDescriptor: TestDescriptor =
d.testDescriptorManager.fetchDescriptorFromRunId(
d.testDescriptorManager.fetchDescriptorFromRunId(
descriptor.runId,
applicationContext
)

if (descriptor.shouldUpdate(updatedDescriptor)) {
updatedDescriptor.isAutoUpdate = descriptor.isAutoUpdate
updatedDescriptor.isAutoRun = descriptor.isAutoRun
)?.let { updatedDescriptor ->
if (descriptor.shouldUpdate(updatedDescriptor)) {
updatedDescriptor.isAutoUpdate = descriptor.isAutoUpdate
updatedDescriptor.isAutoRun = descriptor.isAutoRun

Log.d(TAG, "Saving updates for ${descriptor.runId}")
Log.d(TAG, "Saving updates for ${descriptor.runId}")

updatedDescriptor.save()
updatedDescriptors.add(updatedDescriptor)
updatedDescriptor.save()
updatedDescriptors.add(updatedDescriptor)
}
}
}

Expand All @@ -70,21 +70,21 @@ class AutoUpdateDescriptorsWorker(
companion object {
@JvmField
var UPDATED_DESCRIPTORS_WORK_NAME =
"${AutoUpdateDescriptorsWorker::class.java.name}.UPDATED_DESCRIPTORS_WORK_NAME"
"${AutoUpdateDescriptorsWorker::class.java.name}.UPDATED_DESCRIPTORS_WORK_NAME"

private val TAG = AutoUpdateDescriptorsWorker::class.java.simpleName

private val UPDATE_DESCRIPTOR_CHANNEL: String =
AutoUpdateDescriptorsWorker::class.java.simpleName
AutoUpdateDescriptorsWorker::class.java.simpleName

private val KEY_UPDATED_DESCRIPTORS =
"${AutoUpdateDescriptorsWorker::class.java.name}.KEY_UPDATED_DESCRIPTORS"
"${AutoUpdateDescriptorsWorker::class.java.name}.KEY_UPDATED_DESCRIPTORS"
}
}

class ManualUpdateDescriptorsWorker(
context: Context,
workerParams: WorkerParameters
context: Context,
workerParams: WorkerParameters
) : Worker(context, workerParams) {

override fun doWork(): Result {
Expand Down Expand Up @@ -113,17 +113,18 @@ class ManualUpdateDescriptorsWorker(
for (descriptor in descriptors) {
Log.d(TAG, "Fetching updates for ${descriptor.runId}")

val updatedDescriptor: TestDescriptor =
d.testDescriptorManager.fetchDescriptorFromRunId(
d.testDescriptorManager.fetchDescriptorFromRunId(
descriptor.runId,
applicationContext
)
/**
* NOTE(aanorbel): Refine this logic to only update if the descriptor has changed.
* Consider explicit version compare.
*/
if (descriptor.shouldUpdate(updatedDescriptor)) {
updatedDescriptors.add(updatedDescriptor)
)?.let { updatedDescriptor ->

/**
* NOTE(aanorbel): Refine this logic to only update if the descriptor has changed.
* Consider explicit version compare.
*/
if (descriptor.shouldUpdate(updatedDescriptor)) {
updatedDescriptors.add(updatedDescriptor)
}
}
}
val outputData = Data.Builder()
Expand Down Expand Up @@ -153,19 +154,20 @@ class ManualUpdateDescriptorsWorker(
companion object {
@JvmField
var UPDATED_DESCRIPTORS_WORK_NAME =
"${AutoUpdateDescriptorsWorker::class.java.name}.UPDATED_DESCRIPTORS_WORK_NAME"
"${AutoUpdateDescriptorsWorker::class.java.name}.UPDATED_DESCRIPTORS_WORK_NAME"

private val TAG = AutoUpdateDescriptorsWorker::class.java.simpleName

private val UPDATE_DESCRIPTOR_CHANNEL: String =
AutoUpdateDescriptorsWorker::class.java.simpleName
AutoUpdateDescriptorsWorker::class.java.simpleName

@JvmField
var KEY_UPDATED_DESCRIPTORS =
"${AutoUpdateDescriptorsWorker::class.java.name}.KEY_UPDATED_DESCRIPTORS"
"${AutoUpdateDescriptorsWorker::class.java.name}.KEY_UPDATED_DESCRIPTORS"

@JvmField
var KEY_DESCRIPTOR_IDS =
"${AutoUpdateDescriptorsWorker::class.java.name}.KEY_DESCRIPTOR_IDS"
"${AutoUpdateDescriptorsWorker::class.java.name}.KEY_DESCRIPTOR_IDS"
}
}

Expand Down

0 comments on commit d112c90

Please sign in to comment.