summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/DeleteOldImages.java
blob: c3168253ca43af60126060039a5fa3796a3eecd1 (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
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.models.LocalImageVersion;
import org.openslx.bwlp.sat.util.FileSystem;
import org.openslx.util.QuickTimer;
import org.openslx.util.QuickTimer.Task;

/**
 * Delete old image versions (images that reached their expire time).
 */
public class DeleteOldImages implements Runnable {
	
	private static final Logger LOGGER = Logger.getLogger(DeleteOldImages.class);
	
	private static final DeleteOldImages instance = new DeleteOldImages();
	
	private static long blockedUntil = 0;

	/**
	 * Initialize the delete 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;
				if (Maintenance.trySubmit(instance)) {
					blockedUntil = System.currentTimeMillis() + TimeUnit.HOURS.toMillis(12);
				}
			}
		}, TimeUnit.MINUTES.toMillis(5), TimeUnit.MINUTES.toMillis(5));
	}
	
	private DeleteOldImages() {
	}

	@Override
	public void run() {
		LOGGER.info("Looking for old image versions to delete");
		List<LocalImageVersion> versions;
		try {
			versions = DbImage.getExpiringLocalImageVersions(0);
		} catch (SQLException e) {
			LOGGER.error("Will not be able to clean up old image versions");
			return;
		}
		// Mark all as invalid. This will also trigger emails.
		try {
			DbImage.markValid(false, false, versions.toArray(new LocalImageVersion[versions.size()]));
		} catch (SQLException e) {
			LOGGER.error("Could not mark images to be deleted as invalid. Cleanup of old images failed.");
			return;
		}
		// Delete them permanently only if they expired (at least) one day ago
		int hardDeleteCount = 0;
		final long hardDelete = (System.currentTimeMillis() / 1000) - 86400;
		for (LocalImageVersion version : versions) {
			if (version.expireTime < hardDelete) {
				hardDeleteCount++;
				FileSystem.deleteImageRelatedFiles(version);
				try {
					DbImage.deletePermanently(version);
				} catch (SQLException e) {
					// Logging done in method
				}
			}
		}
		LOGGER.info("Deletion done. Soft: " + versions.size() + ", hard: " + hardDeleteCount);
	}

}