forked from community/device-mgt-core
operation timeout feature See merge request entgra/carbon-device-mgt!838temp
commit
ff1438ac09
@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package org.wso2.carbon.device.mgt.core.config.operation.timeout;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlElementWrapper;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@XmlRootElement(name = "OperationTimeout")
|
||||||
|
public class OperationTimeout {
|
||||||
|
|
||||||
|
private String code;
|
||||||
|
private int timeout;
|
||||||
|
private List<String> deviceTypes;
|
||||||
|
private String initialStatus;
|
||||||
|
private String nextStatus;
|
||||||
|
|
||||||
|
public String getCode() {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "Code", required = true)
|
||||||
|
public void setCode(String code) {
|
||||||
|
this.code = code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTimeout() {
|
||||||
|
return timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "Timeout", required = true)
|
||||||
|
public void setTimeout(int timeout) {
|
||||||
|
this.timeout = timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getDeviceTypes() {
|
||||||
|
return deviceTypes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElementWrapper(name = "DeviceTypes", required = true)
|
||||||
|
@XmlElement(name = "DeviceType", required = true)
|
||||||
|
public void setDeviceTypes(List<String> deviceTypes) {
|
||||||
|
this.deviceTypes = deviceTypes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getInitialStatus() {
|
||||||
|
return initialStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "InitialStatus", required = true)
|
||||||
|
public void setInitialStatus(String initialStatus) {
|
||||||
|
this.initialStatus = initialStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNextStatus() {
|
||||||
|
return nextStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "NextStatus", required = true)
|
||||||
|
public void setNextStatus(String nextStatus) {
|
||||||
|
this.nextStatus = nextStatus;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package org.wso2.carbon.device.mgt.core.config.operation.timeout;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlElementWrapper;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@XmlRootElement(name = "OperationTimeoutConfigurations")
|
||||||
|
public class OperationTimeoutConfiguration {
|
||||||
|
private List<OperationTimeout> operationTimeoutList;
|
||||||
|
|
||||||
|
public List<OperationTimeout> getOperationTimeoutList() {
|
||||||
|
return operationTimeoutList;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElementWrapper(name = "OperationTimeouts", required = true)
|
||||||
|
@XmlElement(name = "OperationTimeout", required = false)
|
||||||
|
public void setOperationTimeoutList(List<OperationTimeout> operationTimeoutList) {
|
||||||
|
this.operationTimeoutList = operationTimeoutList;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.core.operation.timeout.task;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This exception class defines the custom exceptions thrown by the OperationTimeoutTask related components.
|
||||||
|
*/
|
||||||
|
public class OperationTimeoutTaskException extends Exception {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -31222242646464497L;
|
||||||
|
|
||||||
|
private String errorMessage;
|
||||||
|
|
||||||
|
public String getErrorMessage() {
|
||||||
|
return errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setErrorMessage(String errorMessage) {
|
||||||
|
this.errorMessage = errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OperationTimeoutTaskException(String msg, Exception nestedEx) {
|
||||||
|
super(msg, nestedEx);
|
||||||
|
setErrorMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public OperationTimeoutTaskException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
setErrorMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public OperationTimeoutTaskException(String msg) {
|
||||||
|
super(msg);
|
||||||
|
setErrorMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public OperationTimeoutTaskException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public OperationTimeoutTaskException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.core.operation.timeout.task;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.core.config.operation.timeout.OperationTimeout;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This interface defines the methods that should be implemented by the management service of OperationTimeoutTask
|
||||||
|
*/
|
||||||
|
public interface OperationTimeoutTaskManagerService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will start the task
|
||||||
|
* @param config
|
||||||
|
* @throws OperationTimeoutTaskException
|
||||||
|
*/
|
||||||
|
void startTask(OperationTimeout config)
|
||||||
|
throws OperationTimeoutTaskException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will stop the task.
|
||||||
|
* @param config
|
||||||
|
* @throws OperationTimeoutTaskException
|
||||||
|
*/
|
||||||
|
void stopTask(OperationTimeout config)
|
||||||
|
throws OperationTimeoutTaskException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This will update the task frequency which it runs.
|
||||||
|
*
|
||||||
|
* @param config
|
||||||
|
* @throws OperationTimeoutTaskException
|
||||||
|
*/
|
||||||
|
void updateTask(OperationTimeout config)
|
||||||
|
throws OperationTimeoutTaskException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This will check weather the task is scheduled.
|
||||||
|
* @return
|
||||||
|
* @throws OperationTimeoutTaskException
|
||||||
|
*/
|
||||||
|
boolean isTaskScheduled(OperationTimeout config) throws OperationTimeoutTaskException;
|
||||||
|
}
|
@ -0,0 +1,106 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package org.wso2.carbon.device.mgt.core.operation.timeout.task.impl;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
|
||||||
|
import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus;
|
||||||
|
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
|
||||||
|
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.core.config.operation.timeout.OperationTimeout;
|
||||||
|
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
|
||||||
|
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
||||||
|
import org.wso2.carbon.device.mgt.core.task.impl.DynamicPartitionedScheduleTask;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class OperationTimeoutTask extends DynamicPartitionedScheduleTask {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(OperationTimeoutTask.class);
|
||||||
|
private OperationTimeout operationTimeoutConfig;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setProperties(Map<String, String> properties) {
|
||||||
|
super.setProperties(properties);
|
||||||
|
String operationTimeoutTaskConfigStr = properties
|
||||||
|
.get(OperationTimeoutTaskManagerServiceImpl.OPERATION_TIMEOUT_TASK_CONFIG);
|
||||||
|
Gson gson = new Gson();
|
||||||
|
operationTimeoutConfig = gson.fromJson(operationTimeoutTaskConfigStr, OperationTimeout.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getProperty(String name) {
|
||||||
|
return super.getProperty(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void refreshContext() {
|
||||||
|
super.refreshContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void setup() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void executeDynamicTask() {
|
||||||
|
try {
|
||||||
|
|
||||||
|
long timeMillis = System.currentTimeMillis() - operationTimeoutConfig.getTimeout() * 60 * 1000;
|
||||||
|
List<String> deviceTypes = new ArrayList<>();
|
||||||
|
if (operationTimeoutConfig.getDeviceTypes().size() == 1 &&
|
||||||
|
"ALL".equals(operationTimeoutConfig.getDeviceTypes().get( 0))) {
|
||||||
|
try {
|
||||||
|
List<DeviceType> deviceTypeList = DeviceManagementDataHolder.getInstance()
|
||||||
|
.getDeviceManagementProvider().getDeviceTypes();
|
||||||
|
for (DeviceType deviceType : deviceTypeList) {
|
||||||
|
deviceTypes.add(deviceType.getName());
|
||||||
|
}
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
log.error("Error occurred while reading device types", e);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
deviceTypes = operationTimeoutConfig.getDeviceTypes();
|
||||||
|
}
|
||||||
|
List<Activity> activities = DeviceManagementDataHolder.getInstance().getOperationManager()
|
||||||
|
.getActivities(deviceTypes, operationTimeoutConfig.getCode(), timeMillis,
|
||||||
|
operationTimeoutConfig.getInitialStatus());
|
||||||
|
for (Activity activity : activities) {
|
||||||
|
for (ActivityStatus activityStatus : activity.getActivityStatus()) {
|
||||||
|
String operationId = activity.getActivityId().replace("ACTIVITY_", "");
|
||||||
|
Operation operation = DeviceManagementDataHolder.getInstance().getOperationManager()
|
||||||
|
.getOperation(Integer.parseInt(operationId));
|
||||||
|
operation.setStatus(Operation.Status.valueOf(operationTimeoutConfig.getNextStatus()));
|
||||||
|
DeviceManagementDataHolder.getInstance().getOperationManager()
|
||||||
|
.updateOperation(activityStatus.getDeviceIdentifier(), operation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (OperationManagementException e) {
|
||||||
|
String msg = "Error occurred while retrieving operations.";
|
||||||
|
log.error(msg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,172 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package org.wso2.carbon.device.mgt.core.operation.timeout.task.impl;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.core.config.operation.timeout.OperationTimeout;
|
||||||
|
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
||||||
|
import org.wso2.carbon.device.mgt.core.operation.timeout.task.OperationTimeoutTaskException;
|
||||||
|
import org.wso2.carbon.device.mgt.core.operation.timeout.task.OperationTimeoutTaskManagerService;
|
||||||
|
import org.wso2.carbon.ntask.common.TaskException;
|
||||||
|
import org.wso2.carbon.ntask.core.TaskInfo;
|
||||||
|
import org.wso2.carbon.ntask.core.TaskManager;
|
||||||
|
import org.wso2.carbon.ntask.core.service.TaskService;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class OperationTimeoutTaskManagerServiceImpl implements OperationTimeoutTaskManagerService {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(OperationTimeoutTaskManagerServiceImpl.class);
|
||||||
|
|
||||||
|
public static final String OPERATION_TIMEOUT_TASK = "OPERATION_TIMEOUT_TASK";
|
||||||
|
static final String DEVICE_TYPES = "DEVICE_TYPES";
|
||||||
|
static final String OPERATION_TIMEOUT_TASK_CONFIG = "OPERATION_TIMEOUT_TASK_CONFIG";
|
||||||
|
static final String INITIAL_STATUS = "INITIAL_STATUS";
|
||||||
|
private static final String TASK_CLASS = OperationTimeoutTask.class.getName();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void startTask(OperationTimeout config)
|
||||||
|
throws OperationTimeoutTaskException {
|
||||||
|
log.info("Operation timeout task adding for device type(s) : " + config.getDeviceTypes()
|
||||||
|
+ ", operation code : " + config.getInitialStatus());
|
||||||
|
|
||||||
|
try {
|
||||||
|
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
|
||||||
|
taskService.registerTaskType(OPERATION_TIMEOUT_TASK);
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Operation timeout task is started for the device type(s) : " + config.getDeviceTypes()
|
||||||
|
+ ", operation code : " + config.getInitialStatus());
|
||||||
|
log.debug(
|
||||||
|
"Operation timeout task is at frequency of : " + config.getTimeout() + " minutes");
|
||||||
|
}
|
||||||
|
|
||||||
|
TaskManager taskManager = taskService.getTaskManager(OPERATION_TIMEOUT_TASK);
|
||||||
|
|
||||||
|
TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo();
|
||||||
|
//Convert to milli seconds
|
||||||
|
triggerInfo.setIntervalMillis(config.getTimeout() * 60 * 1000);
|
||||||
|
triggerInfo.setRepeatCount(-1);
|
||||||
|
|
||||||
|
Gson gson = new Gson();
|
||||||
|
String operationTimeoutConfig = gson.toJson(config);
|
||||||
|
|
||||||
|
Map<String, String> properties = new HashMap<>();
|
||||||
|
|
||||||
|
String deviceTypes = StringUtils.join(config.getDeviceTypes(), "_");
|
||||||
|
properties.put(DEVICE_TYPES, deviceTypes);
|
||||||
|
properties.put(INITIAL_STATUS, config.getInitialStatus());
|
||||||
|
properties.put(OPERATION_TIMEOUT_TASK_CONFIG, operationTimeoutConfig);
|
||||||
|
|
||||||
|
String taskName = OPERATION_TIMEOUT_TASK + "_" + config.getInitialStatus() + "_" + deviceTypes;
|
||||||
|
|
||||||
|
if (!taskManager.isTaskScheduled(taskName)) {
|
||||||
|
TaskInfo taskInfo = new TaskInfo(taskName, TASK_CLASS, properties, triggerInfo);
|
||||||
|
taskManager.registerTask(taskInfo);
|
||||||
|
taskManager.rescheduleTask(taskInfo.getName());
|
||||||
|
} else {
|
||||||
|
throw new OperationTimeoutTaskException(
|
||||||
|
"Operation Timeout task is already started for the device type(s) : " + config.getDeviceTypes()
|
||||||
|
+ ", operation code : " + config.getInitialStatus());
|
||||||
|
}
|
||||||
|
} catch (TaskException e) {
|
||||||
|
throw new OperationTimeoutTaskException("Error occurred while creating the Operation timeout task " +
|
||||||
|
"for the device type(s) : " + config.getDeviceTypes() + ", operation code : " + config
|
||||||
|
.getInitialStatus(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stopTask(OperationTimeout config)
|
||||||
|
throws OperationTimeoutTaskException {
|
||||||
|
try {
|
||||||
|
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
|
||||||
|
String deviceTypes = StringUtils.join(config.getDeviceTypes(), "_");
|
||||||
|
String taskName = OPERATION_TIMEOUT_TASK + "_" + config.getInitialStatus() + "_" + deviceTypes;
|
||||||
|
if (taskService != null && taskService.isServerInit()) {
|
||||||
|
TaskManager taskManager = taskService.getTaskManager(OPERATION_TIMEOUT_TASK);
|
||||||
|
taskManager.deleteTask(taskName);
|
||||||
|
}
|
||||||
|
} catch (TaskException e) {
|
||||||
|
throw new OperationTimeoutTaskException("Error occurred while deleting the Operation timeout task " +
|
||||||
|
"for the device type(s) : " + config.getDeviceTypes() + ", operation code : " + config
|
||||||
|
.getInitialStatus(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateTask(OperationTimeout config)
|
||||||
|
throws OperationTimeoutTaskException {
|
||||||
|
try {
|
||||||
|
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
|
||||||
|
TaskManager taskManager = taskService.getTaskManager(OPERATION_TIMEOUT_TASK);
|
||||||
|
String deviceTypes = StringUtils.join(config.getDeviceTypes(), "_");
|
||||||
|
String taskName = OPERATION_TIMEOUT_TASK + "_" + config.getInitialStatus() + "_" + deviceTypes;
|
||||||
|
|
||||||
|
if (taskManager.isTaskScheduled(taskName)) {
|
||||||
|
taskManager.deleteTask(taskName);
|
||||||
|
TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo();
|
||||||
|
triggerInfo.setIntervalMillis(config.getTimeout() * 60 * 1000);
|
||||||
|
triggerInfo.setRepeatCount(-1);
|
||||||
|
|
||||||
|
Map<String, String> properties = new HashMap<>();
|
||||||
|
properties.put(DEVICE_TYPES, deviceTypes);
|
||||||
|
properties.put(INITIAL_STATUS, config.getInitialStatus());
|
||||||
|
|
||||||
|
Gson gson = new Gson();
|
||||||
|
String deviceStatusTaskConfigs = gson.toJson(config);
|
||||||
|
properties.put(OPERATION_TIMEOUT_TASK_CONFIG, deviceStatusTaskConfigs);
|
||||||
|
|
||||||
|
TaskInfo taskInfo = new TaskInfo(taskName, TASK_CLASS, properties, triggerInfo);
|
||||||
|
|
||||||
|
taskManager.registerTask(taskInfo);
|
||||||
|
taskManager.rescheduleTask(taskInfo.getName());
|
||||||
|
} else {
|
||||||
|
throw new OperationTimeoutTaskException(
|
||||||
|
"Operation timeout task has not been started for this device-type the device type(s) : "
|
||||||
|
+ config.getDeviceTypes() + ", operation code : " + config.getInitialStatus() +
|
||||||
|
". Please start the task first.");
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (TaskException e) {
|
||||||
|
throw new OperationTimeoutTaskException("Error occurred while updating the Operation timeout task " +
|
||||||
|
"for the device type(s) : " + config.getDeviceTypes() + ", operation code : " +
|
||||||
|
config.getInitialStatus(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isTaskScheduled(OperationTimeout config) throws OperationTimeoutTaskException {
|
||||||
|
String deviceTypes = StringUtils.join(config.getDeviceTypes(), "_");
|
||||||
|
String taskName = OPERATION_TIMEOUT_TASK + "_" + config.getInitialStatus() + "_" + deviceTypes;
|
||||||
|
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
|
||||||
|
TaskManager taskManager;
|
||||||
|
try {
|
||||||
|
taskManager = taskService.getTaskManager(OPERATION_TIMEOUT_TASK);
|
||||||
|
return taskManager.isTaskScheduled(taskName);
|
||||||
|
} catch (TaskException e) {
|
||||||
|
throw new OperationTimeoutTaskException("Error occurred while checking the task schedule status " +
|
||||||
|
"of the Operation timeout task for the device type(s) : " + config.getDeviceTypes() +
|
||||||
|
", operation code : " + config.getInitialStatus(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue