Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


English

By putting the following script into Bean Shell tool, and into a scheduler job or any process tool, we can automate the deletion of completed process instance data to keep server performance optimal.

The following script would pick up the process instances older than 12 months.


Code Block
languagejava
linenumberstrue
import org.joget.workflow.model.service.WorkflowManager;
import org.joget.workflow.model.WorkflowProcess;
import org.joget.apps.app.service.AppService;
import org.joget.apps.app.service.AppUtil;
import org.joget.commons.util.LogUtil;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

AppService appService = (AppService) AppUtil.getApplicationContext().getBean("appService");
WorkflowManager workflowManager = (WorkflowManager) AppUtil.getApplicationContext().getBean("workflowManager");

String appId = null; //to filter by specific appId
String processId = null; //to filter by specific processId
String processName = null;
String version = null;
String recordId = null;
String requester = null;
String sort = "Started";
boolean desc = false; //find oldest first
int start = 0;
int rows = 15;

int months = 12; //retain last X months data AKA remove data older than X months
Date cutOffDate = Date.from(LocalDate.now().minusMonths(months).atStartOfDay(ZoneId.systemDefault()).toInstant());

int removedCount = 0;
Collection removedPids = new ArrayList();

LogUtil.info("Purge Old Completed Process Data ", "Searching for completed process instances older than " + cutOffDate.toString());

boolean newerThanCutOffFound = false;

while(!newerThanCutOffFound){
    //get 15 oldest records
    Collection processList = workflowManager.getCompletedProcessList(appId, processId, processName, version, recordId, requester, sort, desc, start, rows);
    for (WorkflowProcess workflowProcess : processList) {
        String id = workflowProcess.getInstanceId();
        
        Date startedTime = workflowProcess.getStartedTime();
        
        if( cutOffDate.compareTo(startedTime) == 1){
            //startedTime is older than cutOffDate, remove process data
            
            LogUtil.info("Purge Old Process Data", "Found [" + id + "] with start time [" + startedTime + "]");
            removedCount++;
            removedPids.add(id);
            appService.getAppDefinitionForWorkflowProcess(id);
            workflowManager.removeProcessInstance(id);
        }else{
            newerThanCutOffFound = true;
            break;
        }
        
    }
    //uncomment below to run only once for testing
    //newerThanCutOffFound = true;
}

LogUtil.info("Purge Old Completed Process Data ", "Completed with [" + removedCount + "] removed: " + removedPids.toString());

...