Skip to content

Commit 2c04789

Browse files
committed
fixup: postCreateDelay now separate from the retry delay logic
1 parent 040249f commit 2c04789

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/operations/impl/OperationRepo.kt

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,9 @@ internal class OperationRepo(
304304
}
305305
}
306306

307-
// set post create delay if the execution resulted in ID translations to stall processing
308-
val postCreateDelay = response.idTranslations?.let { _configModelStore.model.opRepoPostCreateDelay } ?: 0
309-
delayBeforeNextExecution(highestRetries, response.retryAfterSeconds, postCreateDelay)
307+
// wait for retry and post create waiters to start next operation
308+
delayBeforeNextExecution(highestRetries, response.retryAfterSeconds)
309+
delayForPostCreate(response.idTranslations != null)
310310
} catch (e: Throwable) {
311311
Logging.log(LogLevel.ERROR, "Error attempting to execute operation: $ops", e)
312312

@@ -317,39 +317,43 @@ internal class OperationRepo(
317317
}
318318

319319
/**
320-
* Wait which ever is longer, post create delay, retryAfterSeconds returned by the server,
320+
* Wait which ever is longer, retryAfterSeconds returned by the server,
321321
* or based on the retry count.
322-
*
323-
* postCreateDelay: Stall processing the queue so the backend's DB has to time reflect the
324-
* change before we do any other operations to it.
325-
*
326-
* NOTE: Future: We could run this logic in a
327-
* coroutineScope.launch() block so other operations not
328-
* effecting this these id's can still be done in parallel,
329-
* however other parts of the system don't currently account
330-
* for this so this is not safe to do.
331322
*/
332323
suspend fun delayBeforeNextExecution(
333324
retries: Int,
334325
retryAfterSeconds: Int?,
335-
postCreateDelay: Long,
336326
) {
337327
Logging.debug("retryAfterSeconds: $retryAfterSeconds")
338328
val retryAfterSecondsNonNull = retryAfterSeconds?.toLong() ?: 0L
339329
val delayForOnRetries = retries * _configModelStore.model.opRepoDefaultFailRetryBackoff
340-
val delayFor = max(delayForOnRetries, max(retryAfterSecondsNonNull * 1_000, postCreateDelay))
330+
val delayFor = max(delayForOnRetries, retryAfterSecondsNonNull * 1_000)
341331
if (delayFor < 1) return
342-
// do not log error if there is an expected delay for post create
343-
if (delayFor == postCreateDelay) {
344-
Logging.debug("Operations being delay for $delayFor ms due to PostCreateDelay")
345-
} else {
346-
Logging.error("Operations being delay for: $delayFor ms")
347-
}
332+
Logging.error("Operations being delay for: $delayFor ms")
348333
withTimeoutOrNull(delayFor) {
349334
retryWaiter.waitForWake()
350335
}
351336
}
352337

338+
/**
339+
* Stall processing the queue so the backend's DB has to time
340+
* reflect the change before we do any other operations to it.
341+
* NOTE: Future: We could run this logic in a
342+
* coroutineScope.launch() block so other operations not
343+
* effecting this these id's can still be done in parallel,
344+
* however other parts of the system don't currently account
345+
* for this so this is not safe to do.
346+
*/
347+
suspend fun delayForPostCreate(hasIDTranslation: Boolean) {
348+
val postCreateDelay: Long = _configModelStore.model.opRepoPostCreateDelay
349+
if (!hasIDTranslation || postCreateDelay == 0L)
350+
return
351+
delay(postCreateDelay)
352+
synchronized(queue) {
353+
if (queue.isNotEmpty()) waiter.wake(LoopWaiterMessage(false, postCreateDelay))
354+
}
355+
}
356+
353357
internal fun getNextOps(bucketFilter: Int): List<OperationQueueItem>? {
354358
return synchronized(queue) {
355359
val startingOp =

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/core/internal/operations/OperationRepoTests.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class OperationRepoTests : FunSpec({
215215
// Given
216216
val mocks = Mocks()
217217
val opRepo = mocks.operationRepo
218-
coEvery { opRepo.delayBeforeNextExecution(any(), any(), any()) } just runs
218+
coEvery { opRepo.delayBeforeNextExecution(any(), any()) } just runs
219219
coEvery {
220220
mocks.executor.execute(any())
221221
} returns ExecutionResponse(ExecutionResult.FAIL_RETRY) andThen ExecutionResponse(ExecutionResult.SUCCESS)
@@ -239,7 +239,7 @@ class OperationRepoTests : FunSpec({
239239
it[0] shouldBe operation
240240
},
241241
)
242-
opRepo.delayBeforeNextExecution(1, null, 0)
242+
opRepo.delayBeforeNextExecution(1, null)
243243
mocks.executor.execute(
244244
withArg {
245245
it.count() shouldBe 1
@@ -671,7 +671,7 @@ class OperationRepoTests : FunSpec({
671671
// ensure the order: IDs are translated, operation removed from the store, then delay for postCreateDelay
672672
operation.translateIds(idTranslation)
673673
mocks.operationModelStore.remove(opId)
674-
mocks.operationRepo.delayBeforeNextExecution(any(), any(), 100)
674+
mocks.operationRepo.delayBeforeNextExecution(any(), any())
675675
}
676676
}
677677

0 commit comments

Comments
 (0)