From 440bb134a4a234f2bc15354dc0ad6ca90a1b5da8 Mon Sep 17 00:00:00 2001 From: Muhammad Rehan Date: Mon, 1 Aug 2022 17:56:05 +0500 Subject: [PATCH] fix: parsing exception for expired tasks --- .../io/customer/sdk/queue/QueueStorage.kt | 33 ++++++++++++------- .../java/io/customer/sdk/util/JsonAdapter.kt | 9 +++++ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/sdk/src/main/java/io/customer/sdk/queue/QueueStorage.kt b/sdk/src/main/java/io/customer/sdk/queue/QueueStorage.kt index 13d295d73..3d7452b3e 100644 --- a/sdk/src/main/java/io/customer/sdk/queue/QueueStorage.kt +++ b/sdk/src/main/java/io/customer/sdk/queue/QueueStorage.kt @@ -5,13 +5,7 @@ import io.customer.base.extenstions.subtract import io.customer.sdk.CustomerIOConfig import io.customer.sdk.data.store.FileStorage import io.customer.sdk.data.store.FileType -import io.customer.sdk.queue.type.QueueInventory -import io.customer.sdk.queue.type.QueueModifyResult -import io.customer.sdk.queue.type.QueueStatus -import io.customer.sdk.queue.type.QueueTask -import io.customer.sdk.queue.type.QueueTaskGroup -import io.customer.sdk.queue.type.QueueTaskMetadata -import io.customer.sdk.queue.type.QueueTaskRunResults +import io.customer.sdk.queue.type.* import io.customer.sdk.util.DateUtil import io.customer.sdk.util.JsonAdapter import io.customer.sdk.util.Logger @@ -22,7 +16,13 @@ import java.util.concurrent.TimeUnit interface QueueStorage { fun getInventory(): QueueInventory fun saveInventory(inventory: QueueInventory): Boolean - fun create(type: String, data: String, groupStart: QueueTaskGroup?, blockingGroups: List?): QueueModifyResult + fun create( + type: String, + data: String, + groupStart: QueueTaskGroup?, + blockingGroups: List? + ): QueueModifyResult + fun update(taskStorageId: String, runResults: QueueTaskRunResults): Boolean fun get(taskStorageId: String): QueueTask? fun delete(taskStorageId: String): QueueModifyResult @@ -40,7 +40,7 @@ class QueueStorageImpl internal constructor( @Synchronized override fun getInventory(): QueueInventory { val dataFromFile = fileStorage.get(FileType.QueueInventory()) ?: return emptyList() - return jsonAdapter.fromJsonList(dataFromFile) + return jsonAdapter.fromJsonListOrNull(dataFromFile) ?: emptyList() } @Synchronized @@ -108,11 +108,17 @@ class QueueStorageImpl internal constructor( override fun delete(taskStorageId: String): QueueModifyResult { // update inventory first so if any deletion operation is unsuccessful, at least the inventory will not contain the task so queue doesn't try running it. val existingInventory = getInventory().toMutableList() - val queueStatusBeforeModifyInventory = QueueStatus(sdkConfig.siteId, existingInventory.count()) + val queueStatusBeforeModifyInventory = + QueueStatus(sdkConfig.siteId, existingInventory.count()) existingInventory.removeAll { it.taskPersistedId == taskStorageId } - if (!saveInventory(existingInventory) || !fileStorage.delete(FileType.QueueTask(taskStorageId))) { + if (!saveInventory(existingInventory) || !fileStorage.delete( + FileType.QueueTask( + taskStorageId + ) + ) + ) { logger.error("error trying to delete task with storage id: $taskStorageId from queue") return QueueModifyResult(false, queueStatusBeforeModifyInventory) } @@ -125,7 +131,10 @@ class QueueStorageImpl internal constructor( logger.debug("deleting expired tasks from the queue") val tasksToDelete: MutableSet = mutableSetOf() - val queueTaskExpiredThreshold = Date().subtract(sdkConfig.backgroundQueueTaskExpiredSeconds.toSeconds().value, TimeUnit.SECONDS) + val queueTaskExpiredThreshold = Date().subtract( + sdkConfig.backgroundQueueTaskExpiredSeconds.toSeconds().value, + TimeUnit.SECONDS + ) logger.debug("deleting tasks older then $queueTaskExpiredThreshold, current time is: ${Date()}") getInventory().filter { diff --git a/sdk/src/main/java/io/customer/sdk/util/JsonAdapter.kt b/sdk/src/main/java/io/customer/sdk/util/JsonAdapter.kt index dd8ad41a0..6d7778826 100644 --- a/sdk/src/main/java/io/customer/sdk/util/JsonAdapter.kt +++ b/sdk/src/main/java/io/customer/sdk/util/JsonAdapter.kt @@ -70,6 +70,15 @@ class JsonAdapter internal constructor(val moshi: Moshi) { return adapter.fromJson(json) as List } + /** + * Use if you anticipate the json parsing to not work. You don't care about the exception. Else, use [fromJsonList]. + */ + inline fun fromJsonListOrNull(json: String): List? = try { + fromJsonList(json) + } catch (ex: Exception) { + null + } + fun toJson(data: T): String { val jsonAdapter = moshi.adapter(data::class.java)