operation timeout task updated to execute only in elected node

mssqldbscriptfix
Amalka Subasinghe 1 year ago
parent 349fce4e63
commit 21606bc5cd

@ -18,6 +18,7 @@
package io.entgra.device.mgt.core.device.mgt.core.operation.timeout.task.impl; package io.entgra.device.mgt.core.device.mgt.core.operation.timeout.task.impl;
import com.google.gson.Gson; import com.google.gson.Gson;
import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.exception.HeartBeatManagementException;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException;
@ -36,7 +37,6 @@ import java.util.List;
public class OperationTimeoutTask extends DynamicPartitionedScheduleTask { public class OperationTimeoutTask extends DynamicPartitionedScheduleTask {
private static final Log log = LogFactory.getLog(OperationTimeoutTask.class); private static final Log log = LogFactory.getLog(OperationTimeoutTask.class);
@Override @Override
protected void setup() { protected void setup() {
@ -44,45 +44,60 @@ public class OperationTimeoutTask extends DynamicPartitionedScheduleTask {
@Override @Override
protected void executeDynamicTask() { protected void executeDynamicTask() {
String operationTimeoutTaskConfigStr = getProperty( if (isQualifiedToExecuteTask()) { // this task will run only in one node when the deployment has multiple nodes
OperationTimeoutTaskManagerServiceImpl.OPERATION_TIMEOUT_TASK_CONFIG); String operationTimeoutTaskConfigStr = getProperty(
Gson gson = new Gson(); OperationTimeoutTaskManagerServiceImpl.OPERATION_TIMEOUT_TASK_CONFIG);
OperationTimeout operationTimeoutConfig = gson.fromJson(operationTimeoutTaskConfigStr, OperationTimeout.class); Gson gson = new Gson();
try { OperationTimeout operationTimeoutConfig = gson.fromJson(operationTimeoutTaskConfigStr, OperationTimeout.class);
long timeMillis = System.currentTimeMillis() - operationTimeoutConfig.getTimeout() * 60 * 1000; try {
List<String> deviceTypes = new ArrayList<>(); long timeMillis = System.currentTimeMillis() - (long) operationTimeoutConfig.getTimeout();
if (operationTimeoutConfig.getDeviceTypes().size() == 1 && List<String> deviceTypes = new ArrayList<>();
"ALL".equals(operationTimeoutConfig.getDeviceTypes().get(0))) { if (operationTimeoutConfig.getDeviceTypes().size() == 1 &&
try { "ALL".equals(operationTimeoutConfig.getDeviceTypes().get(0))) {
List<DeviceType> deviceTypeList = DeviceManagementDataHolder.getInstance() try {
.getDeviceManagementProvider().getDeviceTypes(); List<DeviceType> deviceTypeList = DeviceManagementDataHolder.getInstance()
for (DeviceType deviceType : deviceTypeList) { .getDeviceManagementProvider().getDeviceTypes();
deviceTypes.add(deviceType.getName()); for (DeviceType deviceType : deviceTypeList) {
deviceTypes.add(deviceType.getName());
}
} catch (DeviceManagementException e) {
log.error("Error occurred while reading device types", e);
} }
} catch (DeviceManagementException e) { } else {
log.error("Error occurred while reading device types", e); deviceTypes = operationTimeoutConfig.getDeviceTypes();
} }
} else { List<Activity> activities = DeviceManagementDataHolder.getInstance().getOperationManager()
deviceTypes = operationTimeoutConfig.getDeviceTypes(); .getActivities(deviceTypes, operationTimeoutConfig.getCode(), timeMillis,
} operationTimeoutConfig.getInitialStatus());
List<Activity> activities = DeviceManagementDataHolder.getInstance().getOperationManager() for (Activity activity : activities) {
.getActivities(deviceTypes, operationTimeoutConfig.getCode(), timeMillis, for (ActivityStatus activityStatus : activity.getActivityStatus()) {
operationTimeoutConfig.getInitialStatus()); String operationId = activity.getActivityId().replace("ACTIVITY_", "");
for (Activity activity : activities) { Operation operation = DeviceManagementDataHolder.getInstance().getOperationManager()
for (ActivityStatus activityStatus : activity.getActivityStatus()) { .getOperation(Integer.parseInt(operationId));
String operationId = activity.getActivityId().replace("ACTIVITY_", ""); operation.setStatus(Operation.Status.valueOf(operationTimeoutConfig.getNextStatus()));
Operation operation = DeviceManagementDataHolder.getInstance().getOperationManager() DeviceManagementDataHolder.getInstance().getOperationManager()
.getOperation(Integer.parseInt(operationId)); .updateOperation(activityStatus.getDeviceIdentifier(), operation);
operation.setStatus(Operation.Status.valueOf(operationTimeoutConfig.getNextStatus())); }
DeviceManagementDataHolder.getInstance().getOperationManager()
.updateOperation(activityStatus.getDeviceIdentifier(), operation);
} }
}
} catch (OperationManagementException e) { } catch (OperationManagementException e) {
String msg = "Error occurred while retrieving operations."; String msg = "Error occurred while retrieving operations.";
log.error(msg, e); log.error(msg, e);
}
} }
} }
private boolean isQualifiedToExecuteTask() {
if (isDynamicTaskEligible()) {
try {
return DeviceManagementDataHolder.getInstance().getHeartBeatService().isQualifiedToExecuteTask();
} catch (HeartBeatManagementException e) {
log.error("Error while checking is qualified to execute task", e);
}
} else {
return true;
}
return false;
}
} }

@ -57,14 +57,14 @@ public class OperationTimeoutTaskManagerServiceImpl implements OperationTimeoutT
log.debug("Operation timeout task is started for the device type(s) : " + config.getDeviceTypes() log.debug("Operation timeout task is started for the device type(s) : " + config.getDeviceTypes()
+ ", operation code : " + config.getInitialStatus()); + ", operation code : " + config.getInitialStatus());
log.debug( log.debug(
"Operation timeout task is at frequency of : " + config.getTimeout() + " minutes"); "Operation timeout task is at frequency of : " + config.getTimeout() + " milliseconds");
} }
TaskManager taskManager = taskService.getTaskManager(OPERATION_TIMEOUT_TASK); TaskManager taskManager = taskService.getTaskManager(OPERATION_TIMEOUT_TASK);
TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo(); TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo();
//Convert to milli seconds //Convert to milli seconds
triggerInfo.setIntervalMillis(config.getTimeout() * 60 * 1000); triggerInfo.setIntervalMillis(config.getTimeout());
triggerInfo.setRepeatCount(-1); triggerInfo.setRepeatCount(-1);
Gson gson = new Gson(); Gson gson = new Gson();
@ -125,7 +125,7 @@ public class OperationTimeoutTaskManagerServiceImpl implements OperationTimeoutT
if (taskManager.isTaskScheduled(taskName)) { if (taskManager.isTaskScheduled(taskName)) {
taskManager.deleteTask(taskName); taskManager.deleteTask(taskName);
TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo(); TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo();
triggerInfo.setIntervalMillis(config.getTimeout() * 60 * 1000); triggerInfo.setIntervalMillis(config.getTimeout());
triggerInfo.setRepeatCount(-1); triggerInfo.setRepeatCount(-1);
Map<String, String> properties = new HashMap<>(); Map<String, String> properties = new HashMap<>();

@ -353,7 +353,7 @@
<!--</DeviceTypes>--> <!--</DeviceTypes>-->
<!--<Code>DOUBLE_COMMAND</Code>--> <!--<Code>DOUBLE_COMMAND</Code>-->
<!--<InitialStatus>REQUIRED_CONFIRMATION</InitialStatus>--> <!--<InitialStatus>REQUIRED_CONFIRMATION</InitialStatus>-->
<!--<Timeout>30</Timeout>--> <!--<Timeout>30000</Timeout>-->
<!--<NextStatus>ERROR</NextStatus>--> <!--<NextStatus>ERROR</NextStatus>-->
<!--</OperationTimeout>--> <!--</OperationTimeout>-->
{% if device_mgt_conf.operation_timeout_conf is defined %} {% if device_mgt_conf.operation_timeout_conf is defined %}

Loading…
Cancel
Save