From 1b1397815a4beed04db81b1fd35e115b5bd1206d Mon Sep 17 00:00:00 2001 From: Arunan Date: Fri, 15 Mar 2024 16:24:56 +0530 Subject: [PATCH] Improve NTaskTaskManager to remove pending tasks during server startup Resolves https://github.com/wso2/api-manager/issues/2525 When a task is deleted before the task manager is initialized, the task is not cleared from the pending queueList. As a result, when the Task Manager is initialized the task is scheduled from the pending queueList. --- .../mediation/ntask/NTaskTaskManager.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/components/mediation/tasks/org.wso2.micro.integrator.mediation.ntask/src/main/java/org/wso2/micro/integrator/mediation/ntask/NTaskTaskManager.java b/components/mediation/tasks/org.wso2.micro.integrator.mediation.ntask/src/main/java/org/wso2/micro/integrator/mediation/ntask/NTaskTaskManager.java index a32ad004fe..ec879d84ef 100644 --- a/components/mediation/tasks/org.wso2.micro.integrator.mediation.ntask/src/main/java/org/wso2/micro/integrator/mediation/ntask/NTaskTaskManager.java +++ b/components/mediation/tasks/org.wso2.micro.integrator.mediation.ntask/src/main/java/org/wso2/micro/integrator/mediation/ntask/NTaskTaskManager.java @@ -146,9 +146,6 @@ public boolean reschedule(String taskName, TaskDescription taskDescription) { @Override public boolean delete(String taskName) { - if (!isInitialized()) { - return false; - } if (taskName == null) { return false; } @@ -172,6 +169,10 @@ public boolean delete(String taskName) { try { boolean deleted; synchronized (lock) { + if (!isInitialized()) { + checkAndDeleteTaskFromQueue(name, group, taskName); + return false; + } if (taskManager == null) { logger.warn("#delete Could not delete task [" + taskName + "]. Task manager is not available."); return false; @@ -714,5 +715,18 @@ public void run() { }).start(); } -} + private void checkAndDeleteTaskFromQueue(String name, String group, String taskName) { + synchronized (taskQueueLock) { + Iterator iterator = taskQueue.iterator(); + while (iterator.hasNext()) { + TaskDescription taskDescription = iterator.next(); + if (taskDescription.getName().equals(name) && taskDescription.getTaskGroup().equals(group)) { + iterator.remove(); + NTaskAdapter.removeProperty(taskName); + logger.info("Deleted queued task [" + name + "::" + group + "]."); + } + } + } + } +}