Fix executing policy monitoring continuously

revert-70ac1926
Charitha Goonetilleke 4 years ago
parent 49a816fd3a
commit 99e5f9f876

@ -52,6 +52,7 @@ public class DeviceStatusMonitoringTask extends DynamicPartitionedScheduleTask {
@Override
public void setProperties(Map<String, String> properties) {
super.setProperties(properties);
deviceType = properties.get(DeviceStatusTaskManagerServiceImpl.DEVICE_TYPE);
deviceTypeId = Integer.parseInt(properties.get(DeviceStatusTaskManagerServiceImpl.DEVICE_TYPE_ID));
String deviceStatusTaskConfigStr = properties.get(DeviceStatusTaskManagerServiceImpl.DEVICE_STATUS_TASK_CONFIG);

@ -39,28 +39,26 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.DynamicTaskContext;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
import org.wso2.carbon.device.mgt.common.StartupOperationConfig;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.task.DeviceMgtTaskException;
import org.wso2.carbon.device.mgt.core.task.DeviceTaskManager;
import org.wso2.carbon.ntask.core.Task;
import org.wso2.carbon.user.api.UserStoreException;
import java.util.List;
import java.util.Map;
public class DeviceDetailsRetrieverTask extends DynamicPartitionedScheduleTask {
private static Log log = LogFactory.getLog(DeviceDetailsRetrieverTask.class);
private static final Log log = LogFactory.getLog(DeviceDetailsRetrieverTask.class);
private String deviceType;
private DeviceManagementProviderService deviceManagementProviderService;
@Override
public void setProperties(Map<String, String> map) {
super.setProperties(map);
deviceType = map.get("DEVICE_TYPE");
}
@ -122,7 +120,7 @@ public class DeviceDetailsRetrieverTask extends DynamicPartitionedScheduleTask {
//pass the configurations also from here, monitoring tasks
try {
if (deviceManagementProviderService.isDeviceMonitoringEnabled(deviceType)) {
deviceTaskManager.addOperations(super.getTaskContext());
deviceTaskManager.addOperations(getTaskContext());
}
} catch (DeviceMgtTaskException e) {
log.error("Error occurred while trying to add the operations to " +

@ -26,12 +26,27 @@ import org.wso2.carbon.device.mgt.common.DynamicTaskContext;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.ntask.core.Task;
import java.util.Map;
public abstract class DynamicPartitionedScheduleTask implements Task {
private static final Log log = LogFactory.getLog(DynamicPartitionedScheduleTask.class);
private static DynamicTaskContext taskContext = null;
private Map<String, String> properties;
@Override
public void setProperties(Map<String, String> properties) {
this.properties = properties;
}
public String getProperty(String name) {
if (properties == null) {
return null;
}
return properties.get(name);
}
@Override
public final void init() {
@ -97,11 +112,7 @@ public abstract class DynamicPartitionedScheduleTask implements Task {
}
public static boolean isDynamicTaskEligible(){
if(taskContext != null && taskContext.isPartitioningEnabled()) {
return true;
} else {
return false;
}
return taskContext != null && taskContext.isPartitioningEnabled();
}
}

@ -21,8 +21,8 @@ package org.wso2.carbon.policy.mgt.core.enforcement;
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.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
@ -36,19 +36,13 @@ import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class DelegationTask extends DynamicPartitionedScheduleTask {
private static final Log log = LogFactory.getLog(DelegationTask.class);
private PolicyConfiguration policyConfiguration = DeviceConfigurationManager.getInstance()
private final PolicyConfiguration policyConfiguration = DeviceConfigurationManager.getInstance()
.getDeviceManagementConfig().getPolicyConfiguration();
@Override
public void setProperties(Map<String, String> map) {
}
@Override
public void executeDynamicTask() {
try {
@ -70,10 +64,10 @@ public class DelegationTask extends DynamicPartitionedScheduleTask {
try {
devices = new ArrayList<>();
toBeNotified = new ArrayList<>();
if (super.isDynamicTaskEligible()) {
if (isDynamicTaskEligible()) {
devices.addAll(service.getAllocatedDevices(deviceType,
super.getTaskContext().getActiveServerCount(),
super.getTaskContext().getServerHashIndex()));
getTaskContext().getActiveServerCount(),
getTaskContext().getServerHashIndex()));
} else {
devices.addAll(service.getAllDevices(deviceType, false));
}
@ -81,11 +75,11 @@ public class DelegationTask extends DynamicPartitionedScheduleTask {
if (device != null && device.getEnrolmentInfo() != null
&& device.getEnrolmentInfo().getStatus() != EnrolmentInfo.Status.REMOVED) {
toBeNotified.add(device);
}
if (log.isDebugEnabled()) {
log.debug("Adding policy operation to device : " + device.getDeviceIdentifier());
}
}
}
if (!toBeNotified.isEmpty()) {
PolicyEnforcementDelegator enforcementDelegator = new PolicyEnforcementDelegatorImpl(
toBeNotified, updatedPolicyDeviceList.getUpdatedPolicyIds());

@ -24,33 +24,22 @@ import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager;
import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.task.impl.DynamicPartitionedScheduleTask;
import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder;
import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager;
import org.wso2.carbon.policy.mgt.core.util.PolicyManagementConstants;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class MonitoringTask extends DynamicPartitionedScheduleTask {
private static final Log log = LogFactory.getLog(MonitoringTask.class);
@Override
public void setProperties(Map<String, String> map) {
}
@Override
public void executeDynamicTask() {
if (log.isDebugEnabled()) {
log.debug("Monitoring task started to run.");
}
this.executeforAllTenants();
}
private String tenant;
/**
* Check whether Device platform (ex: android) is exist in the cdm-config.xml file before adding a
@ -66,32 +55,29 @@ public class MonitoringTask extends DynamicPartitionedScheduleTask {
return policyMonitoringManager != null;
}
private void executeforAllTenants() {
@Override
public void executeDynamicTask() {
tenant = getProperty(PolicyManagementConstants.TENANT_ID);
if (tenant == null) {
log.warn("Tenant id of the Monitoring Task got null");
return;
}
int tenantId = Integer.parseInt(tenant);
if (log.isDebugEnabled()) {
log.debug("Monitoring task started to run for all tenants.");
log.debug("Monitoring task started to run for tenant: " + tenant);
}
try {
DeviceManagementProviderService deviceManagementService = PolicyManagementDataHolder
.getInstance().getDeviceManagementService();
List<Integer> tenants = deviceManagementService.getDeviceEnrolledTenants();
for (Integer tenant : tenants) {
if (MultitenantConstants.SUPER_TENANT_ID == tenant) {
if (MultitenantConstants.SUPER_TENANT_ID == tenantId) {
this.executeTask();
continue;
return;
}
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant, true);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true);
this.executeTask();
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
} catch (DeviceManagementException e) {
log.error("Error occurred while trying to get the available tenants from device manager service ", e);
}
}
private void executeTask() {
MonitoringManager monitoringManager = PolicyManagementDataHolder.getInstance().getMonitoringManager();
@ -105,7 +91,7 @@ public class MonitoringTask extends DynamicPartitionedScheduleTask {
}
}
} catch (PolicyComplianceException e) {
log.error("Error occurred while getting the device types.");
log.error("TID:[" + tenant + "] Error occurred while getting the device types.");
}
if (!deviceTypes.isEmpty()) {
try {
@ -113,56 +99,57 @@ public class MonitoringTask extends DynamicPartitionedScheduleTask {
PolicyManagementDataHolder.getInstance().getDeviceManagementService();
for (String deviceType : configDeviceTypes) {
if (log.isDebugEnabled()) {
log.debug("Running task for device type : " + deviceType);
log.debug("TID:[" + tenant + "] Running task for device type : " + deviceType);
}
PolicyMonitoringManager monitoringService =
PolicyManagementDataHolder.getInstance().getDeviceManagementService()
.getPolicyMonitoringManager(deviceType);
List<Device> devices;
if(super.isDynamicTaskEligible()){
devices = deviceManagementProviderService.getAllocatedDevices(deviceType,
super.getTaskContext().getActiveServerCount(),
super.getTaskContext().getServerHashIndex());
if(isDynamicTaskEligible()){
devices = deviceManagementProviderService
.getAllocatedDevices(deviceType, getTaskContext().getActiveServerCount(),
getTaskContext().getServerHashIndex());
} else {
devices = deviceManagementProviderService.getAllDevices(deviceType, false);
}
if (monitoringService != null && !devices.isEmpty()) {
List<Device> notifiableDevices = new ArrayList<>();
if (log.isDebugEnabled()) {
log.debug("Removing inactive and blocked devices from the list for the device type : " +
deviceType);
log.debug("TID:[" + tenant + "] Removing inactive and blocked devices from " +
"the list for the device type : " + deviceType);
}
StringBuilder sb = new StringBuilder();
for (Device device : devices) {
EnrolmentInfo.Status status = device.getEnrolmentInfo().getStatus();
if (status.equals(EnrolmentInfo.Status.ACTIVE) ||
status.equals(EnrolmentInfo.Status.INACTIVE) ||
status.equals(EnrolmentInfo.Status.UNREACHABLE)) {
notifiableDevices.add(device);
}
if (log.isDebugEnabled()) {
log.debug("Adding monitoring operation to device : " + device.getDeviceIdentifier());
if (sb.length() > 0) {
sb.append(", ");
}
sb.append(device.getDeviceIdentifier());
}
if (log.isDebugEnabled()) {
log.debug("Following '" + deviceType + "' devices selected to send the notification " +
"for policy monitoring");
for (Device device : notifiableDevices) {
log.debug(device.getDeviceIdentifier());
}
}
if (log.isDebugEnabled()) {
log.debug("TID:[" + tenant + "] Sending monitoring to '" + deviceType +
"' devices with ids [" + sb + "]");
}
if (!notifiableDevices.isEmpty()) {
monitoringManager.addMonitoringOperation(deviceType, notifiableDevices);
}
}
}
if (log.isDebugEnabled()) {
log.debug("Monitoring task running completed.");
log.debug("TID:[" + tenant + "] Monitoring task running completed.");
}
} catch (Exception e) {
log.error("Error occurred while trying to run a task.", e);
log.error("TID:[" + tenant + "] Error occurred while trying to run a task.", e);
}
} else {
log.info("No device types registered currently. So did not run the monitoring task.");
log.info("TID:[" + tenant + "] No device types registered currently. " +
"So did not run the monitoring task.");
}
}
@ -170,4 +157,5 @@ public class MonitoringTask extends DynamicPartitionedScheduleTask {
protected void setup() {
}
}

@ -39,7 +39,7 @@ import java.util.Map;
public class TaskScheduleServiceImpl implements TaskScheduleService {
private static final Log log = LogFactory.getLog(TaskScheduleServiceImpl.class);
private PolicyConfiguration policyConfiguration;
private final PolicyConfiguration policyConfiguration;
public TaskScheduleServiceImpl() {
@ -72,7 +72,7 @@ public class TaskScheduleServiceImpl implements TaskScheduleService {
Map<String, String> properties = new HashMap<>();
properties.put(PolicyManagementConstants.TENANT_ID, String.valueOf(tenantId));
String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId);
String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + tenantId;
if (!taskManager.isTaskScheduled(taskName)) {
@ -101,7 +101,7 @@ public class TaskScheduleServiceImpl implements TaskScheduleService {
public void stopTask() throws PolicyMonitoringTaskException {
int tenantId = getTenantId();
try {
String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId);
String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + tenantId;
TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService();
if (taskService.isServerInit()) {
TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE);
@ -117,7 +117,7 @@ public class TaskScheduleServiceImpl implements TaskScheduleService {
public void updateTask(int monitoringFrequency) throws PolicyMonitoringTaskException {
int tenantId = getTenantId();
try {
String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId);
String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + tenantId;
TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService();
TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE);
@ -129,7 +129,7 @@ public class TaskScheduleServiceImpl implements TaskScheduleService {
triggerInfo.setRepeatCount(-1);
Map<String, String> properties = new HashMap<>();
properties.put("tenantId", String.valueOf(tenantId));
properties.put(PolicyManagementConstants.TENANT_ID, String.valueOf(tenantId));
TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.MONITORING_TASK_CLAZZ, properties,
triggerInfo);
@ -150,7 +150,7 @@ public class TaskScheduleServiceImpl implements TaskScheduleService {
@Override
public boolean isTaskScheduled() throws PolicyMonitoringTaskException {
int tenantId = getTenantId();
String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId);
String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + tenantId;
TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService();
TaskManager taskManager;
try {

Loading…
Cancel
Save