Purging workflow after couple minutes

arkadiuszc

26-03-2020

I have a Workflow step which if meet issues throw `WorkflowException` with a message and stacktrace, in effect - blocks whole workflow launcher with the payload. Finally, the workflow is indefinitely in the `RUNNING` state and does not handle any updates for blocked payload. This situation requires admin action to manually terminate the workflow.

There is how the simple workflow looks:

 

@Properties({
        (name = Constants.SERVICE_DESCRIPTION, value = "Workflow"),
        (name = "process.label", value = "Workflow Step") })
public class WorkflowStep implements WorkflowProcess {
    
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap)throws WorkflowException {       
        try {
            ... doing some stuff ...
        } catch (Exception e) {
            throw new WorkflowException(e.getMessage(), e);
        }
    }
}

 

 

I want to check after i.e 2 minutes if the workflow is `COMPLETED` if not - terminate them to unblock the payload and after upload the next asset into this path - again handle by the workflow.

Any idea how to solve it without using `CRON Scheduler`?

AEM workflow

Accepted Solutions (1)

Accepted Solutions (1)

vanegi

Employee

14-07-2020

We have a built in workflow purge feature enabled OOTB. You can create two configurations of the service to purge workflow instances that satisfy different criteria’s.

1:  First configuration:  purges instances of a particular workflow model which are running for longer duration than expected.

2:  Second configuration: purges all completed workflows after a certain number of days to minimize the size of repository

 

Create a Workflow Purge Scheduler instance. The Workflow Purge Scheduler is a Sling Service Factory, configuration added to service PID:  com.adobe.granite.workflow.purge.Scheduler

 

  •    As service is a factory service, the name of the sling:OsgiConfig node requires identifier suffix like:   com.adobe.granite.workflow.purge.Scheduler-myidentifier

    Add below properties on the node:

 

Property Name

OSGi Property Name

Description

Job Name

scheduledpurge.name

A descriptive name for the scheduled purge.

Workflow Status

scheduledpurge.workflowStatus

The status of the workflow instances to purge. The following values are valid:

  • COMPLETED: Completed workflow instances are purged.
  • RUNNING: Running workflow instances are purged.

Models To Purge

scheduledpurge.modelIds

The ID of the workflow models to purge. The ID is the path to the model node, for example /etc/workflow/models/dam/update_asset/jcr:content/model. Specify no value to purge instances of all workflow models.

To specify multiple models, click the + button in the Web Console. 

Workflow Age

scheduledpurge.daysold

The age of the workflow instances to purge, in days.

 

Once we deploy this file as a content package, we should see configuration show up under the Workflow Purge Scheduler in the OSGi console like below & it will purge workflows which are older than the specified number of days mentioned in configuration.

 

 

Configure Workflow Purge Scheduler in a Package

Deploying a configuration as a part of package deployment process is also one of best approach.  Through the Apache Sling's OSGi Configurations, we can do this with below simple XML file.

·         Create a XML file under a path Eg:  /apps/[my-app]/config

·         Set name of the file to: com.adobe.granite.workflow.purge.Scheduler.config.[some-arbitrary-id].xml

 

Add below content to XML file and replace values with respective configuration details:

<?xml version="1.0" encoding="UTF-8"?> <jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0"  xmlns:jcr=http://www.jcp.org/jcr/1.0jcr:primaryType="sling:OsgiConfig" scheduledpurge.name="Purge All Completed Workflows"scheduledpurge.modelIds="[]" scheduledpurge.workflowStatus="COMPLETED" scheduledpurge.cron="0 0 * * * ?" scheduledpurge.daysold="30" />

 

Once you click save, the scheduler schedules CRON schedule and will purge workflows older than the specified number of days within configuration we specified.

 

You can also use JMX console to purge repository of completed workflows.

If list of archived workflows grows too large, purge those that are of certain age limit this speeds up page loading.

Answers (0)