Log seen users

This commit is contained in:
Andrea Cavalli 2022-01-22 20:24:35 +01:00
parent 3a74997b49
commit 5991b116f3
1 changed files with 13 additions and 3 deletions

View File

@ -16,6 +16,7 @@ import java.time.Instant;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentHashMap.KeySetView;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
@ -53,6 +54,12 @@ public class PeriodicRestarter {
*/
private final ConcurrentMap<Long, Boolean> sessionAuthReady = new ConcurrentHashMap<>();
/**
* Useful to register sessions at startup
* User id -> x
*/
private final KeySetView<Long, Object> seenUsers = new ConcurrentHashMap<Long, Object>().keySet(new Object());
public PeriodicRestarter(ReactiveApi api, Duration interval, Set<Long> restartUserIds) {
this.api = api;
this.interval = interval;
@ -113,9 +120,12 @@ public class PeriodicRestarter {
}
}
} else if (event.update() instanceof UpdateNewMessage) {
var wasReady = requireNonNullElse(this.sessionAuthReady.put(event.liveId(), true), false);
if (!wasReady) {
onSessionReady(event.liveId(), event.userId());
if (seenUsers.add(event.userId())) {
LOG.info("Found a running bot that wasn't registered: {} (liveId: {})", event.userId(), event.liveId());
var wasReady = requireNonNullElse(this.sessionAuthReady.put(event.liveId(), true), false);
if (!wasReady) {
onSessionReady(event.liveId(), event.userId());
}
}
}
})