Search in sources :

Example 11 with JobSummaries

use of org.finra.herd.model.api.xml.JobSummaries in project herd by FINRAOS.

the class JobRestControllerTest method testGetJobs.

@Test
public void testGetJobs() throws Exception {
    // Create a job summary.
    JobSummary jobSummary = new JobSummary();
    jobSummary.setId(JOB_ID);
    jobSummary.setNamespace(JOB_NAMESPACE);
    jobSummary.setJobName(JOB_NAME);
    // Create a job summaries object.
    JobSummaries jobSummaries = new JobSummaries(Arrays.asList(jobSummary));
    // Mock the external calls.
    when(jobService.getJobs(JOB_NAMESPACE, JOB_NAME, JobStatusEnum.RUNNING, START_TIME, END_TIME)).thenReturn(jobSummaries);
    // Call the method under test.
    JobSummaries result = jobRestController.getJobs(JOB_NAMESPACE, JOB_NAME, JobStatusEnum.RUNNING, START_TIME, END_TIME);
    // Verify the external calls.
    verify(jobService).getJobs(JOB_NAMESPACE, JOB_NAME, JobStatusEnum.RUNNING, START_TIME, END_TIME);
    verifyNoMoreInteractionsHelper();
    // Validate the results.
    assertEquals(jobSummaries, result);
}
Also used : JobSummary(org.finra.herd.model.api.xml.JobSummary) JobSummaries(org.finra.herd.model.api.xml.JobSummaries) Test(org.junit.Test)

Example 12 with JobSummaries

use of org.finra.herd.model.api.xml.JobSummaries in project herd by FINRAOS.

the class JobServiceImpl method getJobs.

@Override
public JobSummaries getJobs(String namespace, String jobName, JobStatusEnum jobStatus, DateTime startTime, DateTime endTime) throws Exception {
    // Trim the parameters.
    String namespaceTrimmed = namespace == null ? null : namespace.trim();
    String jobNameTrimmed = jobName == null ? null : jobName.trim();
    // Construct the list of job summaries to return.
    JobSummaries jobSummaries = new JobSummaries();
    /*
         * Get the namespaces which the current user is authorized to READ.
         * If a specific namespace was requested, and the current user is authorized to read the namespace, include ONLY the requested namespace.
         * If a specific namespace was requested, but the current user is not authorized to read the namespace, clear all namespaces.
         * Otherwise, include all authorized namespaces.
         *
         * This ensures that only authorized namespaces are queried from the database and that
         * an unauthorized user cannot determine if he specified an existing namespace or not.
         */
    Set<String> authorizedNamespaces = namespaceSecurityHelper.getAuthorizedNamespaces(NamespacePermissionEnum.READ);
    if (StringUtils.isNotBlank(namespaceTrimmed)) {
        NamespaceEntity namespaceEntity = namespaceDao.getNamespaceByCd(namespaceTrimmed);
        if (namespaceEntity != null && authorizedNamespaces.contains(namespaceEntity.getCode())) {
            authorizedNamespaces.clear();
            authorizedNamespaces.add(namespaceEntity.getCode());
        } else {
            authorizedNamespaces.clear();
        }
    }
    // Continue the processing only when the list of authorized namespaces is not empty.
    if (!authorizedNamespaces.isEmpty()) {
        // Build a set of Activiti process definition keys for each job definition that was filtered.
        Set<String> processDefinitionIds = new HashSet<>();
        for (JobDefinitionEntity jobDefinitionEntity : jobDefinitionDao.getJobDefinitionsByFilter(authorizedNamespaces, jobNameTrimmed)) {
            processDefinitionIds.add(jobDefinitionEntity.getActivitiId());
        }
        // Continue the processing only when the list of filtered job definitions is not empty.
        if (!processDefinitionIds.isEmpty()) {
            /*
                 * Keep a mapping of definition ID to its key.
                 * Activiti API does not provide an easy way to get the key without doing extra queries for each definition.
                 */
            Map<String, String> processDefinitionIdToKeyMap = new HashMap<>();
            for (ProcessDefinition processDefinition : activitiService.getProcessDefinitionsByIds(processDefinitionIds)) {
                processDefinitionIdToKeyMap.put(processDefinition.getId(), processDefinition.getKey());
            }
            List<HistoricProcessInstance> historicProcessInstances = getHistoricProcessInstances(processDefinitionIdToKeyMap.values(), jobStatus, startTime, endTime);
            // Get a set of all runtime suspended process instance ids.
            Set<String> suspendedProcessInstanceIds = getSuspendedProcessInstanceIds();
            // Compile the Regex pattern.
            Pattern pattern = jobDefinitionHelper.getNamespaceAndJobNameRegexPattern();
            // Loop over the process instances and build a list of job summaries to return.
            for (HistoricProcessInstance historicProcessInstance : historicProcessInstances) {
                // Exclude all process instances started using older versions of the process definition.
                if (processDefinitionIdToKeyMap.containsKey(historicProcessInstance.getProcessDefinitionId())) {
                    // Set a flag if this job is SUSPENDED.
                    boolean suspended = suspendedProcessInstanceIds.contains(historicProcessInstance.getId());
                    // getHistoricProcessInstances() returns both RUNNING and SUSPENDED process instances.
                    if (!(JobStatusEnum.SUSPENDED.equals(jobStatus) && !suspended) && !(JobStatusEnum.RUNNING.equals(jobStatus) && suspended)) {
                        // Create a new job summary.
                        JobSummary jobSummary = new JobSummary();
                        jobSummary.setId(historicProcessInstance.getId());
                        // Get the job definition key.
                        JobDefinitionAlternateKeyDto jobDefinitionKey = jobDefinitionHelper.getJobDefinitionKey(processDefinitionIdToKeyMap.get(historicProcessInstance.getProcessDefinitionId()), pattern);
                        // Set the namespace and job name on the job summary.
                        jobSummary.setNamespace(jobDefinitionKey.getNamespace());
                        jobSummary.setJobName(jobDefinitionKey.getJobName());
                        // Set the start time always since all jobs will have a start time.
                        jobSummary.setStartTime(HerdDateUtils.getXMLGregorianCalendarValue(historicProcessInstance.getStartTime()));
                        if (historicProcessInstance.getEndTime() == null) {
                            // Since there is no end time, the job is running or suspended.
                            jobSummary.setStatus(suspended ? JobStatusEnum.SUSPENDED : JobStatusEnum.RUNNING);
                            // If the end time is null, then determine the status based on the presence of any exceptions.
                            jobSummary.setTotalExceptions(activitiService.getJobsWithExceptionCountByProcessInstanceId(historicProcessInstance.getId()));
                        } else {
                            // If the end time is set, then the job has finished so set the end time and the status to completed.
                            jobSummary.setEndTime(HerdDateUtils.getXMLGregorianCalendarValue(historicProcessInstance.getEndTime()));
                            jobSummary.setStatus(JobStatusEnum.COMPLETED);
                        }
                        // Add the new summary to the list.
                        jobSummaries.getJobSummaries().add(jobSummary);
                    }
                }
            }
        }
    }
    // Return the list of job summaries.
    return jobSummaries;
}
Also used : Pattern(java.util.regex.Pattern) NamespaceEntity(org.finra.herd.model.jpa.NamespaceEntity) HashMap(java.util.HashMap) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) JobSummaries(org.finra.herd.model.api.xml.JobSummaries) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) JobSummary(org.finra.herd.model.api.xml.JobSummary) JobDefinitionEntity(org.finra.herd.model.jpa.JobDefinitionEntity) JobDefinitionAlternateKeyDto(org.finra.herd.model.dto.JobDefinitionAlternateKeyDto) HashSet(java.util.HashSet)

Aggregations

JobSummaries (org.finra.herd.model.api.xml.JobSummaries)12 Test (org.junit.Test)11 JobStatusEnum (org.finra.herd.model.api.xml.JobStatusEnum)10 JobSummary (org.finra.herd.model.api.xml.JobSummary)7 HashSet (java.util.HashSet)6 Task (org.activiti.engine.task.Task)5 Job (org.finra.herd.model.api.xml.Job)5 NamespaceEntity (org.finra.herd.model.jpa.NamespaceEntity)5 Collection (java.util.Collection)4 HistoricProcessInstance (org.activiti.engine.history.HistoricProcessInstance)4 ProcessDefinition (org.activiti.engine.repository.ProcessDefinition)4 JobDefinitionEntity (org.finra.herd.model.jpa.JobDefinitionEntity)4 DateTime (org.joda.time.DateTime)4 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 HistoricProcessInstanceEntity (org.activiti.engine.impl.persistence.entity.HistoricProcessInstanceEntity)3 HashMap (java.util.HashMap)1 Pattern (java.util.regex.Pattern)1 JobUpdateRequest (org.finra.herd.model.api.xml.JobUpdateRequest)1 JobDefinitionAlternateKeyDto (org.finra.herd.model.dto.JobDefinitionAlternateKeyDto)1