|
|
@ -18,7 +18,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
package io.entgra.task.mgt.watcher;
|
|
|
|
package io.entgra.task.mgt.watcher;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import io.entgra.server.bootup.heartbeat.beacon.exception.HeartBeatManagementException;
|
|
|
|
import io.entgra.task.mgt.common.bean.DynamicTask;
|
|
|
|
import io.entgra.task.mgt.common.bean.DynamicTask;
|
|
|
|
import io.entgra.task.mgt.common.constant.TaskMgtConstants;
|
|
|
|
import io.entgra.task.mgt.common.constant.TaskMgtConstants;
|
|
|
|
import io.entgra.task.mgt.common.exception.TaskManagementException;
|
|
|
|
import io.entgra.task.mgt.common.exception.TaskManagementException;
|
|
|
@ -26,13 +26,21 @@ import io.entgra.task.mgt.core.util.TaskManagementUtil;
|
|
|
|
import io.entgra.task.mgt.watcher.internal.TaskWatcherDataHolder;
|
|
|
|
import io.entgra.task.mgt.watcher.internal.TaskWatcherDataHolder;
|
|
|
|
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 org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
import org.wso2.carbon.core.ServerStartupObserver;
|
|
|
|
import org.wso2.carbon.core.ServerStartupObserver;
|
|
|
|
import org.wso2.carbon.ntask.common.TaskException;
|
|
|
|
import org.wso2.carbon.ntask.common.TaskException;
|
|
|
|
import org.wso2.carbon.ntask.core.TaskInfo;
|
|
|
|
import org.wso2.carbon.ntask.core.TaskInfo;
|
|
|
|
import org.wso2.carbon.ntask.core.TaskManager;
|
|
|
|
import org.wso2.carbon.ntask.core.TaskManager;
|
|
|
|
import org.wso2.carbon.ntask.core.service.TaskService;
|
|
|
|
import org.wso2.carbon.ntask.core.service.TaskService;
|
|
|
|
|
|
|
|
import org.wso2.carbon.user.api.Tenant;
|
|
|
|
|
|
|
|
import org.wso2.carbon.user.api.UserStoreException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.user.core.service.RealmService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Timer;
|
|
|
|
import java.util.Timer;
|
|
|
|
import java.util.TimerTask;
|
|
|
|
import java.util.TimerTask;
|
|
|
|
|
|
|
|
|
|
|
@ -49,93 +57,169 @@ public class IoTSStartupHandler implements ServerStartupObserver {
|
|
|
|
timer.schedule(new TimerTask() {
|
|
|
|
timer.schedule(new TimerTask() {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
public void run() {
|
|
|
|
compareTasks();
|
|
|
|
try {
|
|
|
|
|
|
|
|
compareTasks();
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
log.error("Error occurred when comparing tasks.", e);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 200000, 600000);
|
|
|
|
}, 200000, 600000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void compareTasks() {
|
|
|
|
private void compareTasks() {
|
|
|
|
log.info("Comparing Tasks from carbon n task manager and engtra task manager");
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
TaskManager taskManager = null;
|
|
|
|
log.debug("Comparing Tasks from carbon nTask manager and entgra task manager");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TaskManager taskManager;
|
|
|
|
TaskService nTaskService = TaskWatcherDataHolder.getInstance().getnTaskService();
|
|
|
|
TaskService nTaskService = TaskWatcherDataHolder.getInstance().getnTaskService();
|
|
|
|
if (nTaskService == null) {
|
|
|
|
if (nTaskService == null) {
|
|
|
|
String msg = "Unable to load TaskService from the carbon n task core";
|
|
|
|
String msg = "Unable to load TaskService from the carbon nTask core";
|
|
|
|
log.error(msg);
|
|
|
|
log.error(msg);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if (!nTaskService.getRegisteredTaskTypes().contains(
|
|
|
|
|
|
|
|
TaskMgtConstants.Task.DYNAMIC_TASK_TYPE)) {
|
|
|
|
|
|
|
|
nTaskService.registerTaskType(TaskMgtConstants.Task.DYNAMIC_TASK_TYPE);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
taskManager = nTaskService.getTaskManager(TaskMgtConstants.Task.DYNAMIC_TASK_TYPE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<DynamicTask> dynamicTasks = TaskWatcherDataHolder.getInstance().getTaskManagementService()
|
|
|
|
List<DynamicTask> dynamicTasks = TaskWatcherDataHolder.getInstance().getTaskManagementService()
|
|
|
|
.getAllDynamicTasks();
|
|
|
|
.getAllDynamicTasks();
|
|
|
|
List<TaskInfo> tasks = taskManager.getAllTasks();
|
|
|
|
Map<Integer, List<DynamicTask>> tenantedDynamicTasks = new HashMap<>();
|
|
|
|
// add or update task into n task core
|
|
|
|
List<DynamicTask> dts;
|
|
|
|
for (DynamicTask dt : dynamicTasks) {
|
|
|
|
for (DynamicTask dt : dynamicTasks) {
|
|
|
|
String generatedTaskId = TaskManagementUtil.generateTaskId(dt.getDynamicTaskId());
|
|
|
|
if (tenantedDynamicTasks.containsKey(dt.getTenantId())) {
|
|
|
|
boolean isExist = false;
|
|
|
|
dts = tenantedDynamicTasks.get(dt.getTenantId());
|
|
|
|
for (TaskInfo taskInfo : tasks) {
|
|
|
|
} else {
|
|
|
|
if (taskInfo.getName().equals(generatedTaskId)) {
|
|
|
|
dts = new ArrayList<>();
|
|
|
|
isExist = true;
|
|
|
|
}
|
|
|
|
TaskInfo.TriggerInfo triggerInfo = taskInfo.getTriggerInfo();
|
|
|
|
dts.add(dt);
|
|
|
|
String dynamicTaskPropMD5 = TaskManagementUtil.generateTaskPropsMD5(dt.getProperties());
|
|
|
|
tenantedDynamicTasks.put(dt.getTenantId(), dts);
|
|
|
|
String existingTaskPropMD5 = TaskManagementUtil.generateTaskPropsMD5(taskInfo.getProperties());
|
|
|
|
}
|
|
|
|
if (!triggerInfo.getCronExpression().equals(dt.getCronExpression())
|
|
|
|
for (Integer tenantId : tenantedDynamicTasks.keySet()) {
|
|
|
|
|| !dynamicTaskPropMD5.equals(existingTaskPropMD5)) {
|
|
|
|
if (tenantId == -1) {
|
|
|
|
triggerInfo.setCronExpression(dt.getCronExpression());
|
|
|
|
log.warn("Found " + tenantedDynamicTasks.get(tenantId).size() +
|
|
|
|
taskInfo.setTriggerInfo(triggerInfo);
|
|
|
|
" invalid tasks without a valid tenant id.");
|
|
|
|
taskInfo.setProperties(dt.getProperties());
|
|
|
|
continue;
|
|
|
|
taskManager.registerTask(taskInfo);
|
|
|
|
}
|
|
|
|
taskManager.rescheduleTask(generatedTaskId);
|
|
|
|
PrivilegedCarbonContext.startTenantFlow();
|
|
|
|
log.debug("Task - '" + generatedTaskId + "' updated according to the dynamic task table");
|
|
|
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true);
|
|
|
|
|
|
|
|
if (!nTaskService.getRegisteredTaskTypes().contains(TaskMgtConstants.Task.DYNAMIC_TASK_TYPE)) {
|
|
|
|
|
|
|
|
nTaskService.registerTaskType(TaskMgtConstants.Task.DYNAMIC_TASK_TYPE);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
taskManager = nTaskService.getTaskManager(TaskMgtConstants.Task.DYNAMIC_TASK_TYPE);
|
|
|
|
|
|
|
|
List<TaskInfo> tasks = taskManager.getAllTasks();
|
|
|
|
|
|
|
|
// add or update task into nTask core
|
|
|
|
|
|
|
|
for (DynamicTask dt : tenantedDynamicTasks.get(tenantId)) {
|
|
|
|
|
|
|
|
String generatedTaskId = TaskManagementUtil.generateTaskId(dt.getDynamicTaskId());
|
|
|
|
|
|
|
|
Map<String, String> taskProperties = dt.getProperties();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
int serverHashIdx = TaskWatcherDataHolder.getInstance().getHeartBeatService()
|
|
|
|
|
|
|
|
.getServerCtxInfo().getLocalServerHashIdx();
|
|
|
|
|
|
|
|
taskProperties.put(TaskMgtConstants.Task.LOCAL_HASH_INDEX, String.valueOf(serverHashIdx));
|
|
|
|
|
|
|
|
taskProperties.put(TaskMgtConstants.Task.LOCAL_TASK_NAME, generatedTaskId);
|
|
|
|
|
|
|
|
} catch (HeartBeatManagementException e) {
|
|
|
|
|
|
|
|
String msg = "Unexpected exception when getting server hash index.";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new TaskManagementException(msg, e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean isExist = false;
|
|
|
|
|
|
|
|
for (TaskInfo taskInfo : tasks) {
|
|
|
|
|
|
|
|
if (taskInfo.getName().equals(generatedTaskId)) {
|
|
|
|
|
|
|
|
isExist = true;
|
|
|
|
|
|
|
|
TaskInfo.TriggerInfo triggerInfo = taskInfo.getTriggerInfo();
|
|
|
|
|
|
|
|
String dynamicTaskPropMD5 = TaskManagementUtil.generateTaskPropsMD5(taskProperties);
|
|
|
|
|
|
|
|
String existingTaskPropMD5 = TaskManagementUtil.generateTaskPropsMD5(taskInfo.getProperties());
|
|
|
|
|
|
|
|
if (!triggerInfo.getCronExpression().equals(dt.getCronExpression())
|
|
|
|
|
|
|
|
|| !dynamicTaskPropMD5.equals(existingTaskPropMD5)) {
|
|
|
|
|
|
|
|
triggerInfo.setCronExpression(dt.getCronExpression());
|
|
|
|
|
|
|
|
taskInfo.setTriggerInfo(triggerInfo);
|
|
|
|
|
|
|
|
taskInfo.setProperties(taskProperties);
|
|
|
|
|
|
|
|
taskManager.registerTask(taskInfo);
|
|
|
|
|
|
|
|
taskManager.rescheduleTask(generatedTaskId);
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("Task - '" + generatedTaskId + "' updated according to the dynamic task table");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dt.isEnabled()
|
|
|
|
|
|
|
|
&& taskManager.getTaskState(generatedTaskId) == TaskManager.TaskState.PAUSED) {
|
|
|
|
|
|
|
|
taskManager.resumeTask(generatedTaskId);
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("Task - '" + generatedTaskId + "' enabled according to the dynamic task table");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (!dt.isEnabled()
|
|
|
|
|
|
|
|
&& taskManager.getTaskState(generatedTaskId) != TaskManager.TaskState.PAUSED) {
|
|
|
|
|
|
|
|
taskManager.pauseTask(generatedTaskId);
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("Task - '" + generatedTaskId + "' disabled according to the dynamic task table");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dt.isEnabled()
|
|
|
|
}
|
|
|
|
&& taskManager.getTaskState(generatedTaskId) == TaskManager.TaskState.PAUSED) {
|
|
|
|
if (!isExist) {
|
|
|
|
taskManager.resumeTask(generatedTaskId);
|
|
|
|
TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo();
|
|
|
|
log.debug("Task - '" + generatedTaskId + "' enabled according to the dynamic task table");
|
|
|
|
triggerInfo.setCronExpression(dt.getCronExpression());
|
|
|
|
} else if (!dt.isEnabled()
|
|
|
|
TaskInfo taskInfo = new TaskInfo(generatedTaskId, dt.getTaskClassName(),
|
|
|
|
&& taskManager.getTaskState(generatedTaskId) != TaskManager.TaskState.PAUSED) {
|
|
|
|
taskProperties, triggerInfo);
|
|
|
|
taskManager.pauseTask(generatedTaskId);
|
|
|
|
taskManager.registerTask(taskInfo);
|
|
|
|
log.debug("Task - '" + generatedTaskId + "' disabled according to the dynamic task table");
|
|
|
|
taskManager.scheduleTask(generatedTaskId);
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("New task -'" + generatedTaskId + "' created according to the dynamic task table");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isExist) {
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo();
|
|
|
|
}
|
|
|
|
triggerInfo.setCronExpression(dt.getCronExpression());
|
|
|
|
|
|
|
|
TaskInfo taskInfo = new TaskInfo(generatedTaskId, dt.getTaskClassName(),
|
|
|
|
List<Tenant> tenants = new ArrayList<>();
|
|
|
|
dt.getProperties(), triggerInfo);
|
|
|
|
try {
|
|
|
|
taskManager.registerTask(taskInfo);
|
|
|
|
RealmService realmService = TaskWatcherDataHolder.getInstance().getRealmService();
|
|
|
|
taskManager.scheduleTask(generatedTaskId);
|
|
|
|
Tenant[] tenantArray = realmService.getTenantManager().getAllTenants();
|
|
|
|
log.debug("New task -'" + generatedTaskId + "' created according to the dynamic task table");
|
|
|
|
if (tenantArray != null && tenantArray.length != 0) {
|
|
|
|
|
|
|
|
tenants.addAll(Arrays.asList(tenantArray));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Tenant superTenant = new Tenant();
|
|
|
|
|
|
|
|
superTenant.setId(-1234);
|
|
|
|
|
|
|
|
tenants.add(superTenant);
|
|
|
|
|
|
|
|
} catch (UserStoreException e) {
|
|
|
|
|
|
|
|
String msg = "Unable to load tenants";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Remove deleted items from the n task core
|
|
|
|
for (Tenant tenant : tenants) {
|
|
|
|
for (TaskInfo taskInfo : tasks) {
|
|
|
|
PrivilegedCarbonContext.startTenantFlow();
|
|
|
|
boolean isExist = false;
|
|
|
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant.getId(), true);
|
|
|
|
for (DynamicTask dt : dynamicTasks) {
|
|
|
|
if (!nTaskService.getRegisteredTaskTypes().contains(TaskMgtConstants.Task.DYNAMIC_TASK_TYPE)) {
|
|
|
|
if (taskInfo.getName().equals(TaskManagementUtil.generateTaskId(dt.getDynamicTaskId()))) {
|
|
|
|
nTaskService.registerTaskType(TaskMgtConstants.Task.DYNAMIC_TASK_TYPE);
|
|
|
|
isExist = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isExist) {
|
|
|
|
taskManager = nTaskService.getTaskManager(TaskMgtConstants.Task.DYNAMIC_TASK_TYPE);
|
|
|
|
taskManager.deleteTask(taskInfo.getName());
|
|
|
|
List<TaskInfo> tasks = taskManager.getAllTasks();
|
|
|
|
log.debug("Task '" + taskInfo.getName() + "' deleted according to the dynamic task table");
|
|
|
|
// Remove deleted items from the nTask core
|
|
|
|
|
|
|
|
for (TaskInfo taskInfo : tasks) {
|
|
|
|
|
|
|
|
boolean isExist = false;
|
|
|
|
|
|
|
|
for (DynamicTask dt : dynamicTasks) {
|
|
|
|
|
|
|
|
if (tenant.getId() == dt.getTenantId() &&
|
|
|
|
|
|
|
|
taskInfo.getName().equals(TaskManagementUtil.generateTaskId(dt.getDynamicTaskId()))) {
|
|
|
|
|
|
|
|
isExist = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isExist) {
|
|
|
|
|
|
|
|
taskManager.deleteTask(taskInfo.getName());
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("Task '" + taskInfo.getName() + "' deleted according to the dynamic task table");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.info("Task Comparison Completed and all tasks in current node are updated");
|
|
|
|
|
|
|
|
} catch (
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
TaskException e) {
|
|
|
|
log.debug("Task Comparison Completed and all tasks in current node are updated");
|
|
|
|
String msg = "Error occurred while accessing carbon n task manager.";
|
|
|
|
}
|
|
|
|
log.error(msg);
|
|
|
|
} catch (TaskException e) {
|
|
|
|
} catch (
|
|
|
|
String msg = "Error occurred while accessing carbon nTask manager.";
|
|
|
|
TaskManagementException e) {
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
} catch (TaskManagementException e) {
|
|
|
|
String msg = "Error occurred while retrieving all active tasks from entgra task manager";
|
|
|
|
String msg = "Error occurred while retrieving all active tasks from entgra task manager";
|
|
|
|
log.error(msg);
|
|
|
|
log.error(msg, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|