From 0578f94522e2e1ed4697e2d62929f80689c630c6 Mon Sep 17 00:00:00 2001 From: md_5 Date: Thu, 30 May 2013 16:23:02 +1000 Subject: [PATCH] Rework shutdown sequence to close #391 --- .../main/java/net/md_5/bungee/BungeeCord.java | 88 +++++++++++-------- .../main/java/net/md_5/bungee/Metrics.java | 45 ++++------ 2 files changed, 66 insertions(+), 67 deletions(-) diff --git a/proxy/src/main/java/net/md_5/bungee/BungeeCord.java b/proxy/src/main/java/net/md_5/bungee/BungeeCord.java index 3046d91e..dacd10f9 100644 --- a/proxy/src/main/java/net/md_5/bungee/BungeeCord.java +++ b/proxy/src/main/java/net/md_5/bungee/BungeeCord.java @@ -85,6 +85,7 @@ public class BungeeCord extends ProxyServer * locations.yml save thread. */ private final Timer saveThread = new Timer( "Reconnect Saver" ); + private final Timer metricsThread = new Timer( "Metrics Thread" ); /** * Server socket listener. */ @@ -213,8 +214,7 @@ public class BungeeCord extends ProxyServer getReconnectHandler().save(); } }, 0, TimeUnit.MINUTES.toMillis( 5 ) ); - - new Metrics().start(); + metricsThread.scheduleAtFixedRate( new Metrics(), 0, TimeUnit.MINUTES.toMillis( Metrics.PING_INTERVAL ) ); } public void startListeners() @@ -265,44 +265,58 @@ public class BungeeCord extends ProxyServer @Override public void stop() { - this.isRunning = false; - - httpClient.close(); - executors.shutdown(); - - stopListeners(); - getLogger().info( "Closing pending connections" ); - - connectionLock.readLock().lock(); - try + new Thread( "Shutdown Thread" ) { - getLogger().info( "Disconnecting " + connections.size() + " connections" ); - for ( UserConnection user : connections.values() ) + @Override + public void run() { - user.disconnect( getTranslation( "restart" ) ); + BungeeCord.this.isRunning = false; + + httpClient.close(); + executors.shutdown(); + + stopListeners(); + getLogger().info( "Closing pending connections" ); + + connectionLock.readLock().lock(); + try + { + getLogger().info( "Disconnecting " + connections.size() + " connections" ); + for ( UserConnection user : connections.values() ) + { + user.disconnect( getTranslation( "restart" ) ); + } + } finally + { + connectionLock.readLock().unlock(); + } + + getLogger().info( "Closing IO threads" ); + eventLoops.shutdownGracefully(); + try + { + eventLoops.awaitTermination( Long.MAX_VALUE, TimeUnit.NANOSECONDS ); + } catch ( InterruptedException ex ) + { + } + + getLogger().info( "Saving reconnect locations" ); + reconnectHandler.save(); + saveThread.cancel(); + metricsThread.cancel(); + + // TODO: Fix this shit + getLogger().info( "Disabling plugins" ); + for ( Plugin plugin : pluginManager.getPlugins() ) + { + plugin.onDisable(); + getScheduler().cancel( plugin ); + } + + getLogger().info( "Thankyou and goodbye" ); + System.exit( 0 ); } - } finally - { - connectionLock.readLock().unlock(); - } - - getLogger().info( "Closing IO threads" ); - eventLoops.shutdownGracefully(); - - getLogger().info( "Saving reconnect locations" ); - reconnectHandler.save(); - saveThread.cancel(); - - // TODO: Fix this shit - getLogger().info( "Disabling plugins" ); - for ( Plugin plugin : pluginManager.getPlugins() ) - { - plugin.onDisable(); - getScheduler().cancel( plugin ); - } - - getLogger().info( "Thankyou and goodbye" ); - System.exit( 0 ); + }.start(); } /** diff --git a/proxy/src/main/java/net/md_5/bungee/Metrics.java b/proxy/src/main/java/net/md_5/bungee/Metrics.java index 0e012e20..77c8828b 100644 --- a/proxy/src/main/java/net/md_5/bungee/Metrics.java +++ b/proxy/src/main/java/net/md_5/bungee/Metrics.java @@ -8,9 +8,10 @@ import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; +import java.util.TimerTask; import net.md_5.bungee.api.ProxyServer; -public class Metrics extends Thread +public class Metrics extends TimerTask { /** @@ -28,41 +29,25 @@ public class Metrics extends Thread /** * Interval of time to ping (in minutes) */ - private final static int PING_INTERVAL = 10; - - public Metrics() - { - super( "Metrics Gathering Thread" ); - setDaemon( true ); - } + final static int PING_INTERVAL = 10; + boolean firstPost = true; @Override public void run() { - boolean firstPost = true; - while ( true ) + try { - try - { - // We use the inverse of firstPost because if it is the first time we are posting, - // it is not a interval ping, so it evaluates to FALSE - // Each time thereafter it will evaluate to TRUE, i.e PING! - postPlugin( !firstPost ); + // We use the inverse of firstPost because if it is the first time we are posting, + // it is not a interval ping, so it evaluates to FALSE + // Each time thereafter it will evaluate to TRUE, i.e PING! + postPlugin( !firstPost ); - // After the first post we set firstPost to false - // Each post thereafter will be a ping - firstPost = false; - } catch ( IOException ex ) - { - ProxyServer.getInstance().getLogger().info( "[Metrics] " + ex.getMessage() ); - } - try - { - sleep( PING_INTERVAL * 1000 * 60 ); - } catch ( InterruptedException ex ) - { - break; - } + // After the first post we set firstPost to false + // Each post thereafter will be a ping + firstPost = false; + } catch ( IOException ex ) + { + ProxyServer.getInstance().getLogger().info( "[Metrics] " + ex.getMessage() ); } }