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

Delete inactive sessions created by guests periodically

parent e34c6de1
Branches
Tags
No related merge requests found
...@@ -1679,6 +1679,36 @@ public class CouchDBDao implements IDatabaseDao, ApplicationEventPublisherAware ...@@ -1679,6 +1679,36 @@ public class CouchDBDao implements IDatabaseDao, ApplicationEventPublisherAware
} }
} }
@Override
public boolean deleteInactiveGuestSessions(long lastActivityBefore) {
try {
NovaView view = new NovaView("session/by_last_activity_for_guests");
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 session document {} for deletion.", oldDoc.getId());
}
if (newDocs.size() > 0) {
getDatabase().bulkSaveDocuments(newDocs.toArray(new Document[newDocs.size()]));
LOGGER.info("Deleted {} inactive guest sessions.", newDocs.size());
}
return true;
} catch (IOException e) {
LOGGER.error("Could not delete inactive guest sessions.");
}
return false;
}
@Cacheable("lecturequestions") @Cacheable("lecturequestions")
@Override @Override
public List<Question> getLectureQuestionsForUsers(final Session session) { public List<Question> getLectureQuestionsForUsers(final Session session) {
......
...@@ -146,6 +146,8 @@ public interface IDatabaseDao { ...@@ -146,6 +146,8 @@ public interface IDatabaseDao {
void deleteSession(Session session); void deleteSession(Session session);
boolean deleteInactiveGuestSessions(long lastActivityBefore);
List<Question> getLectureQuestionsForUsers(Session session); List<Question> getLectureQuestionsForUsers(Session session);
List<Question> getLectureQuestionsForTeachers(Session session); List<Question> getLectureQuestionsForTeachers(Session session);
......
...@@ -91,6 +91,8 @@ public interface ISessionService { ...@@ -91,6 +91,8 @@ public interface ISessionService {
SessionFeature changeSessionFeatures(String sessionkey, SessionFeature features); SessionFeature changeSessionFeatures(String sessionkey, SessionFeature features);
boolean lockFeedbackInput(String sessionkey, Boolean lock); boolean lockFeedbackInput(String sessionkey, Boolean lock);
boolean flipFlashcards(String sessionkey, Boolean flip); boolean flipFlashcards(String sessionkey, Boolean flip);
void deleteInactiveSessions();
} }
...@@ -47,6 +47,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -47,6 +47,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -98,6 +99,9 @@ public class SessionService implements ISessionService, ApplicationEventPublishe ...@@ -98,6 +99,9 @@ public class SessionService implements ISessionService, ApplicationEventPublishe
} }
} }
private static final long SESSION_INACTIVITY_CHECK_INTERVAL_MS = 30 * 60 * 1000L;
private static final long SESSION_INACTIVITY_THRESHOLD_MS = 90 * 24 * 60 * 60 * 1000L;
@Autowired @Autowired
private IDatabaseDao databaseDao; private IDatabaseDao databaseDao;
...@@ -123,6 +127,14 @@ public class SessionService implements ISessionService, ApplicationEventPublishe ...@@ -123,6 +127,14 @@ public class SessionService implements ISessionService, ApplicationEventPublishe
public static final Logger LOGGER = LoggerFactory.getLogger(SessionService.class); public static final Logger LOGGER = LoggerFactory.getLogger(SessionService.class);
@Scheduled(fixedDelay = SESSION_INACTIVITY_CHECK_INTERVAL_MS)
public void deleteInactiveSessions() {
LOGGER.info("Delete inactive sessions.");
long unixTime = System.currentTimeMillis();
long lastActivityBefore = unixTime - SESSION_INACTIVITY_THRESHOLD_MS;
databaseDao.deleteInactiveGuestSessions(lastActivityBefore);
}
public void setDatabaseDao(final IDatabaseDao newDatabaseDao) { public void setDatabaseDao(final IDatabaseDao newDatabaseDao) {
databaseDao = newDatabaseDao; databaseDao = newDatabaseDao;
} }
......
...@@ -345,6 +345,12 @@ public class StubDatabaseDao implements IDatabaseDao { ...@@ -345,6 +345,12 @@ public class StubDatabaseDao implements IDatabaseDao {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
@Override
public boolean deleteInactiveGuestSessions(long lastActivityBefore) {
// TODO Auto-generated method stub
return false;
}
@Override @Override
public void deleteAllQuestionsWithAnswers(Session session) { public void deleteAllQuestionsWithAnswers(Session session) {
// TODO Auto-generated method stub // 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