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.
revert-70aa11f8
lasanthaDLPDS 6 years ago
parent ecd35c34e4
commit f92ae35e4b

@ -147,8 +147,7 @@ public class OperationManagerImpl implements OperationManager {
}
@Override
public Activity addOperation(Operation operation,
List<DeviceIdentifier> deviceIds)
public Activity addOperation(Operation operation, List<DeviceIdentifier> 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<DeviceIdentifier> 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<DeviceIdentifier> 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);
}
}
}

@ -86,6 +86,10 @@ public class DeviceDetailsRetrieverTask implements Task {
try {
List<Integer> 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);

@ -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<Device> devices;
List<String> operations;
List<DeviceIdentifier> 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 {
}
}

Loading…
Cancel
Save