added svcapi ui and camunda code
[it/otf.git] / otf-camunda / src / main / java / org / oran / otf / camunda / service / OtfWorkflowTaskCleanupService.java
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\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
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\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
15 \r
16 \r
17 package org.oran.otf.camunda.service;\r
18 \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
29 \r
30 @Component\r
31 public class OtfWorkflowTaskCleanupService {\r
32   @Autowired RuntimeService runtimeService;\r
33   public static boolean isEnabled = false;\r
34 \r
35   @EventListener(ApplicationReadyEvent.class)\r
36   public void init() {\r
37     Thread otfCleanupService = new Thread(new Worker());\r
38     otfCleanupService.start();\r
39   }\r
40 \r
41   public class Worker implements Runnable {\r
42     @Override\r
43     public void run() {\r
44       try {\r
45         while (true) {\r
46           if (isEnabled) {\r
47             synchronized (WorkflowTask.workflowTasksByExecutionId) {\r
48               Set<Entry<String, List<WorkflowTask>>> set =\r
49                   WorkflowTask.workflowTasksByExecutionId.entrySet();\r
50 \r
51               for (Entry<String, List<WorkflowTask>> entry : set) {\r
52                 String processInstanceId = entry.getKey();\r
53                 List<WorkflowTask> workflowTasks = entry.getValue();\r
54 \r
55                 ProcessInstance processInstance =\r
56                     runtimeService\r
57                         .createProcessInstanceQuery()\r
58                         .processInstanceId(processInstanceId)\r
59                         .singleResult();\r
60 \r
61                 if (processInstance == null) {\r
62                   System.out.println("Cleaning up WorkflowTasks under processInstanceId, " + processInstanceId);\r
63                   workflowTasks.forEach(WorkflowTask::shutdown);\r
64                 }\r
65               }\r
66             }\r
67           }\r
68           Thread.sleep(10000);\r
69         }\r
70       } catch (InterruptedException e) {\r
71         e.printStackTrace();\r
72       }\r
73     }\r
74   }\r
75 }\r