@@ -101,21 +101,58 @@ class RealPirOptOut @Inject constructor(
101
101
private val dispatcherProvider : DispatcherProvider ,
102
102
callbacks : PluginPoint <PirCallbacks >,
103
103
) : PirOptOut, PirJob(callbacks) {
104
- private var profileQuery: ProfileQuery = ProfileQuery (
105
- firstName = " William" ,
106
- lastName = " Smith" ,
107
- city = " Chicago" ,
108
- state = " IL" ,
109
- addresses = listOf (
110
- Address (
111
- city = " Chicago" ,
112
- state = " IL" ,
104
+ private var profileQueries: List <ProfileQuery > = listOf (
105
+ ProfileQuery (
106
+ id = - 1 ,
107
+ firstName = " William" ,
108
+ lastName = " Smith" ,
109
+ city = " Chicago" ,
110
+ state = " IL" ,
111
+ addresses = listOf (
112
+ Address (
113
+ city = " Chicago" ,
114
+ state = " IL" ,
115
+ ),
116
+ ),
117
+ birthYear = 1993 ,
118
+ fullName = " William Smith" ,
119
+ age = 32 ,
120
+ deprecated = false ,
121
+ ),
122
+ ProfileQuery (
123
+ id = - 2 ,
124
+ firstName = " Jane" ,
125
+ lastName = " Doe" ,
126
+ city = " New York" ,
127
+ state = " NY" ,
128
+ addresses = listOf (
129
+ Address (
130
+ city = " New York" ,
131
+ state = " NY" ,
132
+ ),
133
+ ),
134
+ birthYear = 1990 ,
135
+ fullName = " Jane Doe" ,
136
+ age = 35 ,
137
+ deprecated = false ,
138
+ ),
139
+ ProfileQuery (
140
+ id = - 3 ,
141
+ firstName = " Alicia" ,
142
+ lastName = " West" ,
143
+ city = " Los Angeles" ,
144
+ state = " CA" ,
145
+ addresses = listOf (
146
+ Address (
147
+ city = " Los Angeles" ,
148
+ state = " CA" ,
149
+ ),
113
150
),
151
+ birthYear = 1985 ,
152
+ fullName = " Alicia West" ,
153
+ age = 40 ,
154
+ deprecated = false ,
114
155
),
115
- birthYear = 1993 ,
116
- fullName = " William Smith" ,
117
- age = 32 ,
118
- deprecated = false ,
119
156
)
120
157
121
158
private val runners: MutableList <PirActionsRunner > = mutableListOf ()
@@ -131,9 +168,9 @@ class RealPirOptOut @Inject constructor(
131
168
cleanRunners()
132
169
runners.clear()
133
170
}
134
- obtainProfile ()
171
+ obtainProfiles ()
135
172
136
- logcat { " PIR-OPT-OUT: Running opt-out on profile : $profileQuery on ${Thread .currentThread().name} " }
173
+ logcat { " PIR-OPT-OUT: Running debug opt-out for $brokers on profiles : $profileQueries on ${Thread .currentThread().name} " }
137
174
138
175
runners.add(
139
176
pirActionsRunnerFactory.create(
@@ -143,21 +180,28 @@ class RealPirOptOut @Inject constructor(
143
180
),
144
181
)
145
182
146
- // Start each runner on a subset of the broker steps
183
+ // Load opt-out steps jsons for each broker
184
+ val brokerOptOutStepsJsons = brokers.mapNotNull { broker ->
185
+ repository.getBrokerOptOutSteps(broker)?.let { broker to it }
186
+ }
147
187
148
- brokers.mapNotNull { broker ->
149
- repository.getBrokerOptOutSteps(broker)?.run {
150
- brokerStepsParser.parseStep(broker, this )
151
- }
152
- }.filter {
153
- it.isNotEmpty()
188
+ // Map broker steps with their associated profile queries
189
+ val allSteps = profileQueries.map { profileQuery ->
190
+ brokerOptOutStepsJsons.map { (broker, stepsJson) ->
191
+ brokerStepsParser.parseStep(broker, stepsJson, profileQuery.id)
192
+ }.flatten().map { step -> profileQuery to step }
154
193
}.flatten()
155
- .also { list ->
156
- runners[0 ].startOn(webView, profileQuery, list)
157
- runners[0 ].stop()
158
- }
159
194
160
- logcat { " PIR-OPT-OUT: Optout completed for all runners" }
195
+ // Execute each steps sequentially on the single runner
196
+ allSteps.forEach { (profileQuery, step) ->
197
+ logcat { " PIR-OPT-OUT: Start thread=${Thread .currentThread().name} , profile=$profileQuery and step=$step " }
198
+ runners[0 ].startOn(webView, profileQuery, listOf (step))
199
+ runners[0 ].stop()
200
+ logcat { " PIR-OPT-OUT: Finish thread=${Thread .currentThread().name} , profile=$profileQuery and step=$step " }
201
+ }
202
+
203
+ logcat { " PIR-OPT-OUT: Opt-out completed for all runners and profiles" }
204
+
161
205
emitCompletedPixel()
162
206
onJobCompleted()
163
207
return @withContext Result .success(Unit )
@@ -173,25 +217,28 @@ class RealPirOptOut @Inject constructor(
173
217
cleanRunners()
174
218
runners.clear()
175
219
}
176
- obtainProfile ()
220
+ obtainProfiles ()
177
221
178
- logcat { " PIR-OPT-OUT: Running opt-out on profile : $profileQuery on ${Thread .currentThread().name} " }
222
+ logcat { " PIR-OPT-OUT: Running opt-out on profiles : $profileQueries on ${Thread .currentThread().name} " }
179
223
180
224
val script = pirCssScriptLoader.getScript()
181
225
182
- val brokerSteps = brokers.mapNotNull { broker ->
183
- repository.getBrokerOptOutSteps(broker)?.run {
184
- brokerStepsParser.parseStep(broker, this )
185
- }
186
- }.filter {
187
- it.isNotEmpty()
226
+ // Load opt-out steps jsons for each broker
227
+ val brokerOptOutStepsJsons = brokers.mapNotNull { broker ->
228
+ repository.getBrokerOptOutSteps(broker)?.let { broker to it }
229
+ }
230
+
231
+ // Map broker steps with their associated profile queries
232
+ val allSteps = profileQueries.map { profileQuery ->
233
+ brokerOptOutStepsJsons.map { (broker, stepsJson) ->
234
+ brokerStepsParser.parseStep(broker, stepsJson, profileQuery.id)
235
+ }.flatten().map { step -> profileQuery to step }
188
236
}.flatten()
189
237
190
- maxWebViewCount = if (brokerSteps.size <= MAX_DETACHED_WEBVIEW_COUNT ) {
191
- brokerSteps.size
192
- } else {
193
- MAX_DETACHED_WEBVIEW_COUNT
194
- }
238
+ maxWebViewCount = minOf(allSteps.size, MAX_DETACHED_WEBVIEW_COUNT )
239
+
240
+ // Assign steps to runners based on the maximum number of WebViews we can use
241
+ val stepsPerRunner = allSteps.splitIntoParts(maxWebViewCount)
195
242
196
243
logcat { " PIR-OPT-OUT: Attempting to create $maxWebViewCount parallel runners on ${Thread .currentThread().name} " }
197
244
@@ -208,45 +255,49 @@ class RealPirOptOut @Inject constructor(
208
255
createCount++
209
256
}
210
257
211
- // Start each runner on a subset of the broker steps
212
- brokerSteps.splitIntoParts(maxWebViewCount)
213
- .mapIndexed { index, part ->
214
- async {
215
- runners[index].start(profileQuery, part)
258
+ // Execute the steps on all runners in parallel
259
+ stepsPerRunner.mapIndexed { index, partSteps ->
260
+ async {
261
+ partSteps.map { (profileQuery, step) ->
262
+ logcat { " PIR-OPT-OUT: Start opt-out on runner=$index , profile=$profileQuery and step=$step " }
263
+ runners[index].start(profileQuery, listOf (step))
216
264
runners[index].stop()
265
+ logcat { " PIR-OPT-OUT: Finish opt-out on runner=$index , profile=$profileQuery and step=$step " }
217
266
}
218
- }.awaitAll()
267
+ }
268
+ }.awaitAll()
219
269
220
- logcat { " PIR-OPT-OUT: Optout completed for all runners" }
270
+ logcat { " PIR-OPT-OUT: Opt-out completed for all runners and profiles " }
221
271
emitCompletedPixel()
222
272
onJobCompleted()
223
273
return @withContext Result .success(Unit )
224
274
}
225
275
226
- private suspend fun obtainProfile () {
227
- repository.getUserProfiles().also {
228
- if (it.isNotEmpty()) {
229
- // Temporarily taking the first profile only for the PoC. In the reality, more than 1 should be allowed.
230
- val storedProfile = it[0 ]
231
- profileQuery = ProfileQuery (
232
- firstName = storedProfile.userName.firstName,
233
- lastName = storedProfile.userName.lastName,
234
- city = storedProfile.addresses.city,
235
- state = storedProfile.addresses.state,
236
- addresses = listOf (
237
- Address (
238
- city = storedProfile.addresses.city,
239
- state = storedProfile.addresses.state,
276
+ private suspend fun obtainProfiles () {
277
+ repository.getUserProfiles().also { profiles ->
278
+ if (profiles.isNotEmpty()) {
279
+ profileQueries = profiles.map { storedProfile ->
280
+ ProfileQuery (
281
+ id = storedProfile.id,
282
+ firstName = storedProfile.userName.firstName,
283
+ lastName = storedProfile.userName.lastName,
284
+ city = storedProfile.addresses.city,
285
+ state = storedProfile.addresses.state,
286
+ addresses = listOf (
287
+ Address (
288
+ city = storedProfile.addresses.city,
289
+ state = storedProfile.addresses.state,
290
+ ),
240
291
),
241
- ) ,
242
- birthYear = storedProfile.birthYear,
243
- fullName = storedProfile.userName.middleName?. run {
244
- " ${storedProfile.userName.firstName} $this ${storedProfile.userName.lastName} "
245
- }
246
- ? : " ${storedProfile.userName.firstName} ${ storedProfile.userName.lastName} " ,
247
- age = LocalDate .now().year - storedProfile.birthYear ,
248
- deprecated = false ,
249
- )
292
+ birthYear = storedProfile.birthYear ,
293
+ fullName = storedProfile.userName.middleName?. run {
294
+ " ${ storedProfile.userName.firstName} $this ${storedProfile.userName.lastName} "
295
+ }
296
+ ? : " ${storedProfile.userName.firstName} ${storedProfile.userName.lastName} " ,
297
+ age = LocalDate .now().year - storedProfile.birthYear ,
298
+ deprecated = false ,
299
+ )
300
+ }
250
301
}
251
302
}
252
303
}
0 commit comments