From f92ae35e4ba9a9c10f5e64b596a1bbbea3fa62dc Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Mon, 28 Jan 2019 11:28:10 +0530 Subject: [PATCH] Fix the task running issue in tenant mode If a run time error encountered while sending a notification(e.g:- Push Notification ) to a device when the task is running for tenants, it terminates the task running and notifications won't be sending to devices which are enrolled to other tenants as well. Hence cath the exception in sendNotification method and log. Further, since this is critical path, added few debug logs as well. --- .../operation/mgt/OperationManagerImpl.java | 19 ++++++++++++++++--- .../task/impl/DeviceDetailsRetrieverTask.java | 4 ++++ .../core/task/impl/DeviceTaskManagerImpl.java | 18 +++++++++++++----- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 89e541615c..d2fb72b90e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -147,8 +147,7 @@ public class OperationManagerImpl implements OperationManager { } @Override - public Activity addOperation(Operation operation, - List deviceIds) + public Activity addOperation(Operation operation, List deviceIds) throws OperationManagementException, InvalidDeviceException { if (log.isDebugEnabled()) { log.debug("operation:[" + operation.toString() + "]"); @@ -160,7 +159,13 @@ public class OperationManagerImpl implements OperationManager { try { DeviceIDHolder deviceValidationResult = DeviceManagerUtil.validateDeviceIdentifiers(deviceIds); List validDeviceIds = deviceValidationResult.getValidDeviceIDList(); - if (validDeviceIds.size() > 0) { + if (!validDeviceIds.isEmpty()) { + if (log.isDebugEnabled() && deviceIds.get(0).getType() != null) { + log.debug("Adding operation for Device type : " + deviceIds.get(0).getType() + ", tenant ID:" + + PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId() + ", domain:" + + PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain() + + ", device count:" + deviceIds.size() + " operation type:" + operation.getCode()); + } DeviceIDHolder deviceAuthorizationResult = this.authorizeDevices(operation, validDeviceIds); List authorizedDeviceIds = deviceAuthorizationResult.getValidDeviceIDList(); if (authorizedDeviceIds.size() <= 0) { @@ -285,6 +290,11 @@ public class OperationManagerImpl implements OperationManager { } private void sendNotification(Operation operation, Device device) { + if (log.isDebugEnabled()) { + log.debug("Sending notification for device id: " + device.getDeviceIdentifier() + ", type:" + device + .getType() + ", tenant:" + PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId() + + ", domain:" + PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()); + } NotificationStrategy notificationStrategy = getNotificationStrategy(); /* * If notification strategy has not enable to send push notification using scheduler task we will send @@ -315,6 +325,9 @@ public class OperationManagerImpl implements OperationManager { log.error("Error occurred while setting push notification status to SCHEDULED.", ex); OperationManagementDAOFactory.rollbackTransaction(); } + } catch (Exception e) { + log.error("Error occurred while sending notifications to " + device.getType() + " device carrying id '" + + device.getDeviceIdentifier() + "'", e); } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java index 567964d808..f411c3d9f8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java @@ -86,6 +86,10 @@ public class DeviceDetailsRetrieverTask implements Task { try { List tenants = DeviceManagementDataHolder.getInstance(). getDeviceManagementProvider().getDeviceEnrolledTenants(); + if (log.isDebugEnabled()) { + log.debug("Task is running for " + tenants.size() + " tenants and the device type is " + deviceType); + } + for (Integer tenant : tenants) { String tenantDomain = DeviceManagementDataHolder.getInstance(). getRealmService().getTenantManager().getDomain(tenant); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java index 2132889189..602e656f90 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java @@ -22,6 +22,7 @@ package org.wso2.carbon.device.mgt.core.task.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.MonitoringOperation; @@ -84,18 +85,26 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { try { List devices; List operations; - + List validDeviceIdentifiers; operations = this.getValidOperationNames(); //list operations for each device type devices = deviceManagementProviderService.getAllDevices(deviceType, false);//list devices for each type + if (!devices.isEmpty()) { - if (operations != null && DeviceManagerUtil.getValidDeviceIdentifiers(devices).size() != 0) { + if (log.isDebugEnabled() && deviceType != null) { + log.info("Devices exist to add operations and the total number of devices are " + devices.size()); + } + validDeviceIdentifiers = DeviceManagerUtil.getValidDeviceIdentifiers(devices); + if (!validDeviceIdentifiers.isEmpty()) { + if (log.isDebugEnabled() && deviceType != null) { + log.debug("Number of valid device identifier size to add operations: " + validDeviceIdentifiers + .size()); + } for (String str : operations) { CommandOperation operation = new CommandOperation(); operation.setEnabled(true); operation.setType(Operation.Type.COMMAND); operation.setCode(str); - deviceManagementProviderService.addOperation(deviceType, operation, - DeviceManagerUtil.getValidDeviceIdentifiers(devices)); + deviceManagementProviderService.addOperation(deviceType, operation, validDeviceIdentifiers); } } else { if (log.isDebugEnabled()) { @@ -174,4 +183,3 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { } } -