Skip to content

Commit

Permalink
Merge pull request #427 from davidjgonzalez/defect/bulk-workflow-mana…
Browse files Browse the repository at this point in the history
…ger-auto-resume

425-bulk workflow manager - auto-resume enhancements
  • Loading branch information
davidjgonzalez committed Mar 8, 2015
2 parents b81e04a + a7cf966 commit 3ca4846
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,26 @@

@Component(
label = "ACS AEM Commons - Bulk Workflow Engine",
metatype = true,
immediate = true
)
@Service
public class BulkWorkflowEngineImpl implements BulkWorkflowEngine {
private static final Logger log = LoggerFactory.getLogger(BulkWorkflowEngineImpl.class);

private static final String BULK_WORKFLOW_MANAGER_PAGE_FOLDER_PATH = "/etc/acs-commons/bulk-workflow-manager";

private static final int SAVE_THRESHOLD = 1000;

private static final boolean DEFAULT_AUTO_RESUME = false;
private static final boolean DEFAULT_AUTO_RESUME = true;
private boolean autoResume = DEFAULT_AUTO_RESUME;
@Property(label = "Auto-Resume",
description = "Stopping the ACS AEM Commons bundle will stop any executing Bulk Workflow processing. "
+ "By default when the bundle starts, it will not attempt to auto-resume stopped via deactivation bulk workflow jobs "
+ "as the query for this may be expensive in very large repositories. ",
+ "When auto-resume is enabled, it will attempt to resume 'stopped via deactivation' bulk workflow jobs "
+ "under " + BULK_WORKFLOW_MANAGER_PAGE_FOLDER_PATH + ". [ Default: true ] ",
boolValue = DEFAULT_AUTO_RESUME)
public static final String PROP_AUTO_RESUME = "auto-resume";

@Reference
private WorkflowService workflowService;

Expand Down Expand Up @@ -332,7 +335,7 @@ public final void stop(final Resource resource) throws PersistenceException {

/**
* Stops the bulk workflow process using the OSGi Component deactivated stop state.
* <p>
* <p/>
* Allows the system to know to resume this when the OSGi Component is activated.
*
* @param resource the jcr:content configuration resource
Expand Down Expand Up @@ -485,7 +488,7 @@ private Map<String, String> process(final Resource resource, String workflowMode

/**
* Advance to the next batch and update all properties on the current and next batch nodes accordingly.
* <p>
* <p/>
* This method assumes the current batch has been verified as complete.
*
* @param resource the bulk workflow manager content resource
Expand Down Expand Up @@ -711,25 +714,26 @@ protected final void activate(final Map<String, String> config) {
this.autoResume = PropertiesUtil.toBoolean(config.get(PROP_AUTO_RESUME), DEFAULT_AUTO_RESUME);

if (this.autoResume) {
log.info("Looking for any Bulk Workflow Manager pages to resume processing.");
log.info("Looking for any Bulk Workflow Manager pages to resume processing under: {}", BULK_WORKFLOW_MANAGER_PAGE_FOLDER_PATH);

ResourceResolver adminResourceResolver = null;

try {
adminResourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);
final Resource root = adminResourceResolver.getResource(BULK_WORKFLOW_MANAGER_PAGE_FOLDER_PATH);

final String query = "SELECT * FROM [cq:PageContent] WHERE [sling:resourceType] = "
+ "'" + SLING_RESOURCE_TYPE + "'"
+ " AND [" + KEY_STATE + "] = '" + STATE_STOPPED_DEACTIVATED + "'";
if (root != null) {
final ResumableResourceVisitor visitor = new ResumableResourceVisitor();
visitor.accept(root);

log.debug("Finding bulk work flows to reactivate using query: {}", query);
final List<Resource> resources = visitor.getResumableResources();

final Iterator<Resource> resources = adminResourceResolver.findResources(query, Query.JCR_SQL2);
log.debug("Found {} resumable resource(s)", resources.size());

while (resources.hasNext()) {
final Resource resource = resources.next();
log.info("Automatically resuming bulk workflow at [ {} ]", resource.getPath());
this.resume(resource);
for (final Resource resource : resources) {
log.info("Automatically resuming bulk workflow at [ {} ]", resource.getPath());
this.resume(resource);
}
}
} catch (LoginException e) {
log.error("Could not obtain resource resolver for finding stopped Bulk Workflow jobs", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* #%L
* ACS AEM Commons Bundle
* %%
* Copyright (C) 2015 Adobe
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

package com.adobe.acs.commons.workflow.bulk.impl;

import com.adobe.acs.commons.workflow.bulk.BulkWorkflowEngine;
import com.day.cq.commons.jcr.JcrConstants;
import com.day.cq.wcm.api.NameConstants;
import org.apache.commons.lang.ArrayUtils;
import org.apache.sling.api.SlingConstants;
import org.apache.sling.api.resource.AbstractResourceVisitor;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.jcr.resource.JcrResourceConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class ResumableResourceVisitor extends AbstractResourceVisitor {
private static final Logger log = LoggerFactory.getLogger(BulkWorkflowEngineImpl.class);

private static final String BULK_WORKFLOW_MANAGER_PAGE_FOLDER_PATH = "/etc/acs-commons/bulk-workflow-manager";
private static final String NT_PAGE_CONTENT = "cq:PageContent";
private static final String[] ACCEPTED_PRIMARY_TYPES = new String[] { NameConstants.NT_PAGE, NT_PAGE_CONTENT };
private List<Resource> resources = new ArrayList<Resource>();

public final List<Resource> getResumableResources() {
return this.resources;
}

@Override
public final void accept(final Resource resource) {
// Only accept the Root folder and cq:Page and cq:PageContent nodes; All other structures are uninteresting
// to this functionality and may be very large
final ValueMap properties = resource.adaptTo(ValueMap.class);
final String primaryType = properties.get(JcrConstants.JCR_PRIMARYTYPE, String.class);

if (BULK_WORKFLOW_MANAGER_PAGE_FOLDER_PATH.equals(resource.getPath())) {
super.accept(resource);
} else if (ArrayUtils.contains(ACCEPTED_PRIMARY_TYPES, primaryType)) {
super.accept(resource);
}
}

@Override
protected void visit(final Resource resource) {
final ValueMap properties = resource.adaptTo(ValueMap.class);

// Ensure jcr:primaryType = cq:PageContent
if(NT_PAGE_CONTENT.equals(properties.get(JcrConstants.JCR_PRIMARYTYPE, String.class))) {
// Ensure the sling:resourceType is that of Bulk Workflow Manager Page
if(BulkWorkflowEngine.SLING_RESOURCE_TYPE.equals(properties.get(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, String.class))) {
// Ensure the Bulk Workflow Manager Page has been marked as "Stopped by Bundle Deactivation"
if(BulkWorkflowEngine.STATE_STOPPED_DEACTIVATED.equals(properties.get(BulkWorkflowEngine.KEY_STATE, String.class))) {
this.resources.add(resource);
}
}
}

return;
}
}

0 comments on commit 3ca4846

Please sign in to comment.