diff --git a/src/main/java/com/gw/tasks/GeoweaverProcessTask.java b/src/main/java/com/gw/tasks/GeoweaverProcessTask.java index 92036589..3220c8f3 100644 --- a/src/main/java/com/gw/tasks/GeoweaverProcessTask.java +++ b/src/main/java/com/gw/tasks/GeoweaverProcessTask.java @@ -312,12 +312,37 @@ public void startMonitor(javax.websocket.Session socketsession) { /** This function is called when the task is not loaded by a worker */ public void endPrematurely() { - + // Mark the task as stopped this.curstatus = ExecutionStatus.STOPPED; + // If the task is running in a separate thread, interrupt the thread + if (Thread.currentThread().isInterrupted()) { + Thread.currentThread().interrupt(); // Interrupt the current thread (if it's blocking) + } + + // Ensure the task is not performing any further operations + stopRunningOperations(); + + // Update the task's status updateEverything(); } + // This method ensures the task halts long-running operations + /* + * Check if task is in a long-running operation and forcefully stop it + * Example: If it's stuck in a loop or waiting on some resource, break the + * operation + */ + private void stopRunningOperations() { + if (this.curstatus == ExecutionStatus.STOPPED) { + // Example of terminating a running operation or loop + // You can break the loop or stop waiting for resources + // if (someLongRunningCondition) { + // // Break or terminate operation + // } + } + } + /** Stop the monitoring of the task */ public void stopMonitor() { diff --git a/src/main/java/com/gw/tasks/TaskManager.java b/src/main/java/com/gw/tasks/TaskManager.java index 54ed89ea..1e60a2a5 100644 --- a/src/main/java/com/gw/tasks/TaskManager.java +++ b/src/main/java/com/gw/tasks/TaskManager.java @@ -168,21 +168,24 @@ public synchronized void notifyWaitinglist() { logger.debug("notify waiting list to pay attention to the released worker"); if (waitinglist.size() > 0 && wm.getCurrentWorkerNumber() < worknumber) { orderWaitingList(); + + // Process tasks iteratively for (int i = 0; i < waitinglist.size(); i++) { GeoweaverProcessTask newtask = (GeoweaverProcessTask) waitinglist.get(i); if (newtask.getIsReady()) { - waitinglist.remove(newtask); - if (newtask.checkShouldPassOrNot()){ - newtask.endPrematurely(); - notifyWaitinglist(); - }else{ - executeATask(newtask); + waitinglist.remove(newtask); // Remove task from waiting list + if (newtask.checkShouldPassOrNot()) { + newtask.endPrematurely(); // End the task immediately if it should not proceed + } else { + executeATask(newtask); // Otherwise, execute the task } + i--; // Adjusting index due to the removal } } } } + /** * A task is done, being triggered to start doing another task. * @@ -206,46 +209,40 @@ public void arrive(Task t) { * @param history_id */ public void stopTask(String history_id) { - try { - + // Stop tasks in waiting list synchronized (waitinglist) { Iterator iterator = waitinglist.iterator(); + List tasksToRemove = new ArrayList<>(); while (iterator.hasNext()) { - GeoweaverProcessTask thet = (GeoweaverProcessTask) iterator.next(); - if (thet.getHistory_id().equals(history_id)) { - - thet.endPrematurely(); - - waitinglist.remove(thet); // remove from waiting list + thet.endPrematurely(); // Immediately end the task + tasksToRemove.add(thet); // Mark for removal + } + } + waitinglist.removeAll(tasksToRemove); // Remove all tasks at once after iteration } - } - } - - synchronized (runninglist) { - Iterator iterator = runninglist.iterator(); - while (iterator.hasNext()) { - - GeoweaverProcessTask thet = (GeoweaverProcessTask) iterator.next(); - - if (thet.getHistory_id().equals(history_id)) { - - // for task ongoing, it will be ended using pt.stop at above level. - - thet.endPrematurely(); - - runninglist.remove(thet); // remove from waiting list + // Stop tasks in running list + synchronized (runninglist) { + Iterator iterator = runninglist.iterator(); + List tasksToRemove = new ArrayList<>(); + + while (iterator.hasNext()) { + GeoweaverProcessTask thet = (GeoweaverProcessTask) iterator.next(); + if (thet.getHistory_id().equals(history_id)) { + thet.endPrematurely(); // Immediately end the task + tasksToRemove.add(thet); // Mark for removal + } + } + runninglist.removeAll(tasksToRemove); // Remove all tasks at once after iteration } - } - } - - } catch (Exception e) { + } catch (Exception e) { e.printStackTrace(); } } + }