Shutdown.java 19 KB

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