Shutdown.java 14 KB

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