Shutdown.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18. import com.l2jserver.Config;
  19. import com.l2jserver.L2DatabaseFactory;
  20. import com.l2jserver.gameserver.datatables.ClanTable;
  21. import com.l2jserver.gameserver.datatables.OfflineTradersTable;
  22. import com.l2jserver.gameserver.instancemanager.CHSiegeManager;
  23. import com.l2jserver.gameserver.instancemanager.CastleManorManager;
  24. import com.l2jserver.gameserver.instancemanager.CursedWeaponsManager;
  25. import com.l2jserver.gameserver.instancemanager.GlobalVariablesManager;
  26. import com.l2jserver.gameserver.instancemanager.GrandBossManager;
  27. import com.l2jserver.gameserver.instancemanager.HellboundManager;
  28. import com.l2jserver.gameserver.instancemanager.ItemAuctionManager;
  29. import com.l2jserver.gameserver.instancemanager.ItemsOnGroundManager;
  30. import com.l2jserver.gameserver.instancemanager.QuestManager;
  31. import com.l2jserver.gameserver.instancemanager.RaidBossSpawnManager;
  32. import com.l2jserver.gameserver.model.L2World;
  33. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  34. import com.l2jserver.gameserver.model.entity.Hero;
  35. import com.l2jserver.gameserver.model.olympiad.Olympiad;
  36. import com.l2jserver.gameserver.network.L2GameClient;
  37. import com.l2jserver.gameserver.network.SystemMessageId;
  38. import com.l2jserver.gameserver.network.communityserver.CommunityServerThread;
  39. import com.l2jserver.gameserver.network.gameserverpackets.ServerStatus;
  40. import com.l2jserver.gameserver.network.serverpackets.ServerClose;
  41. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  42. import com.l2jserver.gameserver.util.Broadcast;
  43. import gnu.trove.procedure.TObjectProcedure;
  44. /**
  45. * This class provides the functions for shutting down and restarting the server.<br>
  46. * It closes all open client connections and saves all data.
  47. * @version $Revision: 1.2.4.5 $ $Date: 2005/03/27 15:29:09 $
  48. */
  49. public class Shutdown extends Thread
  50. {
  51. private static final Logger _log = Logger.getLogger(Shutdown.class.getName());
  52. private static Shutdown _counterInstance = null;
  53. private int _secondsShut;
  54. private int _shutdownMode;
  55. public static final int SIGTERM = 0;
  56. public static final int GM_SHUTDOWN = 1;
  57. public static final int GM_RESTART = 2;
  58. public static final int ABORT = 3;
  59. private static final String[] MODE_TEXT =
  60. {
  61. "SIGTERM",
  62. "shutting down",
  63. "restarting",
  64. "aborting"
  65. };
  66. /**
  67. * This function starts a shutdown count down from Telnet (Copied from Function startShutdown())
  68. * @param seconds seconds until shutdown
  69. */
  70. private void SendServerQuit(int seconds)
  71. {
  72. SystemMessage sysm = SystemMessage.getSystemMessage(SystemMessageId.THE_SERVER_WILL_BE_COMING_DOWN_IN_S1_SECONDS);
  73. sysm.addNumber(seconds);
  74. Broadcast.toAllOnlinePlayers(sysm);
  75. }
  76. public void startTelnetShutdown(String IP, int seconds, boolean restart)
  77. {
  78. _log.warning("IP: " + IP + " issued shutdown command. " + MODE_TEXT[_shutdownMode] + " in " + seconds + " seconds!");
  79. if (restart)
  80. {
  81. _shutdownMode = GM_RESTART;
  82. }
  83. else
  84. {
  85. _shutdownMode = GM_SHUTDOWN;
  86. }
  87. if (_shutdownMode > 0)
  88. {
  89. switch (seconds)
  90. {
  91. case 540:
  92. case 480:
  93. case 420:
  94. case 360:
  95. case 300:
  96. case 240:
  97. case 180:
  98. case 120:
  99. case 60:
  100. case 30:
  101. case 10:
  102. case 5:
  103. case 4:
  104. case 3:
  105. case 2:
  106. case 1:
  107. break;
  108. default:
  109. SendServerQuit(seconds);
  110. }
  111. }
  112. if (_counterInstance != null)
  113. {
  114. _counterInstance._abort();
  115. }
  116. _counterInstance = new Shutdown(seconds, restart);
  117. _counterInstance.start();
  118. }
  119. /**
  120. * This function aborts a running countdown
  121. * @param IP IP Which Issued shutdown command
  122. */
  123. public void telnetAbort(String IP)
  124. {
  125. _log.warning("IP: " + IP + " issued shutdown ABORT. " + MODE_TEXT[_shutdownMode] + " has been stopped!");
  126. if (_counterInstance != null)
  127. {
  128. _counterInstance._abort();
  129. Announcements _an = Announcements.getInstance();
  130. _an.announceToAll("Server aborts " + MODE_TEXT[_shutdownMode] + " and continues normal operation!");
  131. }
  132. }
  133. /**
  134. * Default constructor is only used internal to create the shutdown-hook instance
  135. */
  136. protected Shutdown()
  137. {
  138. _secondsShut = -1;
  139. _shutdownMode = SIGTERM;
  140. }
  141. /**
  142. * This creates a countdown instance of Shutdown.
  143. * @param seconds how many seconds until shutdown
  144. * @param restart true is the server shall restart after shutdown
  145. */
  146. public Shutdown(int seconds, boolean restart)
  147. {
  148. if (seconds < 0)
  149. {
  150. seconds = 0;
  151. }
  152. _secondsShut = seconds;
  153. if (restart)
  154. {
  155. _shutdownMode = GM_RESTART;
  156. }
  157. else
  158. {
  159. _shutdownMode = GM_SHUTDOWN;
  160. }
  161. }
  162. /**
  163. * This function is called, when a new thread starts if this thread is the thread of getInstance, then this is the shutdown hook and we save all data and disconnect all clients.<br>
  164. * After this thread ends, the server will completely exit if this is not the thread of getInstance, then this is a countdown thread.<br>
  165. * We start the countdown, and when we finished it, and it was not aborted, we tell the shutdown-hook why we call exit, and then call exit when the exit status of the server is 1, startServer.sh / startServer.bat will restart the server.
  166. */
  167. @Override
  168. public void run()
  169. {
  170. if (this == getInstance())
  171. {
  172. TimeCounter tc = new TimeCounter();
  173. TimeCounter tc1 = new TimeCounter();
  174. try
  175. {
  176. if ((Config.OFFLINE_TRADE_ENABLE || Config.OFFLINE_CRAFT_ENABLE) && Config.RESTORE_OFFLINERS)
  177. {
  178. OfflineTradersTable.getInstance().storeOffliners();
  179. _log.info("Offline Traders Table: Offline shops stored(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  180. }
  181. }
  182. catch (Throwable t)
  183. {
  184. _log.log(Level.WARNING, "Error saving offline shops.", t);
  185. }
  186. try
  187. {
  188. disconnectAllCharacters();
  189. _log.info("All players disconnected and saved(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  190. }
  191. catch (Throwable t)
  192. {
  193. // ignore
  194. }
  195. // ensure all services are stopped
  196. try
  197. {
  198. GameTimeController.getInstance().stopTimer();
  199. _log.info("Game Time Controller: Timer stopped(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  200. }
  201. catch (Throwable t)
  202. {
  203. // ignore
  204. }
  205. // stop all threadpolls
  206. try
  207. {
  208. ThreadPoolManager.getInstance().shutdown();
  209. _log.info("Thread Pool Manager: Manager has been shut down(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  210. }
  211. catch (Throwable t)
  212. {
  213. // ignore
  214. }
  215. try
  216. {
  217. CommunityServerThread.getInstance().interrupt();
  218. _log.info("Community Server Thread: Thread interruped(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  219. }
  220. catch (Throwable t)
  221. {
  222. // ignore
  223. }
  224. try
  225. {
  226. LoginServerThread.getInstance().interrupt();
  227. _log.info("Login Server Thread: Thread interruped(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  228. }
  229. catch (Throwable t)
  230. {
  231. // ignore
  232. }
  233. // last byebye, save all data and quit this server
  234. saveData();
  235. tc.restartCounter();
  236. // saveData sends messages to exit players, so shutdown selector after it
  237. try
  238. {
  239. GameServer.gameServer.getSelectorThread().shutdown();
  240. _log.info("Game Server: Selector thread has been shut down(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  241. }
  242. catch (Throwable t)
  243. {
  244. // ignore
  245. }
  246. // commit data, last chance
  247. try
  248. {
  249. L2DatabaseFactory.getInstance().shutdown();
  250. _log.info("L2Database Factory: Database connection has been shut down(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  251. }
  252. catch (Throwable t)
  253. {
  254. }
  255. // server will quit, when this function ends.
  256. if (getInstance()._shutdownMode == GM_RESTART)
  257. {
  258. Runtime.getRuntime().halt(2);
  259. }
  260. else
  261. {
  262. Runtime.getRuntime().halt(0);
  263. }
  264. _log.info("The server has been successfully shut down in " + (tc1.getEstimatedTime() / 1000) + "seconds.");
  265. }
  266. else
  267. {
  268. // gm shutdown: send warnings and then call exit to start shutdown sequence
  269. countdown();
  270. // last point where logging is operational :(
  271. _log.warning("GM shutdown countdown is over. " + MODE_TEXT[_shutdownMode] + " NOW!");
  272. switch (_shutdownMode)
  273. {
  274. case GM_SHUTDOWN:
  275. getInstance().setMode(GM_SHUTDOWN);
  276. System.exit(0);
  277. break;
  278. case GM_RESTART:
  279. getInstance().setMode(GM_RESTART);
  280. System.exit(2);
  281. break;
  282. }
  283. }
  284. }
  285. /**
  286. * This functions starts a shutdown countdown.
  287. * @param activeChar GM who issued the shutdown command
  288. * @param seconds seconds until shutdown
  289. * @param restart true if the server will restart after shutdown
  290. */
  291. public void startShutdown(L2PcInstance activeChar, int seconds, boolean restart)
  292. {
  293. if (restart)
  294. {
  295. _shutdownMode = GM_RESTART;
  296. }
  297. else
  298. {
  299. _shutdownMode = GM_SHUTDOWN;
  300. }
  301. _log.warning("GM: " + activeChar.getName() + "(" + activeChar.getObjectId() + ") issued shutdown command. " + MODE_TEXT[_shutdownMode] + " in " + seconds + " seconds!");
  302. if (_shutdownMode > 0)
  303. {
  304. switch (seconds)
  305. {
  306. case 540:
  307. case 480:
  308. case 420:
  309. case 360:
  310. case 300:
  311. case 240:
  312. case 180:
  313. case 120:
  314. case 60:
  315. case 30:
  316. case 10:
  317. case 5:
  318. case 4:
  319. case 3:
  320. case 2:
  321. case 1:
  322. break;
  323. default:
  324. SendServerQuit(seconds);
  325. }
  326. }
  327. if (_counterInstance != null)
  328. {
  329. _counterInstance._abort();
  330. }
  331. // the main instance should only run for shutdown hook, so we start a new instance
  332. _counterInstance = new Shutdown(seconds, restart);
  333. _counterInstance.start();
  334. }
  335. /**
  336. * This function aborts a running countdown.
  337. * @param activeChar GM who issued the abort command
  338. */
  339. public void abort(L2PcInstance activeChar)
  340. {
  341. _log.warning("GM: " + activeChar.getName() + "(" + activeChar.getObjectId() + ") issued shutdown ABORT. " + MODE_TEXT[_shutdownMode] + " has been stopped!");
  342. if (_counterInstance != null)
  343. {
  344. _counterInstance._abort();
  345. Announcements _an = Announcements.getInstance();
  346. _an.announceToAll("Server aborts " + MODE_TEXT[_shutdownMode] + " and continues normal operation!");
  347. }
  348. }
  349. /**
  350. * Set the shutdown mode.
  351. * @param mode what mode shall be set
  352. */
  353. private void setMode(int mode)
  354. {
  355. _shutdownMode = mode;
  356. }
  357. /**
  358. * Set shutdown mode to ABORT.
  359. */
  360. private void _abort()
  361. {
  362. _shutdownMode = ABORT;
  363. }
  364. /**
  365. * This counts the countdown and reports it to all players countdown is aborted if mode changes to ABORT.
  366. */
  367. private void countdown()
  368. {
  369. try
  370. {
  371. while (_secondsShut > 0)
  372. {
  373. switch (_secondsShut)
  374. {
  375. case 540:
  376. SendServerQuit(540);
  377. break;
  378. case 480:
  379. SendServerQuit(480);
  380. break;
  381. case 420:
  382. SendServerQuit(420);
  383. break;
  384. case 360:
  385. SendServerQuit(360);
  386. break;
  387. case 300:
  388. SendServerQuit(300);
  389. break;
  390. case 240:
  391. SendServerQuit(240);
  392. break;
  393. case 180:
  394. SendServerQuit(180);
  395. break;
  396. case 120:
  397. SendServerQuit(120);
  398. break;
  399. case 60:
  400. LoginServerThread.getInstance().setServerStatus(ServerStatus.STATUS_DOWN); // avoids new players from logging in
  401. SendServerQuit(60);
  402. break;
  403. case 30:
  404. SendServerQuit(30);
  405. break;
  406. case 10:
  407. SendServerQuit(10);
  408. break;
  409. case 5:
  410. SendServerQuit(5);
  411. break;
  412. case 4:
  413. SendServerQuit(4);
  414. break;
  415. case 3:
  416. SendServerQuit(3);
  417. break;
  418. case 2:
  419. SendServerQuit(2);
  420. break;
  421. case 1:
  422. SendServerQuit(1);
  423. break;
  424. }
  425. _secondsShut--;
  426. int delay = 1000; // milliseconds
  427. Thread.sleep(delay);
  428. if (_shutdownMode == ABORT)
  429. {
  430. break;
  431. }
  432. }
  433. }
  434. catch (InterruptedException e)
  435. {
  436. // this will never happen
  437. }
  438. }
  439. /**
  440. * This sends a last byebye, disconnects all players and saves data.
  441. */
  442. private void saveData()
  443. {
  444. switch (_shutdownMode)
  445. {
  446. case SIGTERM:
  447. _log.info("SIGTERM received. Shutting down NOW!");
  448. break;
  449. case GM_SHUTDOWN:
  450. _log.info("GM shutdown received. Shutting down NOW!");
  451. break;
  452. case GM_RESTART:
  453. _log.info("GM restart received. Restarting NOW!");
  454. break;
  455. }
  456. /*
  457. * if (Config.ACTIVATE_POSITION_RECORDER) Universe.getInstance().implode(true);
  458. */
  459. TimeCounter tc = new TimeCounter();
  460. // Seven Signs data is now saved along with Festival data.
  461. if (!SevenSigns.getInstance().isSealValidationPeriod())
  462. {
  463. SevenSignsFestival.getInstance().saveFestivalData(false);
  464. _log.info("SevenSignsFestival: Festival data saved(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  465. }
  466. // Save Seven Signs data before closing. :)
  467. SevenSigns.getInstance().saveSevenSignsData();
  468. _log.info("SevenSigns: Seven Signs data saved(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  469. SevenSigns.getInstance().saveSevenSignsStatus();
  470. _log.info("SevenSigns: Seven Signs status saved(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  471. // Save all raidboss and GrandBoss status ^_^
  472. RaidBossSpawnManager.getInstance().cleanUp();
  473. _log.info("RaidBossSpawnManager: All raidboss info saved(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  474. GrandBossManager.getInstance().cleanUp();
  475. _log.info("GrandBossManager: All Grand Boss info saved(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  476. HellboundManager.getInstance().cleanUp();
  477. _log.info("Hellbound Manager: Data saved(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  478. _log.info("TradeController saving data.. This action may take some minutes! Please wait until completed!");
  479. TradeController.getInstance().dataCountStore();
  480. _log.info("TradeController: All count Item saved(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  481. ItemAuctionManager.getInstance().shutdown();
  482. _log.info("Item Auction Manager: All tasks stopped(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  483. Olympiad.getInstance().saveOlympiadStatus();
  484. _log.info("Olympiad System: Data saved(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  485. Hero.getInstance().shutdown();
  486. _log.info("Hero System: Data saved(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  487. ClanTable.getInstance().storeClanScore();
  488. _log.info("Clan System: Data saved(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  489. // Save Cursed Weapons data before closing.
  490. CursedWeaponsManager.getInstance().saveData();
  491. _log.info("Cursed Weapons Manager: Data saved(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  492. // Save all manor data
  493. CastleManorManager.getInstance().save();
  494. _log.info("Castle Manor Manager: Data saved(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  495. CHSiegeManager.getInstance().onServerShutDown();
  496. _log.info("CHSiegeManager: Siegable hall attacker lists saved!");
  497. // Save all global (non-player specific) Quest data that needs to persist after reboot
  498. QuestManager.getInstance().save();
  499. _log.info("Quest Manager: Data saved(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  500. // Save all global variables data
  501. GlobalVariablesManager.getInstance().saveVars();
  502. _log.info("Global Variables Manager: Variables saved(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  503. // Save items on ground before closing
  504. if (Config.SAVE_DROPPED_ITEM)
  505. {
  506. ItemsOnGroundManager.getInstance().saveInDb();
  507. _log.info("Items On Ground Manager: Data saved(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  508. ItemsOnGroundManager.getInstance().cleanUp();
  509. _log.info("Items On Ground Manager: Cleaned up(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
  510. }
  511. try
  512. {
  513. int delay = 5000;
  514. Thread.sleep(delay);
  515. }
  516. catch (InterruptedException e)
  517. {
  518. // never happens :p
  519. }
  520. }
  521. /**
  522. * This disconnects all clients from the server.
  523. */
  524. private void disconnectAllCharacters()
  525. {
  526. L2World.getInstance().getAllPlayers().safeForEachValue(new DisconnectAllCharacters());
  527. }
  528. protected final class DisconnectAllCharacters implements TObjectProcedure<L2PcInstance>
  529. {
  530. private final Logger _log = Logger.getLogger(DisconnectAllCharacters.class.getName());
  531. @Override
  532. public final boolean execute(final L2PcInstance player)
  533. {
  534. if (player != null)
  535. {
  536. // Logout Character
  537. try
  538. {
  539. L2GameClient client = player.getClient();
  540. if ((client != null) && !client.isDetached())
  541. {
  542. client.close(ServerClose.STATIC_PACKET);
  543. client.setActiveChar(null);
  544. player.setClient(null);
  545. }
  546. player.deleteMe();
  547. }
  548. catch (Throwable t)
  549. {
  550. _log.log(Level.WARNING, "Failed logour char " + player, t);
  551. }
  552. }
  553. return true;
  554. }
  555. }
  556. /**
  557. * A simple class used to track down the estimated time of method executions.<br>
  558. * Once this class is created, it saves the start time, and when you want to get the estimated time, use the getEstimatedTime() method.
  559. */
  560. private static final class TimeCounter
  561. {
  562. private long _startTime;
  563. protected TimeCounter()
  564. {
  565. restartCounter();
  566. }
  567. protected void restartCounter()
  568. {
  569. _startTime = System.currentTimeMillis();
  570. }
  571. protected long getEstimatedTimeAndRestartCounter()
  572. {
  573. final long toReturn = System.currentTimeMillis() - _startTime;
  574. restartCounter();
  575. return toReturn;
  576. }
  577. protected long getEstimatedTime()
  578. {
  579. return System.currentTimeMillis() - _startTime;
  580. }
  581. }
  582. /**
  583. * Get the shutdown-hook instance the shutdown-hook instance is created by the first call of this function, but it has to be registered externally.<br>
  584. * @return instance of Shutdown, to be used as shutdown hook
  585. */
  586. public static Shutdown getInstance()
  587. {
  588. return SingletonHolder._instance;
  589. }
  590. private static class SingletonHolder
  591. {
  592. protected static final Shutdown _instance = new Shutdown();
  593. }
  594. }