Shutdown.java 19 KB

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