2
0

Shutdown.java 15 KB

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