001/** 002 * Copyright (C) 2014 Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/ 003 * 004 * This file is part of Dicoogle/dicoogle. 005 * 006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Dicoogle/dicoogle is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Dicoogle. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package pt.ua.dicoogle.taskManager; 020 021import java.util.HashMap; 022import java.util.Iterator; 023import java.util.Map; 024import java.util.concurrent.ExecutionException; 025 026import net.sf.json.JSONArray; 027import net.sf.json.JSONObject; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030import pt.ua.dicoogle.sdk.datastructs.IndexReport; 031import pt.ua.dicoogle.sdk.datastructs.Report; 032import pt.ua.dicoogle.sdk.task.Task; 033 034/** 035 * Singleton that contains all running index tasks 036 * 037 * @author Frederico Silva <fredericosilva@ua.pt> 038 * @author Eduardo Pinho <eduardopinho@ua.pt> 039 */ 040public class RunningIndexTasks { 041 private static final Logger logger = LoggerFactory.getLogger(RunningIndexTasks.class); 042 043 public static RunningIndexTasks instance; 044 045 private final Map<String, Task<Report>> taskRunningList; 046 047 public static RunningIndexTasks getInstance() { 048 if (instance == null) 049 instance = new RunningIndexTasks(); 050 051 return instance; 052 } 053 054 public RunningIndexTasks() { 055 taskRunningList = new HashMap<>(); 056 } 057 058 public void addTask(String taskUid, Task<Report> task) { 059 taskRunningList.put(taskUid, task); 060 } 061 062 public boolean removeTask(String taskUid) { 063 return taskRunningList.remove(taskUid) != null; 064 } 065 066 public boolean stopTask(String taskUid) { 067 Task<Report> task = taskRunningList.get(taskUid); 068 if (task != null) { 069 return task.cancel(true); 070 } else { 071 logger.info("Attempt to stop unexistent task {}, ignoring", taskUid); 072 } 073 074 return false; 075 } 076 077 public Map<String, Task<Report>> getRunningTasks() { 078 079 return taskRunningList; 080 } 081 082 public String toJson() { 083 JSONObject object = new JSONObject(); 084 JSONArray array = new JSONArray(); 085 086 int countComplete = 0; 087 int countCancelled = 0; 088 for (Map.Entry<String, Task<Report>> pair : taskRunningList.entrySet()) { 089 Task<Report> task = pair.getValue(); 090 JSONObject entry = new JSONObject(); 091 entry.put("taskUid", pair.getKey()); 092 entry.put("taskName", task.getName()); 093 entry.put("taskProgress", task.getProgress()); 094 095 if (task.isDone() && !task.isCancelled()) { 096 entry.put("complete", true); 097 countComplete += 1; 098 try { 099 Report r = task.get(); 100 if (r instanceof IndexReport) { 101 entry.put("elapsedTime", ((IndexReport)r).getElapsedTime()); 102 entry.put("nIndexed", ((IndexReport)r).getNIndexed()); 103 entry.put("nErrors", ((IndexReport)r).getNErrors()); 104 } 105 } catch (InterruptedException | ExecutionException ex) { 106 logger.warn("Could not retrieve task result, ignoring", ex); 107 } 108 } 109 if (task.isCancelled()) { 110 countCancelled += 1; 111 entry.put("canceled", true); 112 } 113 array.add(entry); 114 } 115 116 object.put("results", array); 117 object.put("count", array.size() - countComplete - countCancelled); 118 return object.toString(); 119 } 120}