--- /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.service.impl;\r
+\r
+import org.oran.otf.camunda.configuration.OtfCamundaConfiguration;\r
+import org.oran.otf.camunda.service.CamundaShutdown;\r
+import org.oran.otf.camunda.service.OtfExternalTaskService;\r
+import org.oran.otf.camunda.service.OtfWorkflowTaskCleanupService;\r
+import org.oran.otf.camunda.workflow.utility.WorkflowTask;\r
+import org.oran.otf.common.utility.http.ResponseUtility;\r
+import org.oran.otf.service.DeveloperService;\r
+import com.google.common.base.Strings;\r
+\r
+import java.util.Arrays;\r
+import java.util.Set;\r
+\r
+import javax.servlet.http.HttpServletRequest;\r
+import javax.ws.rs.core.Response;\r
+\r
+import org.springframework.beans.factory.annotation.Autowired;\r
+import org.springframework.beans.factory.annotation.Value;\r
+import org.springframework.context.event.ContextClosedEvent;\r
+import org.springframework.context.event.EventListener;\r
+\r
+import org.camunda.bpm.BpmPlatform;\r
+import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;\r
+import org.camunda.bpm.engine.impl.jobexecutor.JobExecutor;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.springframework.stereotype.Service;\r
+\r
+@Service\r
+public class DeveloperServiceImpl implements DeveloperService {\r
+ \r
+ private Logger logger = LoggerFactory.getLogger(DeveloperServiceImpl.class);\r
+ \r
+ @Autowired\r
+ CamundaShutdown camundaShutdown;\r
+ \r
+ @Value("${otf.camunda.graceful-shutdown.wait-time}")\r
+ private int gracefulWaitTime;\r
+\r
+ private boolean gracefulShutdown = true;\r
+\r
+ @Override\r
+ public Response workflowTaskCleanup(String enabled) {\r
+ if (Strings.isNullOrEmpty(enabled))\r
+ return ResponseUtility.Build.badRequestWithMessage(\r
+ "Path parameter, enabled, cannot be null or empty.");\r
+\r
+ OtfWorkflowTaskCleanupService.isEnabled = enabled.equalsIgnoreCase("true");\r
+ return ResponseUtility.Build.okRequestWithMessage(\r
+ "Clean up service set to " + OtfWorkflowTaskCleanupService.isEnabled);\r
+ }\r
+\r
+ @Override\r
+ public Response externalTaskWorker(String enabled) {\r
+ if (Strings.isNullOrEmpty(enabled)) {\r
+ return ResponseUtility.Build.badRequestWithMessage(\r
+ "Path parameter, enabled, cannot be null or empty.");\r
+ }\r
+\r
+ OtfExternalTaskService.isEnabled = enabled.equalsIgnoreCase("true");\r
+ return ResponseUtility.Build.okRequestWithMessage(\r
+ "OTF External Task set to " + OtfExternalTaskService.isEnabled);\r
+ }\r
+\r
+ @Override\r
+ public Response printThreads(HttpServletRequest request) {\r
+ //Logger logger = LoggerFactory.getLogger(HealthServiceImpl.class);\r
+ String message = String.format("Health request from %s.", request.getRemoteAddr());\r
+ logger.info(message);\r
+\r
+ WorkflowTask.printWorkflowTaskResources();\r
+ logger.info("");\r
+ logger.info("");\r
+ WorkflowTask.printThreadInformation();\r
+\r
+ return ResponseUtility.Build.okRequestWithMessage(message);\r
+ }\r
+ \r
+ @Override\r
+ public Response activateJobExecutor() {\r
+ JobExecutor jobExecutor = ((ProcessEngineConfigurationImpl) (BpmPlatform.getProcessEngineService().getProcessEngine(OtfCamundaConfiguration.processEngineName)).getProcessEngineConfiguration()).getJobExecutor();\r
+ if (!jobExecutor.isActive()) {\r
+ jobExecutor.start();\r
+ }\r
+ return ResponseUtility.Build.okRequest();\r
+ }\r
+\r
+ @Override\r
+ public Response deActivateJobExecutor() {\r
+ JobExecutor jobExecutor = ((ProcessEngineConfigurationImpl) (BpmPlatform.getProcessEngineService().getProcessEngine(OtfCamundaConfiguration.processEngineName)).getProcessEngineConfiguration()).getJobExecutor();\r
+ if (jobExecutor.isActive()) {\r
+ jobExecutor.shutdown();\r
+ }\r
+ return ResponseUtility.Build.okRequest();\r
+ }\r
+ \r
+ @Override\r
+ public Response gracefulShutdown() {\r
+ return this.gracefulShutdown ? ResponseUtility.Build.okRequestWithMessage(shutdown()) : ResponseUtility.Build.okRequestWithMessage("Graceful shutdown is disabled.");\r
+ }\r
+\r
+ @Override\r
+ public Response disableGracefulShutdown() {\r
+ this.gracefulShutdown = false;\r
+ return ResponseUtility.Build.okRequest();\r
+ }\r
+\r
+ @Override\r
+ public Response enableGracefulShutdown() {\r
+ this.gracefulShutdown = true;\r
+ return ResponseUtility.Build.okRequest();\r
+ }\r
+\r
+ @EventListener(ContextClosedEvent.class)\r
+ private String shutdown() {\r
+ String message = "Graceful shutdown:";\r
+ String returnMessage = "Graceful shutdown processes terminated: ";\r
+ try {\r
+ //disable external task service\r
+ OtfExternalTaskService.isEnabled = false;\r
+ //disable job executor\r
+ deActivateJobExecutor();\r
+ logger.info("Disabled job executor and external task service.");\r
+ logger.info("Starting to sleep...");\r
+ Thread.sleep(gracefulWaitTime);\r
+ logger.info("ending to sleep...calling termination service");\r
+ // Call Termination service\r
+ Set<String> processInterrupted = camundaShutdown.gracefulShutdown();\r
+ returnMessage = returnMessage + " " + processInterrupted.size();\r
+ message += String.format("processesInterrupted %s ",\r
+ Arrays.toString(processInterrupted.toArray()));\r
+\r
+ logger.info(message += String.format("processesInterrupted %s ",\r
+ Arrays.toString(processInterrupted.toArray())));\r
+ } catch (InterruptedException e) {\r
+ return "Graceful shutdown processes encountered an error.";\r
+ }\r
+ return returnMessage;\r
+ }\r
+\r
+\r
+\r
+ \r
+}\r