Skip to content

Commit

Permalink
Added older-than milliseconds feature to workflowremover to be able t…
Browse files Browse the repository at this point in the history
…o remove Workflows older than a defined time period.
  • Loading branch information
cornsen committed May 5, 2021
1 parent ff750e9 commit 55f938e
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* #%L
* ACS AEM Commons Bundle
* %%
* Copyright (C) 2017 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.mcp.form;

import org.osgi.annotation.versioning.ProviderType;

/**
* date picker field component
*/
@ProviderType
public class NumberfieldComponent extends FieldComponent {
public NumberfieldComponent(){
setResourceType("granite/ui/components/coral/foundation/form/numberfield");
}

@Override
public void init() {
// Nothing special happens here
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* Form components for MCP
*/
@Version("5.2.0")
@Version("5.3.0")
package com.adobe.acs.commons.mcp.form;

import org.osgi.annotation.versioning.Version;
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package com.adobe.acs.commons.mcp.impl.processes;

import com.adobe.acs.commons.mcp.form.NumberfieldComponent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -70,6 +71,10 @@ public class WorkflowRemover extends ProcessDefinition {
component = DatePickerComponent.class)
public String olderThanVal;

@FormField(name = "Workflows Older Than Milliseconds", description = "only remove workflows that were started longer than the specified milliseconds ago",
component = NumberfieldComponent.class)
public long olderThanMillis;

@FormField(
name = "Workflow Models",
description = "If no Workflow Models are selected, Workflow Instances will not be filtered by Workflow Model.",
Expand Down Expand Up @@ -125,7 +130,7 @@ void performCleanupActivity(ActionManager manager) throws Exception {

parseParameters();

workflowInstanceRemover.removeWorkflowInstances(rr, modelIds, statuses, payloads, olderThan, BATCH_SIZE,
workflowInstanceRemover.removeWorkflowInstances(rr, modelIds, statuses, payloads, olderThan, olderThanMillis, BATCH_SIZE,
MAX_DURATION_MINS);

WorkflowRemovalStatus status = workflowInstanceRemover.getStatus();
Expand Down Expand Up @@ -187,6 +192,10 @@ public Calendar getOlderThan() {
return olderThan;
}

public long getOlderThanMillis() {
return olderThanMillis;
}

public List<String> getStatuses() {
return statuses;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ public interface WorkflowInstanceRemover {
* @param statuses WF Statuses to remove
* @param payloads Regexes; WF Payloads to remove
* @param olderThan UTC time in milliseconds; only delete WF's started after this time
* @param olderThanMillis Milliseconds; only delete WF's started after this milliseconds ago
* @return the number of WF instances removed
*/
int removeWorkflowInstances(final ResourceResolver resourceResolver,
final Collection<String> modelIds,
final Collection<String> statuses,
final Collection<Pattern> payloads,
final Calendar olderThan) throws PersistenceException, WorkflowRemovalException, InterruptedException, WorkflowRemovalForceQuitException;
final Calendar olderThan,
final long olderThanMillis) throws PersistenceException, WorkflowRemovalException, InterruptedException, WorkflowRemovalForceQuitException;


/**
Expand All @@ -69,6 +71,7 @@ int removeWorkflowInstances(final ResourceResolver resourceResolver,
* @param statuses WF Statuses to remove
* @param payloads Regexes; WF Payloads to remove
* @param olderThan UTC time in milliseconds; only delete WF's started after this time
* @param olderThanMillis Milliseconds; only delete WF's started after this milliseconds ago
* @param batchSize number of workflow instances to delete per JCR save
* @return the number of WF instances removed
*/
Expand All @@ -77,6 +80,7 @@ int removeWorkflowInstances(final ResourceResolver resourceResolver,
final Collection<String> statuses,
final Collection<Pattern> payloads,
final Calendar olderThan,
final long olderThanMillis,
final int batchSize) throws PersistenceException, WorkflowRemovalException, InterruptedException, WorkflowRemovalForceQuitException;


Expand All @@ -88,6 +92,7 @@ int removeWorkflowInstances(final ResourceResolver resourceResolver,
* @param statuses WF Statuses to remove
* @param payloads Regexes; WF Payloads to remove
* @param olderThan UTC time in milliseconds; only delete WF's started after this time
* @param olderThanMillis Milliseconds; only delete WF's started after this milliseconds ago
* @param batchSize number of workflow instances to delete per JCR save
* @param maxDurationInMins max number of mins the workflow removal process is allowed to run
* @return the number of WF instances removed
Expand All @@ -97,6 +102,7 @@ int removeWorkflowInstances(final ResourceResolver resourceResolver,
final Collection<String> statuses,
final Collection<Pattern> payloads,
final Calendar olderThan,
final long olderThanMillis,
final int batchSize,
final int maxDurationInMins) throws PersistenceException, WorkflowRemovalException,
InterruptedException, WorkflowRemovalForceQuitException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,10 @@ public int removeWorkflowInstances(final ResourceResolver resourceResolver,
final Collection<String> modelIds,
final Collection<String> statuses,
final Collection<Pattern> payloads,
final Calendar olderThan)
final Calendar olderThan,
final long olderThanMillis)
throws PersistenceException, WorkflowRemovalException, InterruptedException, WorkflowRemovalForceQuitException {
return removeWorkflowInstances(resourceResolver, modelIds, statuses, payloads, olderThan, BATCH_SIZE);
return removeWorkflowInstances(resourceResolver, modelIds, statuses, payloads, olderThan, olderThanMillis, BATCH_SIZE);
}

/**
Expand All @@ -207,9 +208,10 @@ public int removeWorkflowInstances(final ResourceResolver resourceResolver,
final Collection<String> statuses,
final Collection<Pattern> payloads,
final Calendar olderThan,
final long olderThanMillis,
final int batchSize)
throws PersistenceException, WorkflowRemovalException, InterruptedException, WorkflowRemovalForceQuitException {
return removeWorkflowInstances(resourceResolver, modelIds, statuses, payloads, olderThan, batchSize, -1);
return removeWorkflowInstances(resourceResolver, modelIds, statuses, payloads, olderThan, olderThanMillis, batchSize, -1);
}

/**
Expand All @@ -221,6 +223,7 @@ public int removeWorkflowInstances(final ResourceResolver resourceResolver,
final Collection<String> statuses,
final Collection<Pattern> payloads,
final Calendar olderThan,
final long olderThanMillis,
final int batchSize,
final int maxDurationInMins)
throws PersistenceException, WorkflowRemovalException, InterruptedException, WorkflowRemovalForceQuitException {
Expand Down Expand Up @@ -280,6 +283,7 @@ public int removeWorkflowInstances(final ResourceResolver resourceResolver,
final String model = properties.get(PN_MODEL_ID, String.class);
final Calendar startTime = properties.get(PN_START_TIME, properties.get(PN_STARTED_AT, Calendar.class));
final String payload = properties.get(PAYLOAD_PATH, String.class);
final long startTimeDelta = start - olderThanMillis;

if (StringUtils.isBlank(payload)) {
log.warn("Unable to find payload for Workflow instance [ {} ]", instance.getPath());
Expand All @@ -298,6 +302,11 @@ public int removeWorkflowInstances(final ResourceResolver resourceResolver,
startTime);
remaining++;
continue;
} else if (olderThanMillis > 0 && startTime != null && start - olderThanMillis < startTime.getTimeInMillis()) {
log.trace("Workflow instance [ {} ] has non-matching start time delta of [ {} ]ms", instance.getPath(),
olderThanMillis);
remaining++;
continue;
} else {

if (CollectionUtils.isNotEmpty(payloads)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ public class WorkflowInstanceRemoverScheduler implements Runnable {
public static final String PROP_WORKFLOWS_OLDER_THAN = "workflow.older-than";


private long olderThanMillis = 0L;

@Property(label = "Older Than Milliseconds",
description = "Only remove Workflow Instances whose payloads start date was at least desired Milliseconds ago",
longValue = 0)
public static final String PROP_WORKFLOWS_OLDER_THAN_MILLIS = "workflow.older-than-millis";

private static final int DEFAULT_BATCH_SIZE = 1000;
private int batchSize = DEFAULT_BATCH_SIZE;
@Property(label = "Batch Size",
Expand Down Expand Up @@ -162,7 +169,8 @@ public final void run() {
models,
statuses,
payloads,
olderThan,
olderThan,
olderThanMillis,
batchSize,
maxDuration);

Expand Down Expand Up @@ -220,6 +228,8 @@ protected final void activate(final Map<String, String> config) {
olderThan = Calendar.getInstance();
olderThan.setTimeInMillis(olderThanTs);
}

olderThanMillis = PropertiesUtil.toLong(config.get(PROP_WORKFLOWS_OLDER_THAN_MILLIS), 0);

batchSize = PropertiesUtil.toInteger(config.get(PROP_BATCH_SIZE), DEFAULT_BATCH_SIZE);
if (batchSize < 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
/**
* Bulk Workflow Removal.
*/
@org.osgi.annotation.versioning.Version("3.1.0")
@org.osgi.annotation.versioning.Version("4.0.0")
package com.adobe.acs.commons.workflow.bulk.removal;
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public void performCleanupActivity() throws Exception {
remover.performCleanupActivity(actionManager);

Mockito.verify(workflowInstanceRemover, Mockito.times(1)).removeWorkflowInstances(Mockito.eq(ctx.resourceResolver()),
Mockito.eq(remover.getModelIds()), Mockito.eq(remover.getStatuses()), Mockito.eq(remover.getPayloads()), Mockito.eq(remover.getOlderThan()),
Mockito.eq(remover.getModelIds()), Mockito.eq(remover.getStatuses()), Mockito.eq(remover.getPayloads()), Mockito.eq(remover.getOlderThan()), Mockito.eq(remover.getOlderThanMillis()),
Mockito.anyInt(), Mockito.anyInt());

}
Expand Down

0 comments on commit 55f938e

Please sign in to comment.