TaskManager.java 14 KB

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