Shutdown.java 19 KB

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