Skip to content

Commit

Permalink
Merge pull request #1900 from OneSignal/v5_retry_failed_create_user
Browse files Browse the repository at this point in the history
[v5] Pause operation repo and retry failed user create
  • Loading branch information
nan-li committed Dec 1, 2023
2 parents fef3aad + 0cf1eb9 commit 6334a24
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,11 @@ enum class ExecutionResult {
* The operation failed due to a conflict and can be handled.
*/
FAIL_CONFLICT,

/**
* Used in special create user case.
* The operation failed due to a non-retryable error. Pause the operation repo
* and retry on a new session, giving the SDK a chance to recover from the failed user create.
*/
FAIL_PAUSE_OPREPO,
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ internal class OperationRepo(
private val executorsMap: Map<String, IOperationExecutor>
private val queue = mutableListOf<OperationQueueItem>()
private val waiter = WaiterWithValue<Boolean>()
private var paused = false

init {
val executorsMap: MutableMap<String, IOperationExecutor> = mutableMapOf()
Expand All @@ -47,6 +48,7 @@ internal class OperationRepo(
}

override fun start() {
paused = false
suspendifyOnThread(name = "OpRepo") {
processQueueForever()
}
Expand Down Expand Up @@ -99,6 +101,10 @@ internal class OperationRepo(

// This runs forever, until the application is destroyed.
while (true) {
if (paused) {
Logging.debug("OperationRepo is paused")
return
}
try {
var ops: List<OperationQueueItem>? = null

Expand Down Expand Up @@ -199,6 +205,15 @@ internal class OperationRepo(
ops.reversed().forEach { queue.add(0, it) }
}
}
ExecutionResult.FAIL_PAUSE_OPREPO -> {
Logging.error("Operation execution failed with eventual retry, pausing the operation repo: $operations")
// keep the failed operation and pause the operation repo from executing
paused = true
// add back all operations to the front of the queue to be re-executed.
synchronized(queue) {
ops.reversed().forEach { queue.add(0, it) }
}
}
}

// if there are operations provided on the result, we need to enqueue them at the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ internal class LoginUserOperationExecutor(
NetworkUtils.ResponseStatusType.UNAUTHORIZED ->
ExecutionResponse(ExecutionResult.FAIL_UNAUTHORIZED)
else ->
ExecutionResponse(ExecutionResult.FAIL_NORETRY)
ExecutionResponse(ExecutionResult.FAIL_PAUSE_OPREPO)
}
}
}
Expand Down

0 comments on commit 6334a24

Please sign in to comment.