summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org
diff options
context:
space:
mode:
authorSimon Rettberg2017-06-19 13:36:28 +0200
committerSimon Rettberg2017-06-19 13:36:28 +0200
commitc300d0689434bd07f814e121e2e14c1cf4884d82 (patch)
treec7fd92128b287d92c178aa8653305d41770dd0ce /dozentenmodulserver/src/main/java/org
parentMerge branch 'master' of git.openslx.org:openslx-ng/tutor-module (diff)
downloadtutor-module-c300d0689434bd07f814e121e2e14c1cf4884d82.tar.gz
tutor-module-c300d0689434bd07f814e121e2e14c1cf4884d82.tar.xz
tutor-module-c300d0689434bd07f814e121e2e14c1cf4884d82.zip
[server] Add missing new class
Diffstat (limited to 'dozentenmodulserver/src/main/java/org')
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/DeleteOldUsers.java76
1 files changed, 76 insertions, 0 deletions
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/DeleteOldUsers.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/DeleteOldUsers.java
new file mode 100644
index 00000000..ebc0cfbd
--- /dev/null
+++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/DeleteOldUsers.java
@@ -0,0 +1,76 @@
+package org.openslx.bwlp.sat.maintenance;
+
+import java.sql.SQLException;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.log4j.Logger;
+import org.joda.time.DateTime;
+import org.openslx.bwlp.sat.database.mappers.DbLog;
+import org.openslx.bwlp.sat.database.mappers.DbUser;
+import org.openslx.bwlp.sat.util.Formatter;
+import org.openslx.bwlp.thrift.iface.UserInfo;
+import org.openslx.util.QuickTimer;
+import org.openslx.util.QuickTimer.Task;
+
+/**
+ * Delete old image versions (images that reached their expire time).
+ */
+public class DeleteOldUsers implements Runnable {
+
+ private static final Logger LOGGER = Logger.getLogger(DeleteOldUsers.class);
+
+ private static final DeleteOldUsers instance = new DeleteOldUsers();
+
+ private static long blockedUntil = 0;
+
+ /**
+ * Initialize the delete task. This schedules a timer that runs
+ * every 6 minutes. If the hour of day reaches 1, it will fire
+ * the task, and block it from running for the next 12 hours.
+ */
+ public synchronized static void init() {
+ if (blockedUntil != 0)
+ return;
+ blockedUntil = 1;
+ QuickTimer.scheduleAtFixedRate(new Task() {
+ @Override
+ public void fire() {
+ if (blockedUntil > System.currentTimeMillis())
+ return;
+ DateTime now = DateTime.now();
+ if (now.getHourOfDay() != 1 || now.getMinuteOfHour() > 15)
+ return;
+ start();
+ }
+ }, TimeUnit.MINUTES.toMillis(5), TimeUnit.MINUTES.toMillis(6));
+ }
+
+ public synchronized static void start() {
+ if (blockedUntil > System.currentTimeMillis())
+ return;
+ if (Maintenance.trySubmit(instance)) {
+ blockedUntil = System.currentTimeMillis() + TimeUnit.HOURS.toMillis(12);
+ }
+ }
+
+ @Override
+ public void run() {
+ List<UserInfo> inactiveUsers;
+ try {
+ inactiveUsers = DbUser.getInactive();
+ } catch (SQLException e) {
+ LOGGER.warn("Cannot get list of old users for deletion");
+ return;
+ }
+ for (UserInfo user : inactiveUsers) {
+ try {
+ DbUser.deleteUser(user);
+ DbLog.log((String)null, null, "Deleted inactive user " + Formatter.userFullName(user));
+ } catch (SQLException e) {
+ // Ignore, constraint
+ }
+ }
+ }
+
+}