Shutdown.java 14 KB

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