Shutdown.java 14 KB

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