Shutdown.java 19 KB

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