private void regenerateTasks(int round) throws SQLException { Connection connection = DatabaseManager.CreateConnection(); try { FlagManager flagManager = new FlagManager(connection); for (Service service : services) { int serviceId = service.getId(); List<Task> tasks = newTasksAll.get(serviceId); synchronized (tasks) { for (Team team : teams) { int teamId = team.getId(); IdFlagPair newIdFlag = new IdFlagPair(flagManager.CreateId(), flagManager.CreateFlag()); IdFlagPair randomIdFlag = flagManager.GetRandomAliveFlag(teamId, serviceId); Task task = new Task(serviceId, round, team, newIdFlag, randomIdFlag); tasks.add(task); } } } } finally { connection.close(); } }
// TODO: убрать ненужные newFlagSuccess и randomFlagSuccess, смотреть на failStage (переделанный в // Enum) public void finishTask( UUID taskId, String newFlagId, int status, String failStage, String failComment, boolean newFlagSuccess, boolean randomFlagSuccess) throws SQLException { Task task = null; for (int serviceId : processingTasksAll.keySet()) { // Может, стоит по GUID сразу получать task? HashMap<UUID, Task> tasks = processingTasksAll.get(serviceId); synchronized (tasks) { if (tasks.containsKey(taskId)) { task = tasks.get(taskId); break; } } } if (task == null) // Если демон опоздал { logger.warn(String.format("Task with id %s not found in processingTasks", taskId)); return; } task.newIdFlag = new IdFlagPair(newFlagId, task.newIdFlag.getFlagData()); task.status = status; Connection connection = DatabaseManager.CreateConnection(); connection.setAutoCommit(false); try { FlagManager flagManager = new FlagManager(connection); AccessChecksInserter dbAccessChecks = new AccessChecksInserter(connection); if (newFlagSuccess) { flagManager.InsertFlag( task.team.getId(), task.serviceId, task.newIdFlag.getFlagId(), task.newIdFlag.getFlagData()); } int accessScore = 0; if (status == CheckerExitCode.OK.toInt()) accessScore = 1; // TODO: Писать в базу не строку failStage, а Enum dbAccessChecks.Insert( task.team.getId(), task.serviceId, status, failStage, failComment, accessScore, task.id); synchronized (processingTasksAll.get(task.serviceId)) { processingTasksAll.get(task.serviceId).remove(task.id); } synchronized (doneTasksAll.get(task.serviceId)) { doneTasksAll.get(task.serviceId).add(task); } connection.commit(); } catch (SQLException e) { connection.rollback(); throw e; } finally { connection.close(); } }