Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run suspension task from master-node. #890

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.wso2.carbon.identity.account.suspension.notification.task;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
Expand Down Expand Up @@ -88,6 +89,11 @@ private void handleTask(String tenantDomain) {
log.debug("Handling idle account suspension task for tenant: " + tenantDomain);
}

// Run the task only from master node in cluster setup.
if (isMasterNodeExclusiveExecutionEnabled() && !isHazelcastMasterNode()) {
return;
}

Property[] identityProperties;
try {
// Start Tenant flow
Expand Down Expand Up @@ -167,6 +173,27 @@ private void handleTask(String tenantDomain) {
}
}

/**
* Check whether suspension task configured to run only in master node or not.
*
* @return true or false based on the deployment config.
*/
private boolean isMasterNodeExclusiveExecutionEnabled() {

String clusterModeEnabledValue = IdentityUtil.getProperty(NotificationConstants.EXECUTE_TASK_IN_MASTER_NODE);
return StringUtils.isNotBlank(clusterModeEnabledValue) ? Boolean.parseBoolean(clusterModeEnabledValue) : false;
}

/**
* Check whether current node is master node in the Hazelcast cluster.
*
* @return true or false based on the Hazelcast cluster.
*/
private boolean isHazelcastMasterNode() {

return NotificationTaskDataHolder.getInstance().getClusteringAgent().isCoordinator();
}

/**
* Notify users about account inactivity via Email.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.wso2.carbon.identity.account.suspension.notification.task.internal;

import org.apache.axis2.clustering.ClusteringAgent;
import org.osgi.framework.BundleContext;
import org.wso2.carbon.identity.account.suspension.notification.task.NotificationReceiversRetrievalFactory;
import org.wso2.carbon.identity.account.suspension.notification.task.util.NotificationConstants;
Expand Down Expand Up @@ -43,6 +44,7 @@ public class NotificationTaskDataHolder {
private String notificationTriggerTime;
private String schedulerDelay;
private String notificationSendingThreadPoolSize = "1";
private ClusteringAgent clusteringAgent;

public int getNotificationSendingThreadPoolSize() {
return Integer.parseInt(notificationSendingThreadPoolSize);
Expand Down Expand Up @@ -112,4 +114,14 @@ public void setRealmService(RealmService realmService) {
public RealmService getRealmService() {
return realmService;
}

public ClusteringAgent getClusteringAgent() {

return clusteringAgent;
}

public void setClusteringAgent(ClusteringAgent clusteringAgent) {

this.clusteringAgent = clusteringAgent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.wso2.carbon.utils.ConfigurationContextService;

/**
* Notification scheduler. Check for users who requires a notification for relogin
Expand Down Expand Up @@ -156,5 +157,20 @@ protected void unsetRealmService(RealmService realmService) {
log.debug("RealmService is unset in the Application Authentication Framework bundle");
}
}

@Reference(
name = "config.context.service",
cardinality = ReferenceCardinality.OPTIONAL,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetClusteringAgent")
protected void setClusteringAgent(ConfigurationContextService configurationContextService) {

NotificationTaskDataHolder.getInstance().setClusteringAgent(
inthirakumaaran marked this conversation as resolved.
Show resolved Hide resolved
configurationContextService.getServerConfigContext().getAxisConfiguration().getClusteringAgent());
}

protected void unsetClusteringAgent(ConfigurationContextService configurationContextService) {
NotificationTaskDataHolder.getInstance().setClusteringAgent(null);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class NotificationConstants {
public static final String SUSPENSION_NOTIFICATION_TRIGGER_TIME= "suspension.notification.trigger.time";
public static final String SUSPENSION_NOTIFICATION_DELAYS="suspension.notification.delays";
public static final String USE_IDENTITY_CLAIM_FOR_LAST_LOGIN_TIME = "AccountSuspension.UseIdentityClaims";
public static final String EXECUTE_TASK_IN_MASTER_NODE = "AccountSuspension.ExecuteTaskOnMasterNode";
public static final String TRIGGER_TIME_FORMAT = "HH:mm:ss";
public static final long SCHEDULER_DELAY = 24; // In hours
public static final String SUSPENSION_NOTIFICATION_THREAD_POOL_SIZE = "suspension.notification.thread.pool.size";
Expand Down
Loading