Shutdown.java 19 KB

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