use of com.adobe.granite.workflow.exec.WorkItem 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";
}
Aggregations