Move `RemoveExpiredLinkedDevicesCommand` error handling for more accurate metrics

This commit is contained in:
Chris Eager 2023-12-21 13:39:12 -06:00 committed by Chris Eager
parent 4d1bca2d97
commit 637792c6d4
1 changed files with 11 additions and 11 deletions

View File

@ -38,8 +38,8 @@ public class RemoveExpiredLinkedDevicesCommand extends AbstractSinglePassCrawlAc
private static final String UPDATED_ACCOUNTS_COUNTER_NAME = name(RemoveExpiredLinkedDevicesCommand.class, private static final String UPDATED_ACCOUNTS_COUNTER_NAME = name(RemoveExpiredLinkedDevicesCommand.class,
"updatedAccounts"); "updatedAccounts");
private static final String FAILED_ACCOUNT_UPDATES_COUNTER_NAME = name(RemoveExpiredLinkedDevicesCommand.class, private static final String FAILED_UPDATES_COUNTER_NAME = name(RemoveExpiredLinkedDevicesCommand.class,
"failedAccountUpdates"); "failedUpdates");
private static final Logger logger = LoggerFactory.getLogger(RemoveExpiredLinkedDevicesCommand.class); private static final Logger logger = LoggerFactory.getLogger(RemoveExpiredLinkedDevicesCommand.class);
public RemoveExpiredLinkedDevicesCommand() { public RemoveExpiredLinkedDevicesCommand() {
@ -90,16 +90,11 @@ public class RemoveExpiredLinkedDevicesCommand extends AbstractSinglePassCrawlAc
final Account account = accountAndExpiredDevices.getT1(); final Account account = accountAndExpiredDevices.getT1();
final Set<Byte> expiredDevices = accountAndExpiredDevices.getT2(); final Set<Byte> expiredDevices = accountAndExpiredDevices.getT2();
final Mono<Void> accountUpdate = dryRun final Mono<Long> accountUpdate = dryRun
? Mono.empty() ? Mono.empty()
: deleteDevices(account, expiredDevices); : deleteDevices(account, expiredDevices);
return accountUpdate.thenReturn(expiredDevices.size()) return accountUpdate.thenReturn(expiredDevices.size());
.onErrorResume(t -> {
logger.warn("Failed to remove expired linked devices {}", account.getUuid(), t);
Metrics.counter(FAILED_ACCOUNT_UPDATES_COUNTER_NAME).increment();
return Mono.empty();
});
}, maxConcurrency) }, maxConcurrency)
.doOnNext(removedDevices -> { .doOnNext(removedDevices -> {
@ -110,13 +105,18 @@ public class RemoveExpiredLinkedDevicesCommand extends AbstractSinglePassCrawlAc
.block(); .block();
} }
private Mono<Void> deleteDevices(final Account account, final Set<Byte> expiredDevices) { private Mono<Long> deleteDevices(final Account account, final Set<Byte> expiredDevices) {
return Flux.fromIterable(expiredDevices) return Flux.fromIterable(expiredDevices)
.flatMap(deviceId -> .flatMap(deviceId ->
Mono.fromFuture(() -> getCommandDependencies().accountsManager().removeDevice(account, deviceId)), Mono.fromFuture(() -> getCommandDependencies().accountsManager().removeDevice(account, deviceId)),
// limit concurrency to avoid contested updates // limit concurrency to avoid contested updates
1) 1)
.then(); .onErrorResume(t -> {
logger.warn("Failed to remove expired linked device {}", account.getUuid(), t);
Metrics.counter(FAILED_UPDATES_COUNTER_NAME).increment();
return Mono.empty();
})
.count();
} }
protected static Set<Byte> getExpiredLinkedDeviceIds(List<Device> devices) { protected static Set<Byte> getExpiredLinkedDeviceIds(List<Device> devices) {