Search in sources :

Example 1 with WorkItemFilter

use of com.adobe.granite.workflow.exec.filter.WorkItemFilter in project acs-aem-commons by Adobe-Consulting-Services.

the class WorkflowInstanceRemoverImpl method getStatus.

private String getStatus(Resource workflowInstanceResource) {
    String instanceStatus = workflowInstanceResource.getValueMap().get(PN_STATUS, "UNKNOWN");
    if (!STATUS_RUNNING.equalsIgnoreCase(instanceStatus)) {
        log.debug("Status of [ {} ] is not RUNNING, so we can take it at face value", instanceStatus);
        return instanceStatus;
    }
    // Else check if its RUNNING or STALE
    log.debug("Status is [ {} ] so we have to determine if its RUNNING or STALE", instanceStatus);
    Resource metadataResource = workflowInstanceResource.getChild("data/metaData");
    if (metadataResource == null) {
        log.debug("Workflow instance data/metaData does not exist for [ {} ]", workflowInstanceResource.getPath());
        return instanceStatus;
    }
    final ValueMap properties = metadataResource.getValueMap();
    final String[] jobIds = StringUtils.splitByWholeSeparator(properties.get("currentJobs", ""), JOB_SEPARATOR);
    if (jobIds.length == 0) {
        log.debug("No jobs found for [ {} ] so assuming status as [ {} ]", workflowInstanceResource.getPath(), instanceStatus);
    }
    // Make sure there are no JOBS that match to this jobs name
    for (final String jobId : jobIds) {
        if (jobManager.getJobById(jobId) != null) {
            // Found a job for this jobID; so this is a RUNNING job
            log.debug("JobManager found a job for jobId [ {} ] so marking workflow instances [ {} ] as RUNNING", jobId, workflowInstanceResource.getPath());
            return STATUS_RUNNING;
        }
    }
    log.debug("JobManager could not find any jobs for jobIds [ {} ] so  workflow instance [ {} ] is potentially STALE", StringUtils.join(jobIds, ", "), workflowInstanceResource.getPath());
    final WorkflowSession workflowSession = workflowInstanceResource.getResourceResolver().adaptTo(WorkflowSession.class);
    Workflow workflow = null;
    try {
        workflow = workflowSession.getWorkflow(workflowInstanceResource.getPath());
        if (workflow == null) {
            throw new WorkflowException(String.format("Workflow instance object is null for [ %s]", workflowInstanceResource.getPath()));
        }
    } catch (WorkflowException e) {
        log.warn("Unable to locate Workflow Instance for [ {} ] So it cannot be RUNNING and must be STALE. ", workflowInstanceResource.getPath(), e);
        return "STALE";
    }
    final List<WorkItem> workItems = workflow.getWorkItems(new WorkItemFilter() {

        public boolean doInclude(WorkItem workItem) {
            // Only include active Workflow instances (ones without an End Time) in this list
            return workItem.getTimeEnded() == null;
        }
    });
    if (!workItems.isEmpty()) {
        // If at least 1 work item exists that does not have an end time (meaning its still active), then its RUNNING
        return STATUS_RUNNING;
    }
    return "STALE";
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) WorkflowSession(com.adobe.granite.workflow.WorkflowSession) WorkflowException(com.adobe.granite.workflow.WorkflowException) Resource(org.apache.sling.api.resource.Resource) Workflow(com.adobe.granite.workflow.exec.Workflow) WorkItemFilter(com.adobe.granite.workflow.exec.filter.WorkItemFilter) WorkItem(com.adobe.granite.workflow.exec.WorkItem)

Aggregations

WorkflowException (com.adobe.granite.workflow.WorkflowException)1 WorkflowSession (com.adobe.granite.workflow.WorkflowSession)1 WorkItem (com.adobe.granite.workflow.exec.WorkItem)1 Workflow (com.adobe.granite.workflow.exec.Workflow)1 WorkItemFilter (com.adobe.granite.workflow.exec.filter.WorkItemFilter)1 Resource (org.apache.sling.api.resource.Resource)1 ValueMap (org.apache.sling.api.resource.ValueMap)1