1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\r
3 # Licensed under the Apache License, Version 2.0 (the "License"); #
\r
4 # you may not use this file except in compliance with the License. #
\r
5 # You may obtain a copy of the License at #
\r
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\r
9 # Unless required by applicable law or agreed to in writing, software #
\r
10 # distributed under the License is distributed on an "AS IS" BASIS, #
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
\r
12 # See the License for the specific language governing permissions and #
\r
13 # limitations under the License. #
\r
14 ##############################################################################*/
\r
17 package org.oran.otf.camunda.service;
\r
19 import org.oran.otf.camunda.workflow.utility.WorkflowTask;
\r
20 import java.util.List;
\r
21 import java.util.Map.Entry;
\r
22 import java.util.Set;
\r
23 import org.camunda.bpm.engine.RuntimeService;
\r
24 import org.camunda.bpm.engine.runtime.ProcessInstance;
\r
25 import org.springframework.beans.factory.annotation.Autowired;
\r
26 import org.springframework.boot.context.event.ApplicationReadyEvent;
\r
27 import org.springframework.context.event.EventListener;
\r
28 import org.springframework.stereotype.Component;
\r
31 public class OtfWorkflowTaskCleanupService {
\r
32 @Autowired RuntimeService runtimeService;
\r
33 public static boolean isEnabled = false;
\r
35 @EventListener(ApplicationReadyEvent.class)
\r
36 public void init() {
\r
37 Thread otfCleanupService = new Thread(new Worker());
\r
38 otfCleanupService.start();
\r
41 public class Worker implements Runnable {
\r
47 synchronized (WorkflowTask.workflowTasksByExecutionId) {
\r
48 Set<Entry<String, List<WorkflowTask>>> set =
\r
49 WorkflowTask.workflowTasksByExecutionId.entrySet();
\r
51 for (Entry<String, List<WorkflowTask>> entry : set) {
\r
52 String processInstanceId = entry.getKey();
\r
53 List<WorkflowTask> workflowTasks = entry.getValue();
\r
55 ProcessInstance processInstance =
\r
57 .createProcessInstanceQuery()
\r
58 .processInstanceId(processInstanceId)
\r
61 if (processInstance == null) {
\r
62 System.out.println("Cleaning up WorkflowTasks under processInstanceId, " + processInstanceId);
\r
63 workflowTasks.forEach(WorkflowTask::shutdown);
\r
68 Thread.sleep(10000);
\r
70 } catch (InterruptedException e) {
\r
71 e.printStackTrace();
\r