Skip to content
Snippets Groups Projects
Commit df73a254 authored by Daniel Gerhardt's avatar Daniel Gerhardt
Browse files

Delete inactive users in intervals

parent e34c6de1
No related merge requests found
......@@ -2212,6 +2212,36 @@ public class CouchDBDao implements IDatabaseDao, ApplicationEventPublisherAware
return false;
}
@Override
public boolean deleteInactiveUsers(long lastActivityBefore) {
try {
NovaView view = new NovaView("user/inactive_by_creation");
view.setEndKey(lastActivityBefore);
List<Document> results = this.getDatabase().view(view).getResults();
final List<Document> newDocs = new ArrayList<Document>();
for (Document oldDoc : results) {
final Document newDoc = new Document();
newDoc.setId(oldDoc.getId());
newDoc.setRev(oldDoc.getJSONObject("value").getString("_rev"));
newDoc.put("_deleted", true);
newDocs.add(newDoc);
LOGGER.debug("Marked user document {} for deletion.", oldDoc.getId());
}
if (newDocs.size() > 0) {
getDatabase().bulkSaveDocuments(newDocs.toArray(new Document[newDocs.size()]));
LOGGER.info("Deleted {} inactive users.", newDocs.size());
}
return true;
} catch (IOException e) {
LOGGER.error("Could not delete inactive users.");
}
return false;
}
@Override
public SessionInfo importSession(User user, ImportExportSession importSession) {
final Session session = this.saveSession(user, importSession.generateSessionEntity(user));
......
......@@ -196,6 +196,8 @@ public interface IDatabaseDao {
boolean deleteUser(DbUser dbUser);
boolean deleteInactiveUsers(long lastActivityBefore);
CourseScore getLearningProgress(Session session);
List<SessionInfo> getMySessionsInfo(User user, final int start, final int limit);
......
......@@ -56,15 +56,8 @@ import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
......@@ -82,6 +75,9 @@ public class UserService implements IUserService {
private static final int PASSWORD_RESET_KEY_DURABILITY_MS = 2 * 60 * 60 * 1000;
private static final long ACTIVATION_KEY_CHECK_INTERVAL_MS = 30 * 60 * 1000L;
private static final long ACTIVATION_KEY_DURABILITY_MS = 6 * 60 * 60 * 1000L;
public static final Logger LOGGER = LoggerFactory.getLogger(UserService.class);
private static final ConcurrentHashMap<UUID, User> socketid2user = new ConcurrentHashMap<UUID, User>();
......@@ -161,6 +157,14 @@ public class UserService implements IUserService {
}
}
@Scheduled(fixedDelay = ACTIVATION_KEY_CHECK_INTERVAL_MS)
public void deleteInactiveUsers() {
LOGGER.info("Delete inactive users.");
long unixTime = System.currentTimeMillis();
long lastActivityBefore = unixTime - ACTIVATION_KEY_DURABILITY_MS;
databaseDao.deleteInactiveUsers(lastActivityBefore);
}
@Override
public User getCurrentUser() {
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
......
......@@ -458,6 +458,12 @@ public class StubDatabaseDao implements IDatabaseDao {
return false;
}
@Override
public boolean deleteInactiveUsers(long lastActivityBefore) {
// TODO Auto-generated method stub
return false;
}
@Override
public List<InterposedQuestion> getInterposedQuestions(Session session, User user, final int start, final int limit) {
// TODO Auto-generated method stub
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment