Skip to content

Commit f7d5a28

Browse files
committed
improve SyncLdapTask
1 parent 0d10a6c commit f7d5a28

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

aaa/src/main/java/name/nkonev/aaa/entity/jdbc/UserAccount.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,32 @@ public UserAccount withLoginColor(String newLoginColor) {
320320
);
321321
}
322322

323+
public UserAccount withSyncLdapTime(LocalDateTime newSyncLdapDateTime) {
324+
return new UserAccount(
325+
id,
326+
creationType,
327+
username,
328+
password,
329+
avatar,
330+
avatarBig,
331+
shortInfo,
332+
expired,
333+
locked,
334+
enabled,
335+
confirmed,
336+
roles,
337+
email,
338+
lastLoginDateTime,
339+
facebookId,
340+
vkontakteId,
341+
googleId,
342+
keycloakId,
343+
ldapId,
344+
loginColor,
345+
newSyncLdapDateTime
346+
);
347+
}
348+
323349
public UserAccount withOauthIdentifiers(OAuth2Identifiers newOauthIdentifiers) {
324350
return new UserAccount(
325351
id,

aaa/src/main/java/name/nkonev/aaa/repository/jdbc/UserAccountRepository.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ public interface UserAccountRepository extends ListCrudRepository<UserAccount, L
4242

4343
List<UserAccount> findByLdapIdInOrderById(Collection<String> strings);
4444

45-
@Modifying
46-
@Query("update user_account set sync_ldap_time = :newSyncLdapDateTime where ldap_id in (:ldapUserIds)")
47-
void updateSyncLdapTime(Set<String> ldapUserIds, LocalDateTime newSyncLdapDateTime);
48-
4945
@Modifying
5046
@Query("delete from user_account where ldap_id is not null and sync_ldap_time < :currTime")
51-
void deleteWithLdapIdElderThan(LocalDateTime currTime);
47+
long deleteWithLdapIdElderThan(LocalDateTime currTime);
5248
}

aaa/src/main/java/name/nkonev/aaa/tasks/SyncLdapTask.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.springframework.util.StringUtils;
2323

2424
import java.time.LocalDateTime;
25-
import java.time.ZoneId;
2625
import java.util.*;
2726
import java.util.function.Consumer;
2827
import java.util.stream.Collectors;
@@ -94,7 +93,8 @@ public void doWork() {
9493
handler.processLeftovers();
9594

9695
LOGGER.info("Deleting entries from database");
97-
transactionTemplate.executeWithoutResult(s -> userAccountRepository.deleteWithLdapIdElderThan(currTime));
96+
var deleted = transactionTemplate.execute(s -> userAccountRepository.deleteWithLdapIdElderThan(currTime));
97+
LOGGER.info("Deleted {} entries from database", deleted);
9898

9999
LOGGER.info("Sync ldap task finish");
100100
}
@@ -193,6 +193,7 @@ private void processUserBatch(List<LdapEntity> attributes) {
193193
}
194194

195195
if (shouldUpdateInDb) {
196+
userAccount = userAccount.withSyncLdapTime(currTime);
196197
LOGGER.info("Updating userId={}, ldapId={}", userAccount.id(), ldapUserId);
197198
userAccountRepository.save(userAccount);
198199
}
@@ -229,10 +230,6 @@ private void processUserBatch(List<LdapEntity> attributes) {
229230
var inserted = userAccountRepository.saveAll(usersToInsert);
230231
LOGGER.debug("Inserted {} to database", inserted);
231232
}
232-
233-
if (!byLdapId.keySet().isEmpty()) {
234-
userAccountRepository.updateSyncLdapTime(byLdapId.keySet(), currTime);
235-
}
236233
});
237234
}
238235
}

0 commit comments

Comments
 (0)