summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/SendExpireWarning.java
blob: db1baa36c9c5f4a8e187fbc35f104c6fa7e037e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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.mail.MailGenerator;
import org.openslx.bwlp.sat.util.FileSystem;
import org.openslx.bwlp.thrift.iface.LectureSummary;
import org.openslx.util.QuickTimer;
import org.openslx.util.QuickTimer.Task;
import org.openslx.util.Util;

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);
			if ((lecture.isEnabled && (days == 14 || days == 1)) || (days == 7)) {
				LOGGER.debug(lecture.lectureName + " expires in " + days);
				MailGenerator.sendLectureExpiringReminder(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);
			boolean mailNormal = (version.isValid && (days == 14 || days == 7 || days == 1))
					|| (!version.isValid && days == 3);
			boolean mailForced = version.isValid && days == 1;
			if (mailNormal || mailForced) {
				LOGGER.debug(version.imageVersionId + " expires in " + days);
				MailGenerator.sendImageDeletionReminder(version, days, mailForced);
			}
		}
	}
}