TaskManager.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.taskmanager;
  20. import static com.l2jserver.gameserver.taskmanager.TaskTypes.TYPE_NONE;
  21. import static com.l2jserver.gameserver.taskmanager.TaskTypes.TYPE_SHEDULED;
  22. import static com.l2jserver.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.List;
  31. import java.util.Map;
  32. import java.util.concurrent.ConcurrentHashMap;
  33. import java.util.concurrent.CopyOnWriteArrayList;
  34. import java.util.concurrent.ScheduledFuture;
  35. import java.util.logging.Level;
  36. import java.util.logging.Logger;
  37. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  38. import com.l2jserver.gameserver.ThreadPoolManager;
  39. import com.l2jserver.gameserver.taskmanager.tasks.TaskBirthday;
  40. import com.l2jserver.gameserver.taskmanager.tasks.TaskClanLeaderApply;
  41. import com.l2jserver.gameserver.taskmanager.tasks.TaskCleanUp;
  42. import com.l2jserver.gameserver.taskmanager.tasks.TaskDailySkillReuseClean;
  43. import com.l2jserver.gameserver.taskmanager.tasks.TaskGlobalVariablesSave;
  44. import com.l2jserver.gameserver.taskmanager.tasks.TaskJython;
  45. import com.l2jserver.gameserver.taskmanager.tasks.TaskOlympiadSave;
  46. import com.l2jserver.gameserver.taskmanager.tasks.TaskRaidPointsReset;
  47. import com.l2jserver.gameserver.taskmanager.tasks.TaskRecom;
  48. import com.l2jserver.gameserver.taskmanager.tasks.TaskRestart;
  49. import com.l2jserver.gameserver.taskmanager.tasks.TaskScript;
  50. import com.l2jserver.gameserver.taskmanager.tasks.TaskSevenSignsUpdate;
  51. import com.l2jserver.gameserver.taskmanager.tasks.TaskShutdown;
  52. /**
  53. * @author Layane
  54. */
  55. public final class TaskManager
  56. {
  57. protected static final Logger _log = Logger.getLogger(TaskManager.class.getName());
  58. private final Map<Integer, Task> _tasks = new ConcurrentHashMap<>();
  59. protected final List<ExecutedTask> _currentTasks = new CopyOnWriteArrayList<>();
  60. protected static final String[] SQL_STATEMENTS =
  61. {
  62. "SELECT id,task,type,last_activation,param1,param2,param3 FROM global_tasks",
  63. "UPDATE global_tasks SET last_activation=? WHERE id=?",
  64. "SELECT id FROM global_tasks WHERE task=?",
  65. "INSERT INTO global_tasks (task,type,last_activation,param1,param2,param3) VALUES(?,?,?,?,?,?)"
  66. };
  67. protected TaskManager()
  68. {
  69. initializate();
  70. startAllTasks();
  71. _log.log(Level.INFO, getClass().getSimpleName() + ": Loaded: " + _tasks.size() + " Tasks");
  72. }
  73. public class ExecutedTask implements Runnable
  74. {
  75. int id;
  76. long lastActivation;
  77. Task task;
  78. TaskTypes type;
  79. String[] params;
  80. ScheduledFuture<?> scheduled;
  81. public ExecutedTask(Task ptask, TaskTypes ptype, ResultSet rset) throws SQLException
  82. {
  83. task = ptask;
  84. type = ptype;
  85. id = rset.getInt("id");
  86. lastActivation = rset.getLong("last_activation");
  87. params = new String[]
  88. {
  89. rset.getString("param1"),
  90. rset.getString("param2"),
  91. rset.getString("param3")
  92. };
  93. }
  94. @Override
  95. public void run()
  96. {
  97. task.onTimeElapsed(this);
  98. lastActivation = System.currentTimeMillis();
  99. try (Connection con = ConnectionFactory.getInstance().getConnection();
  100. PreparedStatement statement = con.prepareStatement(SQL_STATEMENTS[1]))
  101. {
  102. statement.setLong(1, lastActivation);
  103. statement.setInt(2, id);
  104. statement.executeUpdate();
  105. }
  106. catch (SQLException e)
  107. {
  108. _log.log(Level.WARNING, getClass().getSimpleName() + ": Cannot updated the Global Task " + id + ": " + e.getMessage(), e);
  109. }
  110. if ((type == TYPE_SHEDULED) || (type == TYPE_TIME))
  111. {
  112. stopTask();
  113. }
  114. }
  115. @Override
  116. public boolean equals(Object object)
  117. {
  118. if (this == object)
  119. {
  120. return true;
  121. }
  122. if (!(object instanceof ExecutedTask))
  123. {
  124. return false;
  125. }
  126. return id == ((ExecutedTask) object).id;
  127. }
  128. @Override
  129. public int hashCode()
  130. {
  131. return id;
  132. }
  133. public Task getTask()
  134. {
  135. return task;
  136. }
  137. public TaskTypes getType()
  138. {
  139. return type;
  140. }
  141. public int getId()
  142. {
  143. return id;
  144. }
  145. public String[] getParams()
  146. {
  147. return params;
  148. }
  149. public long getLastActivation()
  150. {
  151. return lastActivation;
  152. }
  153. public void stopTask()
  154. {
  155. task.onDestroy();
  156. if (scheduled != null)
  157. {
  158. scheduled.cancel(true);
  159. }
  160. _currentTasks.remove(this);
  161. }
  162. }
  163. private void initializate()
  164. {
  165. registerTask(new TaskBirthday());
  166. registerTask(new TaskClanLeaderApply());
  167. registerTask(new TaskCleanUp());
  168. registerTask(new TaskDailySkillReuseClean());
  169. registerTask(new TaskGlobalVariablesSave());
  170. registerTask(new TaskJython());
  171. registerTask(new TaskOlympiadSave());
  172. registerTask(new TaskRaidPointsReset());
  173. registerTask(new TaskRecom());
  174. registerTask(new TaskRestart());
  175. registerTask(new TaskScript());
  176. registerTask(new TaskSevenSignsUpdate());
  177. registerTask(new TaskShutdown());
  178. }
  179. public void registerTask(Task task)
  180. {
  181. int key = task.getName().hashCode();
  182. _tasks.computeIfAbsent(key, k ->
  183. {
  184. task.initializate();
  185. return task;
  186. });
  187. }
  188. private void startAllTasks()
  189. {
  190. try (Connection con = ConnectionFactory.getInstance().getConnection();
  191. PreparedStatement statement = con.prepareStatement(SQL_STATEMENTS[0]);
  192. ResultSet rset = statement.executeQuery())
  193. {
  194. while (rset.next())
  195. {
  196. Task task = _tasks.get(rset.getString("task").trim().toLowerCase().hashCode());
  197. if (task == null)
  198. {
  199. continue;
  200. }
  201. final TaskTypes type = TaskTypes.valueOf(rset.getString("type"));
  202. if (type != TYPE_NONE)
  203. {
  204. ExecutedTask current = new ExecutedTask(task, type, rset);
  205. if (launchTask(current))
  206. {
  207. _currentTasks.add(current);
  208. }
  209. }
  210. }
  211. }
  212. catch (Exception e)
  213. {
  214. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error while loading Global Task table: " + e.getMessage(), e);
  215. }
  216. }
  217. private boolean launchTask(ExecutedTask task)
  218. {
  219. final ThreadPoolManager scheduler = ThreadPoolManager.getInstance();
  220. final TaskTypes type = task.getType();
  221. long delay, interval;
  222. switch (type)
  223. {
  224. case TYPE_STARTUP:
  225. task.run();
  226. return false;
  227. case TYPE_SHEDULED:
  228. delay = Long.valueOf(task.getParams()[0]);
  229. task.scheduled = scheduler.scheduleGeneral(task, delay);
  230. return true;
  231. case TYPE_FIXED_SHEDULED:
  232. delay = Long.valueOf(task.getParams()[0]);
  233. interval = Long.valueOf(task.getParams()[1]);
  234. task.scheduled = scheduler.scheduleGeneralAtFixedRate(task, delay, interval);
  235. return true;
  236. case TYPE_TIME:
  237. try
  238. {
  239. Date desired = DateFormat.getInstance().parse(task.getParams()[0]);
  240. long diff = desired.getTime() - System.currentTimeMillis();
  241. if (diff >= 0)
  242. {
  243. task.scheduled = scheduler.scheduleGeneral(task, diff);
  244. return true;
  245. }
  246. _log.info(getClass().getSimpleName() + ": Task " + task.getId() + " is obsoleted.");
  247. }
  248. catch (Exception e)
  249. {
  250. }
  251. break;
  252. case TYPE_SPECIAL:
  253. ScheduledFuture<?> result = task.getTask().launchSpecial(task);
  254. if (result != null)
  255. {
  256. task.scheduled = result;
  257. return true;
  258. }
  259. break;
  260. case TYPE_GLOBAL_TASK:
  261. interval = Long.valueOf(task.getParams()[0]) * 86400000L;
  262. String[] hour = task.getParams()[1].split(":");
  263. if (hour.length != 3)
  264. {
  265. _log.warning(getClass().getSimpleName() + ": Task " + task.getId() + " has incorrect parameters");
  266. return false;
  267. }
  268. Calendar check = Calendar.getInstance();
  269. check.setTimeInMillis(task.getLastActivation() + interval);
  270. Calendar min = Calendar.getInstance();
  271. try
  272. {
  273. min.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour[0]));
  274. min.set(Calendar.MINUTE, Integer.parseInt(hour[1]));
  275. min.set(Calendar.SECOND, Integer.parseInt(hour[2]));
  276. }
  277. catch (Exception e)
  278. {
  279. _log.log(Level.WARNING, getClass().getSimpleName() + ": Bad parameter on task " + task.getId() + ": " + e.getMessage(), e);
  280. return false;
  281. }
  282. delay = min.getTimeInMillis() - System.currentTimeMillis();
  283. if (check.after(min) || (delay < 0))
  284. {
  285. delay += interval;
  286. }
  287. task.scheduled = scheduler.scheduleGeneralAtFixedRate(task, delay, interval);
  288. return true;
  289. default:
  290. return false;
  291. }
  292. return false;
  293. }
  294. public static boolean addUniqueTask(String task, TaskTypes type, String param1, String param2, 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, String param3, long lastActivation)
  299. {
  300. try (Connection con = ConnectionFactory.getInstance().getConnection();
  301. PreparedStatement ps1 = con.prepareStatement(SQL_STATEMENTS[2]))
  302. {
  303. ps1.setString(1, task);
  304. try (ResultSet rs = ps1.executeQuery())
  305. {
  306. if (!rs.next())
  307. {
  308. try (PreparedStatement ps2 = con.prepareStatement(SQL_STATEMENTS[3]))
  309. {
  310. ps2.setString(1, task);
  311. ps2.setString(2, type.toString());
  312. ps2.setLong(3, lastActivation);
  313. ps2.setString(4, param1);
  314. ps2.setString(5, param2);
  315. ps2.setString(6, param3);
  316. ps2.execute();
  317. }
  318. }
  319. }
  320. return true;
  321. }
  322. catch (SQLException e)
  323. {
  324. _log.log(Level.WARNING, TaskManager.class.getSimpleName() + ": Cannot add the unique task: " + e.getMessage(), e);
  325. }
  326. return false;
  327. }
  328. public static boolean addTask(String task, TaskTypes type, String param1, String param2, String param3)
  329. {
  330. return addTask(task, type, param1, param2, param3, 0);
  331. }
  332. public static boolean addTask(String task, TaskTypes type, String param1, String param2, String param3, long lastActivation)
  333. {
  334. try (Connection con = ConnectionFactory.getInstance().getConnection();
  335. PreparedStatement statement = con.prepareStatement(SQL_STATEMENTS[3]))
  336. {
  337. statement.setString(1, task);
  338. statement.setString(2, type.toString());
  339. statement.setLong(3, lastActivation);
  340. statement.setString(4, param1);
  341. statement.setString(5, param2);
  342. statement.setString(6, param3);
  343. statement.execute();
  344. return true;
  345. }
  346. catch (SQLException e)
  347. {
  348. _log.log(Level.WARNING, TaskManager.class.getSimpleName() + ": Cannot add the task: " + e.getMessage(), e);
  349. }
  350. return false;
  351. }
  352. public static TaskManager getInstance()
  353. {
  354. return SingletonHolder._instance;
  355. }
  356. private static class SingletonHolder
  357. {
  358. protected static final TaskManager _instance = new TaskManager();
  359. }
  360. }