diff options
author | Simon Rettberg | 2015-08-25 16:28:42 +0200 |
---|---|---|
committer | Simon Rettberg | 2015-08-25 16:28:42 +0200 |
commit | 5b306a9b502c9c6a8818d0d1cbb47e4b25bb41d7 (patch) | |
tree | 40709a6e962ec2a4ca15927befab8f0c4bd1a717 /dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/SendExpireWarning.java | |
parent | [client] back button to the right of the button composite (diff) | |
download | tutor-module-5b306a9b502c9c6a8818d0d1cbb47e4b25bb41d7.tar.gz tutor-module-5b306a9b502c9c6a8818d0d1cbb47e4b25bb41d7.tar.xz tutor-module-5b306a9b502c9c6a8818d0d1cbb47e4b25bb41d7.zip |
[server] Implement scanning for soon-expiring lectures and images
Diffstat (limited to 'dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/SendExpireWarning.java')
-rw-r--r-- | dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/SendExpireWarning.java | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/SendExpireWarning.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/SendExpireWarning.java new file mode 100644 index 00000000..96436471 --- /dev/null +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/SendExpireWarning.java @@ -0,0 +1,107 @@ +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.DbImage; +import org.openslx.bwlp.sat.database.mappers.DbLecture; +import org.openslx.bwlp.sat.database.models.LocalImageVersion; +import org.openslx.bwlp.sat.util.FileSystem; +import org.openslx.bwlp.sat.util.Util; +import org.openslx.bwlp.thrift.iface.LectureSummary; +import org.openslx.util.QuickTimer; +import org.openslx.util.QuickTimer.Task; + +public class SendExpireWarning implements Runnable { + + private static final Logger LOGGER = Logger.getLogger(SendExpireWarning.class); + + private static final SendExpireWarning instance = new SendExpireWarning(); + + private static long blockedUntil = 0; + + /** + * Initialize the task. This schedules a timer that runs + * every 5 minutes. If the hour of day reaches 3, 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() != 3 || now.getMinuteOfHour() > 15) + return; + start(); + } + }, TimeUnit.MINUTES.toMillis(4), TimeUnit.MINUTES.toMillis(5)); + } + + 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() { + checkImages(); + checkLectures(); + } + + private void checkLectures() { + List<LectureSummary> lectures; + try { + lectures = DbLecture.getExpiringLectures(15); + } catch (SQLException e) { + LOGGER.warn("Could not get list of expiring lectures; skipping warning mails"); + return; + } + LOGGER.info("Scanning expiring lectures to send mails to users"); + final long now = Util.unixTime(); + for (LectureSummary lecture : lectures) { + final int days = (int) ((lecture.endTime - now) / 86400); + LOGGER.debug(lecture.lectureName + " expires in " + days); + if ((lecture.isEnabled && (days == 14 || days == 1)) || (days == 7)) { + Mailer.sendDeletionRemainder(lecture, days); + } + } + } + + private void checkImages() { + if (!FileSystem.isStorageMounted()) { + LOGGER.warn("Skipping sending warning mails about expiring images - storage seems unmounted"); + return; + } + // Get all images that expire in 15 days or less + List<LocalImageVersion> versions; + try { + versions = DbImage.getExpiringLocalImageVersions(15); + } catch (SQLException e) { + LOGGER.warn("Could not determine expiring versions; skipping warning mails"); + return; + } + LOGGER.info("Scanning for expiring images to send mails to users"); + // Send reminder on certain days + final long now = Util.unixTime(); + for (LocalImageVersion version : versions) { + final int days = (int) ((version.expireTime - now) / 86400); + LOGGER.debug(version.imageVersionId + " expires in " + days); + if ((version.isValid && (days == 14 || days == 7 || days == 1)) + || (!version.isValid && days == 3)) { + Mailer.sendDeletionReminder(version, days); + } + } + } + +} |