Shutdown.java 14 KB

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