TaskManager.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. {
  52. "SELECT id,task,type,last_activation,param1,param2,param3 FROM global_tasks",
  53. "UPDATE global_tasks SET last_activation=? WHERE id=?",
  54. "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[] { rset.getString("param1"), rset.getString("param2"), rset.getString("param3") };
  74. }
  75. public void run()
  76. {
  77. task.onTimeElapsed(this);
  78. lastActivation = System.currentTimeMillis();
  79. java.sql.Connection con = null;
  80. try
  81. {
  82. con = L2DatabaseFactory.getInstance().getConnection();
  83. PreparedStatement statement = con.prepareStatement(SQL_STATEMENTS[1]);
  84. statement.setLong(1, lastActivation);
  85. statement.setInt(2, id);
  86. statement.executeUpdate();
  87. statement.close();
  88. }
  89. catch (SQLException e)
  90. {
  91. _log.warning("cannot updated the Global Task " + id + ": "
  92. + e.getMessage());
  93. }
  94. finally
  95. {
  96. try
  97. {
  98. con.close();
  99. }
  100. catch (Exception e)
  101. {
  102. }
  103. }
  104. if (type == TYPE_SHEDULED || type == TYPE_TIME)
  105. {
  106. stopTask();
  107. }
  108. }
  109. @Override
  110. public boolean equals(Object object)
  111. {
  112. return id == ((ExecutedTask) object).id;
  113. }
  114. public Task getTask()
  115. {
  116. return task;
  117. }
  118. public TaskTypes getType()
  119. {
  120. return type;
  121. }
  122. public int getId()
  123. {
  124. return id;
  125. }
  126. public String[] getParams()
  127. {
  128. return params;
  129. }
  130. public long getLastActivation()
  131. {
  132. return lastActivation;
  133. }
  134. public void stopTask()
  135. {
  136. task.onDestroy();
  137. if (scheduled != null)
  138. scheduled.cancel(true);
  139. _currentTasks.remove(this);
  140. }
  141. }
  142. public static TaskManager getInstance()
  143. {
  144. if (_instance == null)
  145. {
  146. _instance = new TaskManager();
  147. }
  148. return _instance;
  149. }
  150. public TaskManager()
  151. {
  152. initializate();
  153. startAllTasks();
  154. }
  155. private void initializate()
  156. {
  157. registerTask(new TaskCleanUp());
  158. registerTask(new TaskJython());
  159. registerTask(new TaskOlympiadSave());
  160. registerTask(new TaskRecom());
  161. registerTask(new TaskRestart());
  162. registerTask(new TaskSevenSignsUpdate());
  163. registerTask(new TaskShutdown());
  164. }
  165. public void registerTask(Task task)
  166. {
  167. int key = task.getName().hashCode();
  168. if (!_tasks.containsKey(key))
  169. {
  170. _tasks.put(key, task);
  171. task.initializate();
  172. }
  173. }
  174. private void startAllTasks()
  175. {
  176. java.sql.Connection con = null;
  177. try
  178. {
  179. try
  180. {
  181. con = L2DatabaseFactory.getInstance().getConnection();
  182. PreparedStatement statement = con.prepareStatement(SQL_STATEMENTS[0]);
  183. ResultSet rset = statement.executeQuery();
  184. while (rset.next())
  185. {
  186. Task task = _tasks.get(rset.getString("task").trim().toLowerCase().hashCode());
  187. if (task == null)
  188. continue;
  189. TaskTypes type = TaskTypes.valueOf(rset.getString("type"));
  190. if (type != TYPE_NONE)
  191. {
  192. ExecutedTask current = new ExecutedTask(task, type, rset);
  193. if (launchTask(current))
  194. _currentTasks.add(current);
  195. }
  196. }
  197. rset.close();
  198. statement.close();
  199. }
  200. catch (Exception e)
  201. {
  202. _log.severe("error while loading Global Task table " + e);
  203. e.printStackTrace();
  204. }
  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()
  272. + " has incorrect parameters");
  273. return false;
  274. }
  275. Calendar check = Calendar.getInstance();
  276. check.setTimeInMillis(task.getLastActivation() + interval);
  277. Calendar min = Calendar.getInstance();
  278. try
  279. {
  280. min.set(Calendar.HOUR_OF_DAY, Integer.valueOf(hour[0]));
  281. min.set(Calendar.MINUTE, Integer.valueOf(hour[1]));
  282. min.set(Calendar.SECOND, Integer.valueOf(hour[2]));
  283. }
  284. catch (Exception e)
  285. {
  286. _log.warning("Bad parameter on task " + task.getId() + ": "
  287. + e.getMessage());
  288. return false;
  289. }
  290. long delay = min.getTimeInMillis() - System.currentTimeMillis();
  291. if (check.after(min) || delay < 0)
  292. {
  293. delay += interval;
  294. }
  295. task.scheduled = scheduler.scheduleGeneralAtFixedRate(task, delay, interval);
  296. return true;
  297. }
  298. return false;
  299. }
  300. public static boolean addUniqueTask(String task, TaskTypes type, String param1, String param2, String param3)
  301. {
  302. return addUniqueTask(task, type, param1, param2, param3, 0);
  303. }
  304. public static boolean addUniqueTask(String task, TaskTypes type, String param1, String param2, String param3, long lastActivation)
  305. {
  306. java.sql.Connection con = null;
  307. try
  308. {
  309. con = L2DatabaseFactory.getInstance().getConnection();
  310. PreparedStatement statement = con.prepareStatement(SQL_STATEMENTS[2]);
  311. statement.setString(1, task);
  312. ResultSet rset = statement.executeQuery();
  313. if (!rset.next())
  314. {
  315. statement = con.prepareStatement(SQL_STATEMENTS[3]);
  316. statement.setString(1, task);
  317. statement.setString(2, type.toString());
  318. statement.setLong(3, lastActivation);
  319. statement.setString(4, param1);
  320. statement.setString(5, param2);
  321. statement.setString(6, param3);
  322. statement.execute();
  323. }
  324. rset.close();
  325. statement.close();
  326. return true;
  327. }
  328. catch (SQLException e)
  329. {
  330. _log.warning("cannot add the unique task: " + e.getMessage());
  331. }
  332. finally
  333. {
  334. try
  335. {
  336. con.close();
  337. }
  338. catch (Exception e)
  339. {
  340. }
  341. }
  342. return false;
  343. }
  344. public static boolean addTask(String task, TaskTypes type, String param1, String param2, String param3)
  345. {
  346. return addTask(task, type, param1, param2, param3, 0);
  347. }
  348. public static boolean addTask(String task, TaskTypes type, String param1, String param2, String param3, long lastActivation)
  349. {
  350. java.sql.Connection con = null;
  351. try
  352. {
  353. con = L2DatabaseFactory.getInstance().getConnection();
  354. PreparedStatement statement = con.prepareStatement(SQL_STATEMENTS[3]);
  355. statement.setString(1, task);
  356. statement.setString(2, type.toString());
  357. statement.setLong(3, lastActivation);
  358. statement.setString(4, param1);
  359. statement.setString(5, param2);
  360. statement.setString(6, param3);
  361. statement.execute();
  362. statement.close();
  363. return true;
  364. }
  365. catch (SQLException e)
  366. {
  367. _log.warning("cannot add the task: " + e.getMessage());
  368. }
  369. finally
  370. {
  371. try
  372. {
  373. con.close();
  374. }
  375. catch (Exception e)
  376. {
  377. }
  378. }
  379. return false;
  380. }
  381. }