Run updater task async

This commit is contained in:
Dan Mulloy 2018-07-05 16:15:14 -04:00 committed by Alexander Meech
parent 50b9148c7d
commit adfa2d8bc3

View File

@ -4,7 +4,10 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
@ -46,13 +49,13 @@ public class FileUpdater extends MiniPlugin
private static final FilenameFilter JAR_FILTER = (file, name) -> name.endsWith(".jar");
private Portal _portal;
private NautHashMap<String, String> _jarMd5Map = new NautHashMap<String, String>();
private Map<String, String> _jarMd5Map = new ConcurrentHashMap<>();
private String _serverName;
private Region _region;
private final GenericServer _transferHub;
private boolean _needUpdate;
private AtomicBoolean _needUpdate = new AtomicBoolean();
private boolean _enabled = true;
private boolean _restartTriggered = false;
@ -122,7 +125,7 @@ public class FileUpdater extends MiniPlugin
return;
}
if (!_needUpdate || !_enabled || _restartTriggered)
if (!_enabled || _restartTriggered || !_needUpdate.get())
{
return;
}
@ -160,25 +163,14 @@ public class FileUpdater extends MiniPlugin
@EventHandler(priority = EventPriority.HIGHEST)
public void reflectMotd(ServerListPingEvent event)
{
if (_needUpdate)
if (_needUpdate.get())
{
event.setMotd("Restarting soon");
}
}
@EventHandler
public void checkForUpdates(UpdateEvent event)
private void checkForUpdates()
{
if (event.getType() != UpdateType.MIN_01)
{
return;
}
if (_needUpdate || !_enabled)
{
return;
}
boolean windows = System.getProperty("os.name").startsWith("Windows");
File updateDir = new File((windows ? "C:" : File.separator + "home" + File.separator + "mineplex") + File.separator + "update");
@ -201,7 +193,7 @@ public class FileUpdater extends MiniPlugin
{
System.out.println(file.getName() + " old hash : " + hash);
System.out.println(file.getName() + " new hash : " + newHash);
_needUpdate = true;
_needUpdate.set(true);
}
}
catch (IOException ex)
@ -213,6 +205,22 @@ public class FileUpdater extends MiniPlugin
}
}
@EventHandler
public void checkForUpdates(UpdateEvent event)
{
if (event.getType() != UpdateType.MIN_01)
{
return;
}
if (!_enabled || _needUpdate.get())
{
return;
}
runAsync(this::checkForUpdates);
}
private void collectHashes(File[] files)
{
if (files == null)