TaskManager.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 net.sf.l2j.gameserver.taskmanager;
  16. import static net.sf.l2j.gameserver.taskmanager.TaskTypes.TYPE_FIXED_SHEDULED;
  17. import static net.sf.l2j.gameserver.taskmanager.TaskTypes.TYPE_GLOBAL_TASK;
  18. import static net.sf.l2j.gameserver.taskmanager.TaskTypes.TYPE_NONE;
  19. import static net.sf.l2j.gameserver.taskmanager.TaskTypes.TYPE_SHEDULED;
  20. import static net.sf.l2j.gameserver.taskmanager.TaskTypes.TYPE_SPECIAL;
  21. import static net.sf.l2j.gameserver.taskmanager.TaskTypes.TYPE_STARTUP;
  22. import static net.sf.l2j.gameserver.taskmanager.TaskTypes.TYPE_TIME;
  23. import java.sql.Connection;
  24. import java.sql.PreparedStatement;
  25. import java.sql.ResultSet;
  26. import java.sql.SQLException;
  27. import java.text.DateFormat;
  28. import java.util.Calendar;
  29. import java.util.Date;
  30. import java.util.concurrent.ScheduledFuture;
  31. import java.util.logging.Logger;
  32. import javolution.util.FastList;
  33. import javolution.util.FastMap;
  34. import net.sf.l2j.L2DatabaseFactory;
  35. import net.sf.l2j.gameserver.ThreadPoolManager;
  36. import net.sf.l2j.gameserver.taskmanager.tasks.TaskCleanUp;
  37. import net.sf.l2j.gameserver.taskmanager.tasks.TaskJython;
  38. import net.sf.l2j.gameserver.taskmanager.tasks.TaskOlympiadSave;
  39. import net.sf.l2j.gameserver.taskmanager.tasks.TaskRaidPointsReset;
  40. import net.sf.l2j.gameserver.taskmanager.tasks.TaskRecom;
  41. import net.sf.l2j.gameserver.taskmanager.tasks.TaskRestart;
  42. import net.sf.l2j.gameserver.taskmanager.tasks.TaskSevenSignsUpdate;
  43. import net.sf.l2j.gameserver.taskmanager.tasks.TaskShutdown;
  44. /**
  45. * @author Layane
  46. *
  47. */
  48. public final class TaskManager
  49. {
  50. protected static final Logger _log = Logger.getLogger(TaskManager.class.getName());
  51. private static TaskManager _instance;
  52. protected static final String[] SQL_STATEMENTS =
  53. {
  54. "SELECT id,task,type,last_activation,param1,param2,param3 FROM global_tasks", "UPDATE global_tasks SET last_activation=? WHERE id=?", "SELECT id FROM global_tasks WHERE task=?",
  55. "INSERT INTO global_tasks (task,type,last_activation,param1,param2,param3) VALUES(?,?,?,?,?,?)"
  56. };
  57. private final FastMap<Integer, Task> _tasks = new FastMap<Integer, Task>();
  58. protected final FastList<ExecutedTask> _currentTasks = new FastList<ExecutedTask>();
  59. public class ExecutedTask implements Runnable
  60. {
  61. int id;
  62. long lastActivation;
  63. Task task;
  64. TaskTypes type;
  65. String[] params;
  66. ScheduledFuture<?> scheduled;
  67. public ExecutedTask(Task ptask, TaskTypes ptype, ResultSet rset) throws SQLException
  68. {
  69. task = ptask;
  70. type = ptype;
  71. id = rset.getInt("id");
  72. lastActivation = rset.getLong("last_activation");
  73. params = new String[]
  74. {
  75. rset.getString("param1"), rset.getString("param2"), rset.getString("param3")
  76. };
  77. }
  78. public void run()
  79. {
  80. task.onTimeElapsed(this);
  81. lastActivation = System.currentTimeMillis();
  82. Connection con = null;
  83. try
  84. {
  85. con = L2DatabaseFactory.getInstance().getConnection();
  86. PreparedStatement statement = con.prepareStatement(SQL_STATEMENTS[1]);
  87. statement.setLong(1, lastActivation);
  88. statement.setInt(2, id);
  89. statement.executeUpdate();
  90. statement.close();
  91. }
  92. catch (SQLException e)
  93. {
  94. _log.warning("cannot updated the Global Task " + id + ": " + e.getMessage());
  95. }
  96. finally
  97. {
  98. try
  99. {
  100. con.close();
  101. }
  102. catch (Exception e)
  103. {
  104. }
  105. }
  106. if (type == TYPE_SHEDULED || type == TYPE_TIME)
  107. {
  108. stopTask();
  109. }
  110. }
  111. @Override
  112. public boolean equals(Object object)
  113. {
  114. return id == ((ExecutedTask) object).id;
  115. }
  116. public Task getTask()
  117. {
  118. return task;
  119. }
  120. public TaskTypes getType()
  121. {
  122. return type;
  123. }
  124. public int getId()
  125. {
  126. return id;
  127. }
  128. public String[] getParams()
  129. {
  130. return params;
  131. }
  132. public long getLastActivation()
  133. {
  134. return lastActivation;
  135. }
  136. public void stopTask()
  137. {
  138. task.onDestroy();
  139. if (scheduled != null)
  140. scheduled.cancel(true);
  141. _currentTasks.remove(this);
  142. }
  143. }
  144. public static TaskManager getInstance()
  145. {
  146. if (_instance == null)
  147. {
  148. _instance = new TaskManager();
  149. }
  150. return _instance;
  151. }
  152. public TaskManager()
  153. {
  154. initializate();
  155. startAllTasks();
  156. }
  157. private void initializate()
  158. {
  159. registerTask(new TaskCleanUp());
  160. registerTask(new TaskJython());
  161. registerTask(new TaskOlympiadSave());
  162. registerTask(new TaskRaidPointsReset());
  163. registerTask(new TaskRecom());
  164. registerTask(new TaskRestart());
  165. registerTask(new TaskSevenSignsUpdate());
  166. registerTask(new TaskShutdown());
  167. }
  168. public void registerTask(Task task)
  169. {
  170. int key = task.getName().hashCode();
  171. if (!_tasks.containsKey(key))
  172. {
  173. _tasks.put(key, task);
  174. task.initializate();
  175. }
  176. }
  177. private void startAllTasks()
  178. {
  179. Connection con = null;
  180. try
  181. {
  182. con = L2DatabaseFactory.getInstance().getConnection();
  183. PreparedStatement statement = con.prepareStatement(SQL_STATEMENTS[0]);
  184. ResultSet rset = statement.executeQuery();
  185. while (rset.next())
  186. {
  187. Task task = _tasks.get(rset.getString("task").trim().toLowerCase().hashCode());
  188. if (task == null)
  189. continue;
  190. TaskTypes type = TaskTypes.valueOf(rset.getString("type"));
  191. if (type != TYPE_NONE)
  192. {
  193. ExecutedTask current = new ExecutedTask(task, type, rset);
  194. if (launchTask(current))
  195. _currentTasks.add(current);
  196. }
  197. }
  198. rset.close();
  199. statement.close();
  200. }
  201. catch (Exception e)
  202. {
  203. _log.severe("error while loading Global Task table " + e);
  204. e.printStackTrace();
  205. }
  206. finally
  207. {
  208. try
  209. {
  210. con.close();
  211. }
  212. catch (Exception e)
  213. {
  214. }
  215. }
  216. }
  217. private boolean launchTask(ExecutedTask task)
  218. {
  219. final ThreadPoolManager scheduler = ThreadPoolManager.getInstance();
  220. final TaskTypes type = task.getType();
  221. if (type == TYPE_STARTUP)
  222. {
  223. task.run();
  224. return false;
  225. }
  226. else if (type == TYPE_SHEDULED)
  227. {
  228. long delay = Long.valueOf(task.getParams()[0]);
  229. task.scheduled = scheduler.scheduleGeneral(task, delay);
  230. return true;
  231. }
  232. else if (type == TYPE_FIXED_SHEDULED)
  233. {
  234. long delay = Long.valueOf(task.getParams()[0]);
  235. long interval = Long.valueOf(task.getParams()[1]);
  236. task.scheduled = scheduler.scheduleGeneralAtFixedRate(task, delay, interval);
  237. return true;
  238. }
  239. else if (type == TYPE_TIME)
  240. {
  241. try
  242. {
  243. Date desired = DateFormat.getInstance().parse(task.getParams()[0]);
  244. long diff = desired.getTime() - System.currentTimeMillis();
  245. if (diff >= 0)
  246. {
  247. task.scheduled = scheduler.scheduleGeneral(task, diff);
  248. return true;
  249. }
  250. _log.info("Task " + task.getId() + " is obsoleted.");
  251. }
  252. catch (Exception e)
  253. {
  254. }
  255. }
  256. else if (type == TYPE_SPECIAL)
  257. {
  258. ScheduledFuture<?> result = task.getTask().launchSpecial(task);
  259. if (result != null)
  260. {
  261. task.scheduled = result;
  262. return true;
  263. }
  264. }
  265. else if (type == TYPE_GLOBAL_TASK)
  266. {
  267. long interval = Long.valueOf(task.getParams()[0]) * 86400000L;
  268. String[] hour = task.getParams()[1].split(":");
  269. if (hour.length != 3)
  270. {
  271. _log.warning("Task " + task.getId() + " has incorrect parameters");
  272. return false;
  273. }
  274. Calendar check = Calendar.getInstance();
  275. check.setTimeInMillis(task.getLastActivation() + interval);
  276. Calendar min = Calendar.getInstance();
  277. try
  278. {
  279. min.set(Calendar.HOUR_OF_DAY, Integer.valueOf(hour[0]));
  280. min.set(Calendar.MINUTE, Integer.valueOf(hour[1]));
  281. min.set(Calendar.SECOND, Integer.valueOf(hour[2]));
  282. }
  283. catch (Exception e)
  284. {
  285. _log.warning("Bad parameter on task " + task.getId() + ": " + e.getMessage());
  286. return false;
  287. }
  288. long delay = min.getTimeInMillis() - System.currentTimeMillis();
  289. if (check.after(min) || delay < 0)
  290. {
  291. delay += interval;
  292. }
  293. task.scheduled = scheduler.scheduleGeneralAtFixedRate(task, delay, interval);
  294. return true;
  295. }
  296. return false;
  297. }
  298. public static boolean addUniqueTask(String task, TaskTypes type, String param1, String param2, String param3)
  299. {
  300. return addUniqueTask(task, type, param1, param2, param3, 0);
  301. }
  302. public static boolean addUniqueTask(String task, TaskTypes type, String param1, String param2, String param3, long lastActivation)
  303. {
  304. Connection con = null;
  305. try
  306. {
  307. con = L2DatabaseFactory.getInstance().getConnection();
  308. PreparedStatement statement = con.prepareStatement(SQL_STATEMENTS[2]);
  309. statement.setString(1, task);
  310. ResultSet rset = statement.executeQuery();
  311. if (!rset.next())
  312. {
  313. statement = con.prepareStatement(SQL_STATEMENTS[3]);
  314. statement.setString(1, task);
  315. statement.setString(2, type.toString());
  316. statement.setLong(3, lastActivation);
  317. statement.setString(4, param1);
  318. statement.setString(5, param2);
  319. statement.setString(6, param3);
  320. statement.execute();
  321. }
  322. rset.close();
  323. statement.close();
  324. return true;
  325. }
  326. catch (SQLException e)
  327. {
  328. _log.warning("cannot add the unique task: " + e.getMessage());
  329. }
  330. finally
  331. {
  332. try
  333. {
  334. con.close();
  335. }
  336. catch (Exception e)
  337. {
  338. }
  339. }
  340. return false;
  341. }
  342. public static boolean addTask(String task, TaskTypes type, String param1, String param2, String param3)
  343. {
  344. return addTask(task, type, param1, param2, param3, 0);
  345. }
  346. public static boolean addTask(String task, TaskTypes type, String param1, String param2, String param3, long lastActivation)
  347. {
  348. Connection con = null;
  349. try
  350. {
  351. con = L2DatabaseFactory.getInstance().getConnection();
  352. PreparedStatement statement = con.prepareStatement(SQL_STATEMENTS[3]);
  353. statement.setString(1, task);
  354. statement.setString(2, type.toString());
  355. statement.setLong(3, lastActivation);
  356. statement.setString(4, param1);
  357. statement.setString(5, param2);
  358. statement.setString(6, param3);
  359. statement.execute();
  360. statement.close();
  361. return true;
  362. }
  363. catch (SQLException e)
  364. {
  365. _log.warning("cannot add the task: " + e.getMessage());
  366. }
  367. finally
  368. {
  369. try
  370. {
  371. con.close();
  372. }
  373. catch (Exception e)
  374. {
  375. }
  376. }
  377. return false;
  378. }
  379. }