1.1 --- a/org/sonews/util/Purger.java Fri Jun 26 16:48:50 2009 +0200
1.2 +++ b/org/sonews/util/Purger.java Wed Aug 12 13:03:23 2009 +0200
1.3 @@ -18,70 +18,131 @@
1.4
1.5 package org.sonews.util;
1.6
1.7 -import org.sonews.daemon.Config;
1.8 -import org.sonews.daemon.storage.Database;
1.9 -import org.sonews.daemon.storage.Article;
1.10 +import org.sonews.daemon.AbstractDaemon;
1.11 +import org.sonews.config.Config;
1.12 +import org.sonews.storage.Article;
1.13 +import org.sonews.storage.Headers;
1.14 import java.util.Date;
1.15 +import java.util.List;
1.16 +import org.sonews.storage.Channel;
1.17 +import org.sonews.storage.Group;
1.18 +import org.sonews.storage.StorageBackendException;
1.19 +import org.sonews.storage.StorageManager;
1.20
1.21 /**
1.22 * The purger is started in configurable intervals to search
1.23 - * for old messages that can be purged.
1.24 + * for messages that can be purged. A message must be deleted if its lifetime
1.25 + * has exceeded, if it was marked as deleted or if the maximum number of
1.26 + * articles in the database is reached.
1.27 * @author Christian Lins
1.28 * @since sonews/0.5.0
1.29 */
1.30 -public class Purger
1.31 +public class Purger extends AbstractDaemon
1.32 {
1.33
1.34 - private long lifetime;
1.35 -
1.36 - public Purger()
1.37 - {
1.38 - this.lifetime = Config.getInstance().get("sonews.article.lifetime", 30)
1.39 - * 24L * 60L * 60L * 1000L; // in Milliseconds
1.40 - }
1.41 -
1.42 /**
1.43 * Loops through all messages and deletes them if their time
1.44 * has come.
1.45 */
1.46 - void purge()
1.47 - throws Exception
1.48 + @Override
1.49 + public void run()
1.50 {
1.51 - System.out.println("Purging old messages...");
1.52 + try
1.53 + {
1.54 + while(isRunning())
1.55 + {
1.56 + purgeDeleted();
1.57 + purgeOutdated();
1.58
1.59 - for (;;)
1.60 + Thread.sleep(120000); // Sleep for two minutes
1.61 + }
1.62 + }
1.63 + catch(StorageBackendException ex)
1.64 {
1.65 - // TODO: Delete articles directly in database
1.66 - Article art = null; //Database.getInstance().getOldestArticle();
1.67 - if (art == null) // No articles in the database
1.68 + ex.printStackTrace();
1.69 + }
1.70 + catch(InterruptedException ex)
1.71 + {
1.72 + Log.msg("Purger interrupted: " + ex, true);
1.73 + }
1.74 + }
1.75 +
1.76 + private void purgeDeleted()
1.77 + throws StorageBackendException
1.78 + {
1.79 + List<Channel> groups = StorageManager.current().getGroups();
1.80 + for(Channel channel : groups)
1.81 + {
1.82 + if(!(channel instanceof Group))
1.83 + continue;
1.84 +
1.85 + Group group = (Group)channel;
1.86 + // Look for groups that are marked as deleted
1.87 + if(group.isDeleted())
1.88 {
1.89 - break;
1.90 + List<Long> ids = StorageManager.current().getArticleNumbers(group.getInternalID());
1.91 + if(ids.size() == 0)
1.92 + {
1.93 + StorageManager.current().purgeGroup(group);
1.94 + Log.msg("Group " + group.getName() + " purged.", true);
1.95 + }
1.96 +
1.97 + for(int n = 0; n < ids.size() && n < 10; n++)
1.98 + {
1.99 + Article art = StorageManager.current().getArticle(ids.get(n), group.getInternalID());
1.100 + StorageManager.current().delete(art.getMessageID());
1.101 + Log.msg("Article " + art.getMessageID() + " purged.", true);
1.102 + }
1.103 + }
1.104 + }
1.105 + }
1.106 +
1.107 + private void purgeOutdated()
1.108 + throws InterruptedException, StorageBackendException
1.109 + {
1.110 + long articleMaximum =
1.111 + Config.inst().get("sonews.article.maxnum", Long.MAX_VALUE);
1.112 + long lifetime =
1.113 + Config.inst().get("sonews.article.lifetime", -1);
1.114 +
1.115 + if(lifetime > 0 || articleMaximum < Stats.getInstance().getNumberOfNews())
1.116 + {
1.117 + Log.msg("Purging old messages...", true);
1.118 + String mid = StorageManager.current().getOldestArticle();
1.119 + if (mid == null) // No articles in the database
1.120 + {
1.121 + return;
1.122 }
1.123
1.124 -/* if (art.getDate().getTime() < (new Date().getTime() + this.lifetime))
1.125 + Article art = StorageManager.current().getArticle(mid);
1.126 + long artDate = 0;
1.127 + String dateStr = art.getHeader(Headers.DATE)[0];
1.128 + try
1.129 {
1.130 - // Database.getInstance().delete(art);
1.131 - System.out.println("Deleted: " + art);
1.132 + artDate = Date.parse(dateStr) / 1000 / 60 / 60 / 24;
1.133 + }
1.134 + catch (IllegalArgumentException ex)
1.135 + {
1.136 + Log.msg("Could not parse date string: " + dateStr + " " + ex, true);
1.137 + }
1.138 +
1.139 + // Should we delete the message because of its age or because the
1.140 + // article maximum was reached?
1.141 + if (lifetime < 0 || artDate < (new Date().getTime() + lifetime))
1.142 + {
1.143 + StorageManager.current().delete(mid);
1.144 + System.out.println("Deleted: " + mid);
1.145 }
1.146 else
1.147 {
1.148 - break;
1.149 - }*/
1.150 + Thread.sleep(1000 * 60); // Wait 60 seconds
1.151 + return;
1.152 + }
1.153 }
1.154 - }
1.155 -
1.156 - public static void main(String[] args)
1.157 - {
1.158 - try
1.159 + else
1.160 {
1.161 - Purger purger = new Purger();
1.162 - purger.purge();
1.163 - System.exit(0);
1.164 - }
1.165 - catch(Exception ex)
1.166 - {
1.167 - ex.printStackTrace();
1.168 - System.exit(1);
1.169 + Log.msg("Lifetime purger is disabled", true);
1.170 + Thread.sleep(1000 * 60 * 30); // Wait 30 minutes
1.171 }
1.172 }
1.173