src/org/sonews/util/Purger.java
changeset 39 73b21e9f3958
parent 36 c404a87db5b7
child 48 b78e77619152
     1.1 --- a/src/org/sonews/util/Purger.java	Sun Aug 29 17:43:58 2010 +0200
     1.2 +++ b/src/org/sonews/util/Purger.java	Mon Aug 30 00:20:06 2010 +0200
     1.3 @@ -40,110 +40,91 @@
     1.4  public class Purger extends AbstractDaemon
     1.5  {
     1.6  
     1.7 -  /**
     1.8 -   * Loops through all messages and deletes them if their time
     1.9 -   * has come.
    1.10 -   */
    1.11 -  @Override
    1.12 -  public void run()
    1.13 -  {
    1.14 -    try
    1.15 -    {
    1.16 -      while(isRunning())
    1.17 -      {
    1.18 -        purgeDeleted();
    1.19 -        purgeOutdated();
    1.20 +	/**
    1.21 +	 * Loops through all messages and deletes them if their time
    1.22 +	 * has come.
    1.23 +	 */
    1.24 +	@Override
    1.25 +	public void run()
    1.26 +	{
    1.27 +		try {
    1.28 +			while (isRunning()) {
    1.29 +				purgeDeleted();
    1.30 +				purgeOutdated();
    1.31  
    1.32 -        Thread.sleep(120000); // Sleep for two minutes
    1.33 -      }
    1.34 -    }
    1.35 -    catch(StorageBackendException ex)
    1.36 -    {
    1.37 -      ex.printStackTrace();
    1.38 -    }
    1.39 -    catch(InterruptedException ex)
    1.40 -    {
    1.41 -      Log.get().warning("Purger interrupted: " + ex);
    1.42 -    }
    1.43 -  }
    1.44 +				Thread.sleep(120000); // Sleep for two minutes
    1.45 +			}
    1.46 +		} catch (StorageBackendException ex) {
    1.47 +			ex.printStackTrace();
    1.48 +		} catch (InterruptedException ex) {
    1.49 +			Log.get().warning("Purger interrupted: " + ex);
    1.50 +		}
    1.51 +	}
    1.52  
    1.53 -  private void purgeDeleted()
    1.54 -    throws StorageBackendException
    1.55 -  {
    1.56 -    List<Channel> groups = StorageManager.current().getGroups();
    1.57 -    for(Channel channel : groups)
    1.58 -    {
    1.59 -      if(!(channel instanceof Group))
    1.60 -        continue;
    1.61 -      
    1.62 -      Group group = (Group)channel;
    1.63 -      // Look for groups that are marked as deleted
    1.64 -      if(group.isDeleted())
    1.65 -      {
    1.66 -        List<Long> ids = StorageManager.current().getArticleNumbers(group.getInternalID());
    1.67 -        if(ids.size() == 0)
    1.68 -        {
    1.69 -          StorageManager.current().purgeGroup(group);
    1.70 -          Log.get().info("Group " + group.getName() + " purged.");
    1.71 -        }
    1.72 +	private void purgeDeleted()
    1.73 +		throws StorageBackendException
    1.74 +	{
    1.75 +		List<Channel> groups = StorageManager.current().getGroups();
    1.76 +		for (Channel channel : groups) {
    1.77 +			if (!(channel instanceof Group)) {
    1.78 +				continue;
    1.79 +			}
    1.80  
    1.81 -        for(int n = 0; n < ids.size() && n < 10; n++)
    1.82 -        {
    1.83 -          Article art = StorageManager.current().getArticle(ids.get(n), group.getInternalID());
    1.84 -          StorageManager.current().delete(art.getMessageID());
    1.85 -          Log.get().info("Article " + art.getMessageID() + " purged.");
    1.86 -        }
    1.87 -      }
    1.88 -    }
    1.89 -  }
    1.90 +			Group group = (Group) channel;
    1.91 +			// Look for groups that are marked as deleted
    1.92 +			if (group.isDeleted()) {
    1.93 +				List<Long> ids = StorageManager.current().getArticleNumbers(group.getInternalID());
    1.94 +				if (ids.size() == 0) {
    1.95 +					StorageManager.current().purgeGroup(group);
    1.96 +					Log.get().info("Group " + group.getName() + " purged.");
    1.97 +				}
    1.98  
    1.99 -  private void purgeOutdated()
   1.100 -    throws InterruptedException, StorageBackendException
   1.101 -  {
   1.102 -    long articleMaximum =
   1.103 -      Config.inst().get("sonews.article.maxnum", Long.MAX_VALUE);
   1.104 -    long lifetime =
   1.105 -      Config.inst().get("sonews.article.lifetime", -1);
   1.106 +				for (int n = 0; n < ids.size() && n < 10; n++) {
   1.107 +					Article art = StorageManager.current().getArticle(ids.get(n), group.getInternalID());
   1.108 +					StorageManager.current().delete(art.getMessageID());
   1.109 +					Log.get().info("Article " + art.getMessageID() + " purged.");
   1.110 +				}
   1.111 +			}
   1.112 +		}
   1.113 +	}
   1.114  
   1.115 -    if(lifetime > 0 || articleMaximum < Stats.getInstance().getNumberOfNews())
   1.116 -    {
   1.117 -      Log.get().info("Purging old messages...");
   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 +	private void purgeOutdated()
   1.124 +		throws InterruptedException, StorageBackendException
   1.125 +	{
   1.126 +		long articleMaximum =
   1.127 +			Config.inst().get("sonews.article.maxnum", Long.MAX_VALUE);
   1.128 +		long lifetime =
   1.129 +			Config.inst().get("sonews.article.lifetime", -1);
   1.130  
   1.131 -      Article art = StorageManager.current().getArticle(mid);
   1.132 -      long artDate = 0;
   1.133 -      String dateStr = art.getHeader(Headers.DATE)[0];
   1.134 -      try
   1.135 -      {
   1.136 -        artDate = Date.parse(dateStr) / 1000 / 60 / 60 / 24;
   1.137 -      }
   1.138 -      catch (IllegalArgumentException ex)
   1.139 -      {
   1.140 -        Log.get().warning("Could not parse date string: " + dateStr + " " + ex);
   1.141 -      }
   1.142 +		if (lifetime > 0 || articleMaximum < Stats.getInstance().getNumberOfNews()) {
   1.143 +			Log.get().info("Purging old messages...");
   1.144 +			String mid = StorageManager.current().getOldestArticle();
   1.145 +			if (mid == null) // No articles in the database
   1.146 +			{
   1.147 +				return;
   1.148 +			}
   1.149  
   1.150 -      // Should we delete the message because of its age or because the
   1.151 -      // article maximum was reached?
   1.152 -      if (lifetime < 0 || artDate < (new Date().getTime() + lifetime))
   1.153 -      {
   1.154 -        StorageManager.current().delete(mid);
   1.155 -        System.out.println("Deleted: " + mid);
   1.156 -      }
   1.157 -      else
   1.158 -      {
   1.159 -        Thread.sleep(1000 * 60); // Wait 60 seconds
   1.160 -        return;
   1.161 -      }
   1.162 -    }
   1.163 -    else
   1.164 -    {
   1.165 -      Log.get().info("Lifetime purger is disabled");
   1.166 -      Thread.sleep(1000 * 60 * 30); // Wait 30 minutes
   1.167 -    }
   1.168 -  }
   1.169 +			Article art = StorageManager.current().getArticle(mid);
   1.170 +			long artDate = 0;
   1.171 +			String dateStr = art.getHeader(Headers.DATE)[0];
   1.172 +			try {
   1.173 +				artDate = Date.parse(dateStr) / 1000 / 60 / 60 / 24;
   1.174 +			} catch (IllegalArgumentException ex) {
   1.175 +				Log.get().warning("Could not parse date string: " + dateStr + " " + ex);
   1.176 +			}
   1.177  
   1.178 +			// Should we delete the message because of its age or because the
   1.179 +			// article maximum was reached?
   1.180 +			if (lifetime < 0 || artDate < (new Date().getTime() + lifetime)) {
   1.181 +				StorageManager.current().delete(mid);
   1.182 +				System.out.println("Deleted: " + mid);
   1.183 +			} else {
   1.184 +				Thread.sleep(1000 * 60); // Wait 60 seconds
   1.185 +				return;
   1.186 +			}
   1.187 +		} else {
   1.188 +			Log.get().info("Lifetime purger is disabled");
   1.189 +			Thread.sleep(1000 * 60 * 30); // Wait 30 minutes
   1.190 +		}
   1.191 +	}
   1.192  }