Skip to content

Commit a508940

Browse files
committed
Use attemptCount for tries
1 parent d449999 commit a508940

File tree

3 files changed

+30
-34
lines changed

3 files changed

+30
-34
lines changed

pir/pir-impl/src/main/java/com/duckduckgo/pir/impl/common/PirRunStateHandler.kt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import com.duckduckgo.pir.impl.common.PirRunStateHandler.PirRunState.BrokerSched
3636
import com.duckduckgo.pir.impl.common.PirRunStateHandler.PirRunState.BrokerScheduledScanStarted
3737
import com.duckduckgo.pir.impl.models.AddressCityState
3838
import com.duckduckgo.pir.impl.models.ExtractedProfile
39+
import com.duckduckgo.pir.impl.models.scheduling.JobRecord.OptOutJobRecord
3940
import com.duckduckgo.pir.impl.pixels.PirPixelSender
4041
import com.duckduckgo.pir.impl.pixels.PirStage
4142
import com.duckduckgo.pir.impl.scheduling.JobRecordUpdater
@@ -137,7 +138,6 @@ interface PirRunStateHandler {
137138
val attemptId: String,
138139
val startTimeInMillis: Long,
139140
val endTimeInMillis: Long,
140-
val tries: Int,
141141
val emailPattern: String?,
142142
) : PirRunState(brokerName)
143143

@@ -148,7 +148,6 @@ interface PirRunStateHandler {
148148
val startTimeInMillis: Long,
149149
val endTimeInMillis: Long,
150150
val failedAction: BrokerAction,
151-
val tries: Int,
152151
val stage: PirStage,
153152
val emailPattern: String?,
154153
) : PirRunState(brokerName)
@@ -427,16 +426,16 @@ class RealPirRunStateHandler @Inject constructor(
427426

428427
private suspend fun handleBrokerRecordOptOutSubmitted(state: BrokerRecordOptOutSubmitted) {
429428
val broker = repository.getBrokerForName(state.brokerName)
430-
updateOptOutRecord(true, state.extractedProfile.dbId)
429+
val optOutJobRecord = updateOptOutRecord(true, state.extractedProfile.dbId)
431430

432-
if (broker == null) return
431+
if (broker == null || optOutJobRecord == null) return
433432

434433
pixelSender.reportOptOutSubmitted(
435434
brokerUrl = broker.url,
436435
parent = broker.parent ?: "",
437436
attemptId = state.attemptId,
438437
durationMs = state.endTimeInMillis - state.startTimeInMillis,
439-
tries = state.tries,
438+
tries = optOutJobRecord.attemptCount,
440439
emailPattern = state.emailPattern,
441440
)
442441

@@ -451,17 +450,17 @@ class RealPirRunStateHandler @Inject constructor(
451450

452451
private suspend fun handleBrokerRecordOptOutFailed(state: BrokerRecordOptOutFailed) {
453452
val broker = repository.getBrokerForName(state.brokerName)
454-
updateOptOutRecord(false, state.extractedProfile.dbId)
453+
val optOutJobRecord = updateOptOutRecord(false, state.extractedProfile.dbId)
455454

456-
if (broker == null) return
455+
if (broker == null || optOutJobRecord == null) return
457456

458457
pixelSender.reportOptOutFailed(
459458
brokerUrl = broker.url,
460459
parent = broker.parent ?: "",
461460
brokerJsonVersion = broker.version,
462461
attemptId = state.attemptId,
463462
durationMs = state.endTimeInMillis - state.startTimeInMillis,
464-
tries = state.tries,
463+
tries = optOutJobRecord.attemptCount,
465464
emailPattern = state.emailPattern,
466465
stage = state.stage,
467466
actionId = state.failedAction.id,
@@ -512,8 +511,8 @@ class RealPirRunStateHandler @Inject constructor(
512511
private suspend fun updateOptOutRecord(
513512
isSubmitted: Boolean,
514513
extractedProfileId: Long,
515-
) {
516-
if (isSubmitted) {
514+
): OptOutJobRecord? {
515+
return if (isSubmitted) {
517516
jobRecordUpdater.updateOptOutRequested(extractedProfileId)
518517
} else {
519518
jobRecordUpdater.updateOptOutError(extractedProfileId)

pir/pir-impl/src/main/java/com/duckduckgo/pir/impl/common/actions/BrokerStepCompletedEventHandler.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ class BrokerStepCompletedEventHandler @Inject constructor(
124124
attemptId = state.attemptId ?: "no-attempt-id",
125125
startTimeInMillis = state.brokerStepStartTime,
126126
endTimeInMillis = currentTimeProvider.currentTimeMillis(),
127-
tries = 0,
128127
emailPattern = "",
129128
)
130129
} else {
@@ -138,8 +137,7 @@ class BrokerStepCompletedEventHandler @Inject constructor(
138137
endTimeInMillis = currentTimeProvider.currentTimeMillis(),
139138
attemptId = state.attemptId ?: "no-attempt-id",
140139
failedAction = lastAction,
141-
tries = 1,
142-
stage = PirStage.OTHER,
140+
stage = PirStage.OTHER, // TODO: Integrate stages properly later on
143141
emailPattern = "",
144142
)
145143
}.also {

pir/pir-impl/src/main/java/com/duckduckgo/pir/impl/scheduling/JobRecordUpdater.kt

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ interface JobRecordUpdater {
129129
*
130130
* @param extractedProfileId The id stored in our database for the [ExtractedProfile]
131131
*/
132-
suspend fun updateOptOutRequested(extractedProfileId: Long)
132+
suspend fun updateOptOutRequested(extractedProfileId: Long): OptOutJobRecord?
133133

134134
/**
135135
* Updates the [OptOutJobRecord] associated with a given [extractedProfileId].
@@ -140,7 +140,7 @@ interface JobRecordUpdater {
140140
*
141141
* @param extractedProfileId The id stored in our database for the [ExtractedProfile]
142142
*/
143-
suspend fun updateOptOutError(extractedProfileId: Long)
143+
suspend fun updateOptOutError(extractedProfileId: Long): OptOutJobRecord?
144144

145145
/**
146146
* Updates the [OptOutJobRecord] associated with the given [extractedProfileId].
@@ -403,33 +403,32 @@ class RealJobRecordUpdater @Inject constructor(
403403
}
404404
}
405405

406-
override suspend fun updateOptOutRequested(extractedProfileId: Long) {
407-
withContext(dispatcherProvider.io()) {
406+
override suspend fun updateOptOutRequested(extractedProfileId: Long): OptOutJobRecord? {
407+
return withContext(dispatcherProvider.io()) {
408408
schedulingRepository.getValidOptOutJobRecord(extractedProfileId)?.also {
409-
schedulingRepository.saveOptOutJobRecord(
410-
it
411-
.copy(
412-
status = OptOutJobStatus.REQUESTED,
413-
optOutRequestedDateInMillis = currentTimeProvider.currentTimeMillis(),
414-
).also {
415-
logcat { "PIR-JOB-RECORD: Updating OptOutRecord for $extractedProfileId to $it" }
416-
},
409+
val updatedRecord = it.copy(
410+
status = OptOutJobStatus.REQUESTED,
411+
optOutRequestedDateInMillis = currentTimeProvider.currentTimeMillis(),
417412
)
413+
schedulingRepository.saveOptOutJobRecord(updatedRecord)
414+
415+
logcat { "PIR-JOB-RECORD: Updating OptOutRecord for $extractedProfileId to $it" }
416+
417+
return@withContext updatedRecord
418418
}
419419
}
420420
}
421421

422-
override suspend fun updateOptOutError(extractedProfileId: Long) {
423-
withContext(dispatcherProvider.io()) {
422+
override suspend fun updateOptOutError(extractedProfileId: Long): OptOutJobRecord? {
423+
return withContext(dispatcherProvider.io()) {
424424
schedulingRepository.getValidOptOutJobRecord(extractedProfileId)?.also {
425-
schedulingRepository.saveOptOutJobRecord(
426-
it
427-
.copy(
428-
status = OptOutJobStatus.ERROR,
429-
).also {
430-
logcat { "PIR-JOB-RECORD: Updating OptOutRecord for $extractedProfileId to $it" }
431-
},
425+
val updatedRecord = it.copy(
426+
status = OptOutJobStatus.ERROR,
432427
)
428+
schedulingRepository.saveOptOutJobRecord(updatedRecord)
429+
logcat { "PIR-JOB-RECORD: Updating OptOutRecord for $extractedProfileId to $it" }
430+
431+
return@withContext updatedRecord
433432
}
434433
}
435434
}

0 commit comments

Comments
 (0)