Fix incorrect device status updates

APPM_Imp
Lasantha Dharmakeerthi 5 months ago
commit 743d60e307

@ -37,10 +37,15 @@ import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOFactory;
import io.entgra.device.mgt.core.device.mgt.core.status.task.DeviceStatusTaskException; import io.entgra.device.mgt.core.device.mgt.core.status.task.DeviceStatusTaskException;
import io.entgra.device.mgt.core.device.mgt.core.task.impl.DynamicPartitionedScheduleTask; import io.entgra.device.mgt.core.device.mgt.core.task.impl.DynamicPartitionedScheduleTask;
import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.core.service.RealmService;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* This implements the Task service which monitors the device activity periodically & update the device-status if * This implements the Task service which monitors the device activity periodically & update the device-status if
@ -92,33 +97,70 @@ public class DeviceStatusMonitoringTask extends DynamicPartitionedScheduleTask {
try { try {
List<EnrolmentInfo> enrolmentInfoTobeUpdated = new ArrayList<>(); List<EnrolmentInfo> enrolmentInfoTobeUpdated = new ArrayList<>();
List<DeviceMonitoringData> allDevicesForMonitoring = getAllDevicesForMonitoring(); List<DeviceMonitoringData> allDevicesForMonitoring = getAllDevicesForMonitoring();
long timeMillis = System.currentTimeMillis(); Map<Integer, List<DeviceMonitoringData>> tenantDevicesMap = new HashMap<>();
for (DeviceMonitoringData monitoringData : allDevicesForMonitoring) { List<DeviceMonitoringData> tenantMonitoringData = null;
long lastUpdatedTime = (timeMillis - monitoringData //Delegate the devices in each tenant to a separate list to be updated the statuses.
.getLastUpdatedTime()) / 1000; //This improvement has been done since the tenants maintain a separate caches and the task is running
//in the super-tenant space. Hence, the device status updates are not reflected in the tenant caches.
EnrolmentInfo enrolmentInfo = monitoringData.getDevice().getEnrolmentInfo(); //Refer to https://roadmap.entgra.net/issues/11386 for more information.
EnrolmentInfo.Status status = null; for (DeviceMonitoringData deviceMonitoringData : allDevicesForMonitoring) {
if (lastUpdatedTime >= deviceStatusTaskPluginConfig tenantMonitoringData = tenantDevicesMap.get(deviceMonitoringData.getTenantId());
.getIdleTimeToMarkInactive()) { if (tenantMonitoringData == null) {
status = EnrolmentInfo.Status.INACTIVE; tenantMonitoringData = new ArrayList<>();
} else if (lastUpdatedTime >= deviceStatusTaskPluginConfig
.getIdleTimeToMarkUnreachable()) {
status = EnrolmentInfo.Status.UNREACHABLE;
} }
tenantMonitoringData.add(deviceMonitoringData);
tenantDevicesMap.put(deviceMonitoringData.getTenantId(), tenantMonitoringData);
}
if (status != null) { List<DeviceMonitoringData> monitoringDevices = null;
enrolmentInfo.setStatus(status); long timeMillis = System.currentTimeMillis();
enrolmentInfoTobeUpdated.add(enrolmentInfo); //Retrieving the devices belongs for each tenants and updating the status of the devices.
DeviceIdentifier deviceIdentifier = for (Map.Entry<Integer, List<DeviceMonitoringData>> entry : tenantDevicesMap.entrySet()) {
new DeviceIdentifier(monitoringData.getDevice() Integer tenantId = entry.getKey();
.getDeviceIdentifier(), deviceType); RealmService realmService = DeviceManagementDataHolder.getInstance().getRealmService();
monitoringData.getDevice().setEnrolmentInfo(enrolmentInfo); if (realmService != null) {
DeviceCacheManagerImpl.getInstance().addDeviceToCache(deviceIdentifier, String domain = realmService.getTenantManager().getDomain(tenantId);
monitoringData.getDevice(), monitoringData.getTenantId()); if (domain != null) {
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(domain, true);
monitoringDevices = entry.getValue();
for (DeviceMonitoringData monitoringData : monitoringDevices) {
long lastUpdatedTime = (timeMillis - monitoringData
.getLastUpdatedTime()) / 1000;
EnrolmentInfo enrolmentInfo = monitoringData.getDevice().getEnrolmentInfo();
EnrolmentInfo.Status status = null;
if (lastUpdatedTime >= deviceStatusTaskPluginConfig
.getIdleTimeToMarkInactive()) {
status = EnrolmentInfo.Status.INACTIVE;
} else if (lastUpdatedTime >= deviceStatusTaskPluginConfig
.getIdleTimeToMarkUnreachable()) {
status = EnrolmentInfo.Status.UNREACHABLE;
}
if (status != null) {
enrolmentInfo.setStatus(status);
enrolmentInfoTobeUpdated.add(enrolmentInfo);
DeviceIdentifier deviceIdentifier =
new DeviceIdentifier(monitoringData.getDevice()
.getDeviceIdentifier(), deviceType);
monitoringData.getDevice().setEnrolmentInfo(enrolmentInfo);
DeviceCacheManagerImpl.getInstance().addDeviceToCache(deviceIdentifier,
monitoringData.getDevice(), monitoringData.getTenantId());
}
}
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
} else {
log.error("Failed while running the device status update task. Failed while " +
"extracting tenant domain of the tenant id : " + tenantId);
}
} else {
log.error("Failed while running the device status update task. RealmService is not initiated");
} }
} }
if (!enrolmentInfoTobeUpdated.isEmpty()) { if (!enrolmentInfoTobeUpdated.isEmpty()) {
try { try {
this.updateDeviceStatus(enrolmentInfoTobeUpdated); this.updateDeviceStatus(enrolmentInfoTobeUpdated);
@ -127,11 +169,12 @@ public class DeviceStatusMonitoringTask extends DynamicPartitionedScheduleTask {
"device-status of devices of type '" + deviceType + "'", e); "device-status of devices of type '" + deviceType + "'", e);
} }
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "Error occurred while retrieving devices list for monitoring."; String msg = "Error occurred while retrieving devices list for monitoring.";
log.error(msg, e); log.error(msg, e);
} catch (UserStoreException e) {
String msg = "Error occurred while retrieving RealmService instance for updating device status.";
log.error(msg, e);
} }
} }

Loading…
Cancel
Save