Shutdown.java 18 KB

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