Shutdown.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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.CastleManorManager;
  24. import com.l2jserver.gameserver.instancemanager.CursedWeaponsManager;
  25. import com.l2jserver.gameserver.instancemanager.GlobalVariablesManager;
  26. import com.l2jserver.gameserver.instancemanager.GrandBossManager;
  27. import com.l2jserver.gameserver.instancemanager.ItemAuctionManager;
  28. import com.l2jserver.gameserver.instancemanager.ItemsOnGroundManager;
  29. import com.l2jserver.gameserver.instancemanager.QuestManager;
  30. import com.l2jserver.gameserver.instancemanager.RaidBossSpawnManager;
  31. import com.l2jserver.gameserver.model.L2World;
  32. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  33. import com.l2jserver.gameserver.model.entity.Hero;
  34. import com.l2jserver.gameserver.model.olympiad.Olympiad;
  35. import com.l2jserver.gameserver.network.L2GameClient;
  36. import com.l2jserver.gameserver.network.SystemMessageId;
  37. import com.l2jserver.gameserver.network.communityserver.CommunityServerThread;
  38. import com.l2jserver.gameserver.network.gameserverpackets.ServerStatus;
  39. import com.l2jserver.gameserver.network.serverpackets.ServerClose;
  40. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  41. import com.l2jserver.gameserver.util.Broadcast;
  42. /**
  43. *
  44. * This class provides the functions for shutting down and restarting the server
  45. * It closes all open clientconnections and saves all data.
  46. *
  47. * @version $Revision: 1.2.4.5 $ $Date: 2005/03/27 15:29:09 $
  48. */
  49. public class Shutdown extends Thread
  50. {
  51. private static Logger _log = Logger.getLogger(Shutdown.class.getName());
  52. private static Shutdown _counterInstance = null;
  53. private int _secondsShut;
  54. private int _shutdownMode;
  55. public static final int SIGTERM = 0;
  56. public static final int GM_SHUTDOWN = 1;
  57. public static final int GM_RESTART = 2;
  58. public static final int ABORT = 3;
  59. private static final String[] MODE_TEXT = { "SIGTERM", "shutting down", "restarting", "aborting" };
  60. /**
  61. * This function starts a shutdown countdown from Telnet (Copied from Function startShutdown())
  62. *
  63. * @param ip IP Which Issued shutdown command
  64. * @param seconds seconds untill shutdown
  65. * @param restart true if the server will restart after 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. try
  197. {
  198. if ((Config.OFFLINE_TRADE_ENABLE || Config.OFFLINE_CRAFT_ENABLE) && Config.RESTORE_OFFLINERS)
  199. OfflineTradersTable.storeOffliners();
  200. }
  201. catch (Throwable t)
  202. {
  203. _log.log(Level.WARNING, "Error saving offline shops.",t);
  204. }
  205. try
  206. {
  207. disconnectAllCharacters();
  208. _log.info("All players disconnected.");
  209. }
  210. catch (Throwable t)
  211. {
  212. // ignore
  213. }
  214. // ensure all services are stopped
  215. try
  216. {
  217. GameTimeController.getInstance().stopTimer();
  218. }
  219. catch (Throwable t)
  220. {
  221. // ignore
  222. }
  223. // stop all threadpolls
  224. try
  225. {
  226. ThreadPoolManager.getInstance().shutdown();
  227. }
  228. catch (Throwable t)
  229. {
  230. // ignore
  231. }
  232. try
  233. {
  234. CommunityServerThread.getInstance().interrupt();
  235. }
  236. catch (Throwable t)
  237. {
  238. // ignore
  239. }
  240. try
  241. {
  242. LoginServerThread.getInstance().interrupt();
  243. }
  244. catch (Throwable t)
  245. {
  246. // ignore
  247. }
  248. // last byebye, save all data and quit this server
  249. saveData();
  250. // saveData sends messages to exit players, so shutdown selector after it
  251. try
  252. {
  253. GameServer.gameServer.getSelectorThread().shutdown();
  254. }
  255. catch (Throwable t)
  256. {
  257. // ignore
  258. }
  259. // commit data, last chance
  260. try
  261. {
  262. L2DatabaseFactory.getInstance().shutdown();
  263. }
  264. catch (Throwable t)
  265. {
  266. }
  267. // server will quit, when this function ends.
  268. if (SingletonHolder._instance._shutdownMode == GM_RESTART)
  269. {
  270. Runtime.getRuntime().halt(2);
  271. }
  272. else
  273. {
  274. Runtime.getRuntime().halt(0);
  275. }
  276. }
  277. else
  278. {
  279. // gm shutdown: send warnings and then call exit to start shutdown sequence
  280. countdown();
  281. // last point where logging is operational :(
  282. _log.warning("GM shutdown countdown is over. " + MODE_TEXT[_shutdownMode] + " NOW!");
  283. switch (_shutdownMode)
  284. {
  285. case GM_SHUTDOWN:
  286. SingletonHolder._instance.setMode(GM_SHUTDOWN);
  287. System.exit(0);
  288. break;
  289. case GM_RESTART:
  290. SingletonHolder._instance.setMode(GM_RESTART);
  291. System.exit(2);
  292. break;
  293. }
  294. }
  295. }
  296. /**
  297. * This functions starts a shutdown countdown
  298. *
  299. * @param activeChar GM who issued the shutdown command
  300. * @param seconds seconds until shutdown
  301. * @param restart true if the server will restart after shutdown
  302. */
  303. public void startShutdown(L2PcInstance activeChar, int seconds, boolean restart)
  304. {
  305. if (restart)
  306. {
  307. _shutdownMode = GM_RESTART;
  308. }
  309. else
  310. {
  311. _shutdownMode = GM_SHUTDOWN;
  312. }
  313. _log.warning("GM: " + activeChar.getName() + "(" + activeChar.getObjectId() + ") issued shutdown command. "
  314. + MODE_TEXT[_shutdownMode] + " in " + seconds + " seconds!");
  315. if (_shutdownMode > 0)
  316. {
  317. switch (seconds)
  318. {
  319. case 540:
  320. case 480:
  321. case 420:
  322. case 360:
  323. case 300:
  324. case 240:
  325. case 180:
  326. case 120:
  327. case 60:
  328. case 30:
  329. case 10:
  330. case 5:
  331. case 4:
  332. case 3:
  333. case 2:
  334. case 1:
  335. break;
  336. default:
  337. SendServerQuit(seconds);
  338. }
  339. }
  340. if (_counterInstance != null)
  341. {
  342. _counterInstance._abort();
  343. }
  344. // the main instance should only run for shutdown hook, so we start a new instance
  345. _counterInstance = new Shutdown(seconds, restart);
  346. _counterInstance.start();
  347. }
  348. /**
  349. * This function aborts a running countdown
  350. *
  351. * @param activeChar GM who issued the abort command
  352. */
  353. public void abort(L2PcInstance activeChar)
  354. {
  355. _log.warning("GM: " + activeChar.getName() + "(" + activeChar.getObjectId() + ") issued shutdown ABORT. "
  356. + MODE_TEXT[_shutdownMode] + " has been stopped!");
  357. if (_counterInstance != null)
  358. {
  359. _counterInstance._abort();
  360. Announcements _an = Announcements.getInstance();
  361. _an.announceToAll("Server aborts " + MODE_TEXT[_shutdownMode] + " and continues normal operation!");
  362. }
  363. }
  364. /**
  365. * set the shutdown mode
  366. * @param mode what mode shall be set
  367. */
  368. private void setMode(int mode)
  369. {
  370. _shutdownMode = mode;
  371. }
  372. /**
  373. * set shutdown mode to ABORT
  374. *
  375. */
  376. private void _abort()
  377. {
  378. _shutdownMode = ABORT;
  379. }
  380. /**
  381. * this counts the countdown and reports it to all players
  382. * countdown is aborted if mode changes to ABORT
  383. */
  384. private void countdown()
  385. {
  386. try
  387. {
  388. while (_secondsShut > 0)
  389. {
  390. switch (_secondsShut)
  391. {
  392. case 540:
  393. SendServerQuit(540);
  394. break;
  395. case 480:
  396. SendServerQuit(480);
  397. break;
  398. case 420:
  399. SendServerQuit(420);
  400. break;
  401. case 360:
  402. SendServerQuit(360);
  403. break;
  404. case 300:
  405. SendServerQuit(300);
  406. break;
  407. case 240:
  408. SendServerQuit(240);
  409. break;
  410. case 180:
  411. SendServerQuit(180);
  412. break;
  413. case 120:
  414. SendServerQuit(120);
  415. break;
  416. case 60:
  417. LoginServerThread.getInstance().setServerStatus(ServerStatus.STATUS_DOWN); //avoids new players from logging in
  418. SendServerQuit(60);
  419. break;
  420. case 30:
  421. SendServerQuit(30);
  422. break;
  423. case 10:
  424. SendServerQuit(10);
  425. break;
  426. case 5:
  427. SendServerQuit(5);
  428. break;
  429. case 4:
  430. SendServerQuit(4);
  431. break;
  432. case 3:
  433. SendServerQuit(3);
  434. break;
  435. case 2:
  436. SendServerQuit(2);
  437. break;
  438. case 1:
  439. SendServerQuit(1);
  440. break;
  441. }
  442. _secondsShut--;
  443. int delay = 1000; //milliseconds
  444. Thread.sleep(delay);
  445. if (_shutdownMode == ABORT)
  446. break;
  447. }
  448. }
  449. catch (InterruptedException e)
  450. {
  451. //this will never happen
  452. }
  453. }
  454. /**
  455. * this sends a last byebye, disconnects all players and saves data
  456. *
  457. */
  458. private void saveData()
  459. {
  460. switch (_shutdownMode)
  461. {
  462. case SIGTERM:
  463. _log.info("SIGTERM received. Shutting down NOW!");
  464. break;
  465. case GM_SHUTDOWN:
  466. _log.info("GM shutdown received. Shutting down NOW!");
  467. break;
  468. case GM_RESTART:
  469. _log.info("GM restart received. Restarting NOW!");
  470. break;
  471. }
  472. /*if (Config.ACTIVATE_POSITION_RECORDER)
  473. Universe.getInstance().implode(true);*/
  474. // Seven Signs data is now saved along with Festival data.
  475. if (!SevenSigns.getInstance().isSealValidationPeriod())
  476. SevenSignsFestival.getInstance().saveFestivalData(false);
  477. // Save Seven Signs data before closing. :)
  478. SevenSigns.getInstance().saveSevenSignsData();
  479. SevenSigns.getInstance().saveSevenSignsStatus();
  480. // Save all raidboss and GrandBoss status ^_^
  481. RaidBossSpawnManager.getInstance().cleanUp();
  482. _log.info("RaidBossSpawnManager: All raidboss info saved!!");
  483. GrandBossManager.getInstance().cleanUp();
  484. _log.info("GrandBossManager: All Grand Boss info saved!!");
  485. _log.info("TradeController saving data.. This action may take some minutes! Please wait until completed!");
  486. TradeController.getInstance().dataCountStore();
  487. _log.info("TradeController: All count Item Saved");
  488. ItemAuctionManager.getInstance().shutdown();
  489. Olympiad.getInstance().saveOlympiadStatus();
  490. _log.info("Olympiad System: Data saved!!");
  491. Hero.getInstance().shutdown();
  492. _log.info("Hero System: Data saved!!");
  493. ClanTable.getInstance().storeClanScore();
  494. _log.info("Clan System: Data saved!!");
  495. // Save Cursed Weapons data before closing.
  496. CursedWeaponsManager.getInstance().saveData();
  497. // Save all manor data
  498. CastleManorManager.getInstance().save();
  499. // Save all global (non-player specific) Quest data that needs to persist after reboot
  500. QuestManager.getInstance().save();
  501. // Save all global variables data
  502. GlobalVariablesManager.getInstance().saveVars();
  503. //Save items on ground before closing
  504. if (Config.SAVE_DROPPED_ITEM)
  505. {
  506. ItemsOnGroundManager.getInstance().saveInDb();
  507. ItemsOnGroundManager.getInstance().cleanUp();
  508. _log.info("ItemsOnGroundManager: All items on ground saved!!");
  509. }
  510. try
  511. {
  512. int delay = 5000;
  513. Thread.sleep(delay);
  514. }
  515. catch (InterruptedException e)
  516. {
  517. //never happens :p
  518. }
  519. }
  520. /**
  521. * this disconnects all clients from the server
  522. *
  523. */
  524. private void disconnectAllCharacters()
  525. {
  526. L2World.getInstance().forEachPlayer(new disconnectAllCharacters());
  527. }
  528. private final class disconnectAllCharacters implements TObjectProcedure<L2PcInstance>
  529. {
  530. @Override
  531. public final boolean execute(final L2PcInstance player)
  532. {
  533. if (player != null)
  534. {
  535. //Logout Character
  536. try
  537. {
  538. L2GameClient client = player.getClient();
  539. if (client != null && !client.isDetached())
  540. {
  541. client.close(ServerClose.STATIC_PACKET);
  542. client.setActiveChar(null);
  543. player.setClient(null);
  544. }
  545. player.deleteMe();
  546. }
  547. catch (Throwable t)
  548. {
  549. _log.log(Level.WARNING, "Failed logour char " + player, t);
  550. }
  551. }
  552. return true;
  553. }
  554. }
  555. @SuppressWarnings("synthetic-access")
  556. private static class SingletonHolder
  557. {
  558. protected static final Shutdown _instance = new Shutdown();
  559. }
  560. }