You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

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.

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;
        }
        
        //System.out.println("id is [" + id + "] date is [" + startedTime + "]");
        //System.out.println( "compare " + cutOffDate.compareTo(startedTime));
        //System.out.println( "compare2 " + startedTime.compareTo(cutOffDate));
    }
    //uncomment below to run only once for testing
    //newerThanCutOffFound = true;
}

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

You may modify lines 13 to 18 if you would like to filter according to certain conditions.

You may also modify line 24 and change how many months of latest process instances you would like to retain.

Sample Server Log
INFO  22 Mar 2022 14:23:54 Purge Old Process Data  - Searching for completed process instances older than Wed Dec 22 00:00:00 MYT 2021
INFO  22 Mar 2022 14:23:54 Purge Old Process Data - Found [68_ap_auditInitial_approver_process] with start time [Fri Aug 31 02:15:14 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [69_ap_auditInitial_approver_process] with start time [Fri Aug 31 02:17:24 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [70_ap_auditInitial_approver_process] with start time [Fri Aug 31 02:22:06 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [74_ap_auditInitial_approver_process] with start time [Fri Aug 31 02:25:35 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [75_ap_actionPlanCreationProcess] with start time [Fri Aug 31 02:26:06 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [76_ap_actionPlanCreationProcess] with start time [Fri Aug 31 02:26:07 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [77_ap_auditInitial_approver_process] with start time [Fri Aug 31 03:12:59 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [78_ap_actionPlanCreationProcess] with start time [Fri Aug 31 03:13:52 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [79_ap_actionPlanCreationProcess] with start time [Fri Aug 31 03:13:52 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [82_ap_auditInitial_approver_process] with start time [Fri Aug 31 03:27:29 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [83_ap_actionPlanCreationProcess] with start time [Fri Aug 31 03:28:04 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [84_ap_actionPlanCreationProcess] with start time [Fri Aug 31 03:28:05 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [86_ap_auditInitial_approver_process] with start time [Fri Aug 31 04:12:40 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [87_ap_actionPlanCreationProcess] with start time [Fri Aug 31 04:13:11 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [88_ap_actionPlanCreationProcess] with start time [Fri Aug 31 04:13:11 MYT 2018]
INFO  22 Mar 2022 14:23:56 Purge Old Process Data  - Completed with [15] removed: [68_ap_auditInitial_approver_process, 69_ap_auditInitial_approver_process, 70_ap_auditInitial_approver_process, 74_ap_auditInitial_approver_process, 75_ap_actionPlanCreationProcess, 76_ap_actionPlanCreationProcess, 77_ap_auditInitial_approver_process, 78_ap_actionPlanCreationProcess, 79_ap_actionPlanCreationProcess, 82_ap_auditInitial_approver_process, 83_ap_actionPlanCreationProcess, 84_ap_actionPlanCreationProcess, 86_ap_auditInitial_approver_process, 87_ap_actionPlanCreationProcess, 88_ap_actionPlanCreationProcess]
  • No labels