--- /dev/null
+/* Copyright (c) 2019 AT&T Intellectual Property. #\r
+# #\r
+# Licensed under the Apache License, Version 2.0 (the "License"); #\r
+# you may not use this file except in compliance with the License. #\r
+# You may obtain a copy of the License at #\r
+# #\r
+# http://www.apache.org/licenses/LICENSE-2.0 #\r
+# #\r
+# Unless required by applicable law or agreed to in writing, software #\r
+# distributed under the License is distributed on an "AS IS" BASIS, #\r
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #\r
+# See the License for the specific language governing permissions and #\r
+# limitations under the License. #\r
+##############################################################################*/\r
+\r
+\r
+package org.oran.otf.camunda.service;\r
+\r
+import org.oran.otf.camunda.workflow.utility.WorkflowTask;\r
+import java.util.List;\r
+import java.util.Map.Entry;\r
+import java.util.Set;\r
+import org.camunda.bpm.engine.RuntimeService;\r
+import org.camunda.bpm.engine.runtime.ProcessInstance;\r
+import org.springframework.beans.factory.annotation.Autowired;\r
+import org.springframework.boot.context.event.ApplicationReadyEvent;\r
+import org.springframework.context.event.EventListener;\r
+import org.springframework.stereotype.Component;\r
+\r
+@Component\r
+public class OtfWorkflowTaskCleanupService {\r
+ @Autowired RuntimeService runtimeService;\r
+ public static boolean isEnabled = false;\r
+\r
+ @EventListener(ApplicationReadyEvent.class)\r
+ public void init() {\r
+ Thread otfCleanupService = new Thread(new Worker());\r
+ otfCleanupService.start();\r
+ }\r
+\r
+ public class Worker implements Runnable {\r
+ @Override\r
+ public void run() {\r
+ try {\r
+ while (true) {\r
+ if (isEnabled) {\r
+ synchronized (WorkflowTask.workflowTasksByExecutionId) {\r
+ Set<Entry<String, List<WorkflowTask>>> set =\r
+ WorkflowTask.workflowTasksByExecutionId.entrySet();\r
+\r
+ for (Entry<String, List<WorkflowTask>> entry : set) {\r
+ String processInstanceId = entry.getKey();\r
+ List<WorkflowTask> workflowTasks = entry.getValue();\r
+\r
+ ProcessInstance processInstance =\r
+ runtimeService\r
+ .createProcessInstanceQuery()\r
+ .processInstanceId(processInstanceId)\r
+ .singleResult();\r
+\r
+ if (processInstance == null) {\r
+ System.out.println("Cleaning up WorkflowTasks under processInstanceId, " + processInstanceId);\r
+ workflowTasks.forEach(WorkflowTask::shutdown);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ Thread.sleep(10000);\r
+ }\r
+ } catch (InterruptedException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ }\r
+}\r