Added file purger (runs every 30 minutes, purges anything older than a day).

Use a logger instead of sout.
General cleanup, especially of console messages.
This commit is contained in:
Keir 2015-12-24 21:13:09 +00:00
parent a72125b1dc
commit 42aba8bda1
4 changed files with 152 additions and 26 deletions

View File

@ -0,0 +1,34 @@
package mineplex.chatsnap;
import java.io.File;
import java.io.FileFilter;
import java.util.UUID;
/**
* @author iKeirNez
*/
public class ChatSnapFileFilter implements FileFilter
{
private static final String FILE_EXTENSION = ".json";
@Override
public boolean accept(File file)
{
String fileName = file.getName();
if (file.isFile() && fileName.endsWith(FILE_EXTENSION))
{
String uuid = fileName.substring(0, fileName.length() - FILE_EXTENSION.length());
try
{
// this will trigger the catch block if invalid
UUID.fromString(uuid);
return true;
}
catch (IllegalArgumentException e) {}
}
return false;
}
}

View File

@ -7,6 +7,8 @@ import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@ -20,21 +22,33 @@ import redis.clients.jedis.JedisPubSub;
*/
public class JedisPubSubHandler extends JedisPubSub
{
private File _directory;
private Gson _gson = new GsonBuilder()
private static final Gson _gson = new GsonBuilder()
.setPrettyPrinting()
.create();
private JsonParser _jsonParser = new JsonParser();
private static final JsonParser _jsonParser = new JsonParser();
public JedisPubSubHandler(File directory)
private File _directory;
private Logger _logger;
public JedisPubSubHandler(File directory, Logger logger)
{
_directory = directory;
_logger = logger;
if (!directory.isDirectory())
if (_directory == null)
{
throw new IllegalArgumentException("Not a directory.");
throw new IllegalArgumentException("Directory cannot be null.");
}
if (!directory.exists() || !directory.isDirectory())
{
throw new IllegalArgumentException("Path non-existent or not a directory: " + directory.getAbsolutePath());
}
if (_logger == null)
{
throw new IllegalArgumentException("Logger cannot be null.");
}
}
@ -50,7 +64,7 @@ public class JedisPubSubHandler extends JedisPubSub
String token = jsonObject.get("token").getAsString();
File target = new File(_directory, token + ".json");
System.out.println("Received ChatSnap, token = " + token + ", writing to file.");
_logger.info("Chat snapshot received [" + token + "], writing to file.");
if (target.exists() && !jsonObject.has("snapshots"))
{
@ -73,8 +87,7 @@ public class JedisPubSubHandler extends JedisPubSub
}
catch (Exception e)
{
System.out.println("Error updating original JSON file.");
e.printStackTrace();
_logger.log(Level.SEVERE, "Exception whilst updating an original snapshot.", e);
}
}
@ -91,21 +104,17 @@ public class JedisPubSubHandler extends JedisPubSub
{
// dataString = token
File target = new File(_directory, dataString + ".json");
System.out.println("Removing ChatSnap, token = " + dataString);
_logger.info("Destroy command received [" + dataString + "].");
if (target.exists() && !target.delete())
{
System.out.println("Failed to delete: " + target.getPath());
_logger.warning("Failed to delete: " + target.getPath());
}
}
}
catch (Exception e)
{
System.out.println("Exception whilst receiving redis message.");
System.out.println("Channel = " + channel);
System.out.println("Data: ");
System.out.println(dataString);
e.printStackTrace();
_logger.log(Level.SEVERE, "Error whilst receiving redis message.", e);
}
}

View File

@ -0,0 +1,66 @@
package mineplex.chatsnap;
import java.io.File;
import java.io.FileFilter;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
/**
* @author iKeirNez
*/
public class PurgeTask implements Runnable
{
private static final FileFilter FILE_FILTER = new ChatSnapFileFilter();
private final File _dataDir;
private final Logger _logger;
public PurgeTask(File dataDir, Logger logger)
{
_dataDir = dataDir;
_logger = logger;
if (_dataDir == null)
{
throw new IllegalArgumentException("dataDir cannot be null.");
}
if (!_dataDir.exists() || !_dataDir.isDirectory())
{
throw new IllegalArgumentException("dataDir must exist and be a directory.");
}
if (_logger == null)
{
throw new IllegalArgumentException("Logger cannot be null.");
}
}
@Override
public void run()
{
int purgeCount = 0;
for (File file : _dataDir.listFiles(FILE_FILTER))
{
long lastModified = file.lastModified();
long timeSince = System.currentTimeMillis() - lastModified;
int days = (int) TimeUnit.MILLISECONDS.toDays(timeSince);
if (days >= 1)
{
if (!file.delete())
{
_logger.warning("Cannot delete file: " + file.getAbsolutePath());
}
else
{
purgeCount++;
}
}
}
_logger.info("Purged " + purgeCount + " old chat snapshots.");
}
}

View File

@ -1,6 +1,10 @@
package mineplex.chatsnap;
import java.io.File;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@ -16,35 +20,48 @@ public class ReportServer
public static void main(String[] args)
{
System.out.println("Starting...");
new ReportServer(new JedisPool(new JedisPoolConfig(), "10.3.203.80", 6379, 10)); // TODO is this okay?
Logger logger = Logger.getLogger("ReportServer");
logger.info("Starting report server.");
new ReportServer(new JedisPool(new JedisPoolConfig(), "10.3.203.80", 6379, 10), logger); // TODO is this okay?
}
private JedisPool _jedisPool;
private File dataDirectory = new File("data");
private Logger _logger;
private ScheduledExecutorService _executorService = Executors.newScheduledThreadPool(1);
public ReportServer(JedisPool jedisPool)
private File _dataDirectory = new File("data");
public ReportServer(JedisPool jedisPool, Logger logger)
{
_jedisPool = jedisPool;
_logger = logger;
if (dataDirectory.exists() && !dataDirectory.isDirectory())
if (_dataDirectory.exists() && !_dataDirectory.isDirectory())
{
throw new RuntimeException("Not a directory: " + dataDirectory.getPath());
throw new RuntimeException("Not a directory: " + _dataDirectory.getPath());
}
if (!dataDirectory.exists() && !dataDirectory.mkdir())
if (!_dataDirectory.exists() && !_dataDirectory.mkdir())
{
throw new RuntimeException("Unable to create directory: " + dataDirectory.getPath());
throw new RuntimeException("Unable to create directory: " + _dataDirectory.getPath());
}
registerHandler();
schedulePurgeTask();
_logger.info("Listening for commands on Redis.");
}
private void registerHandler()
{
try (Jedis jedis = _jedisPool.getResource())
{
jedis.subscribe(new JedisPubSubHandler(dataDirectory), CHANNEL_DEPLOY, CHANNEL_DESTROY);
jedis.subscribe(new JedisPubSubHandler(_dataDirectory, _logger), CHANNEL_DEPLOY, CHANNEL_DESTROY);
}
}
private void schedulePurgeTask()
{
_executorService.scheduleAtFixedRate(new PurgeTask(_dataDirectory, _logger), 0, 30, TimeUnit.MINUTES);
}
}