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

Merge branch 'delete-inactive-guest-sessions' into 'master'

Delete inactive guest sessions

Sessions of guest users which have not been visited by the owner for a
fixed amount of time are now automatically deleted.

Depends on arsnova-setuptool!8

See merge request !36
parents 16c4cc6d 3e969181
Branches
Tags
1 merge request!36Delete inactive guest sessions
Pipeline #5389 passed with warnings with stages
in 3 minutes and 19 seconds
...@@ -801,7 +801,7 @@ public class CouchDBDao implements IDatabaseDao, ApplicationEventPublisherAware ...@@ -801,7 +801,7 @@ public class CouchDBDao implements IDatabaseDao, ApplicationEventPublisherAware
public Session updateSessionOwnerActivity(final Session session) { public Session updateSessionOwnerActivity(final Session session) {
try { try {
/* Do not clutter CouchDB. Only update once every 3 hours. */ /* Do not clutter CouchDB. Only update once every 3 hours. */
if (session.getLastOwnerActivity() > System.currentTimeMillis() - 3 * 3600000) { if (session.getLastOwnerActivity() > System.currentTimeMillis() - 60 * 1000) {
return session; return session;
} }
...@@ -1677,11 +1677,32 @@ public class CouchDBDao implements IDatabaseDao, ApplicationEventPublisherAware ...@@ -1677,11 +1677,32 @@ public class CouchDBDao implements IDatabaseDao, ApplicationEventPublisherAware
public void deleteSession(final Session session) { public void deleteSession(final Session session) {
try { try {
deleteDocument(session.get_id()); deleteDocument(session.get_id());
LOGGER.debug("Deleted session document {} and related data.", session.get_id());
} catch (final IOException e) { } catch (final IOException e) {
LOGGER.error("Could not delete session {}", session); LOGGER.error("Could not delete session {}", session);
} }
} }
@Override
public boolean deleteInactiveGuestSessions(long lastActivityBefore) {
NovaView view = new NovaView("session/by_last_activity_for_guests");
view.setEndKey(lastActivityBefore);
List<Document> results = this.getDatabase().view(view).getResults();
for (Document oldDoc : results) {
Session s = new Session();
s.set_id(oldDoc.getId());
s.set_rev(oldDoc.getJSONObject("value").getString("_rev"));
deleteSession(s);
}
if (results.size() > 0) {
LOGGER.info("Deleted {} inactive guest sessions.", results.size());
}
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 = 60 * 1000L; // 30 * 60 * 1000L;
private static final long SESSION_INACTIVITY_THRESHOLD_MS = 3 * 60 * 1000L; // 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