Search in sources :

Example 1 with JobRequest

use of org.pentaho.platform.web.http.api.resources.JobRequest in project pentaho-platform by pentaho.

the class SolutionImportHandler method createSchedulerJob.

// handlers that extend this class may override this method and perform operations
// over the job prior to its creation at scheduler.createJob()
public Response createSchedulerJob(SchedulerResource scheduler, JobScheduleRequest jobScheduleRequest) throws IOException {
    Response rs = scheduler != null ? scheduler.createJob(jobScheduleRequest) : null;
    if (jobScheduleRequest.getJobState() != JobState.NORMAL) {
        JobRequest jobRequest = new JobRequest();
        jobRequest.setJobId(rs.getEntity().toString());
        scheduler.pauseJob(jobRequest);
    }
    return rs;
}
Also used : Response(javax.ws.rs.core.Response) JobRequest(org.pentaho.platform.web.http.api.resources.JobRequest)

Example 2 with JobRequest

use of org.pentaho.platform.web.http.api.resources.JobRequest in project pentaho-platform by pentaho.

the class SchedulerServiceTest method testGetJobState.

@Test
public void testGetJobState() throws Exception {
    JobRequest mockJobRequest = mock(JobRequest.class);
    String jobId = "jobId";
    doReturn(jobId).when(mockJobRequest).getJobId();
    IPentahoSession mockSession = mock(IPentahoSession.class);
    doReturn(mockSession).when(schedulerService).getSession();
    Job mockJob = mock(Job.class);
    doReturn(mockJob).when(schedulerService).getJob(jobId);
    doReturn(Job.JobState.BLOCKED).when(mockJob).getState();
    String username = "username";
    doReturn(username).when(mockJob).getUserName();
    doReturn(username).when(mockSession).getName();
    // Test 1
    doReturn(true).when(schedulerService).isScheduleAllowed();
    Job.JobState testState = schedulerService.getJobState(mockJobRequest);
    assertEquals(Job.JobState.BLOCKED, testState);
    // Test 2
    doReturn(false).when(schedulerService).isScheduleAllowed();
    testState = schedulerService.getJobState(mockJobRequest);
    assertEquals(Job.JobState.BLOCKED, testState);
    verify(mockJobRequest, times(2)).getJobId();
    verify(schedulerService, times(1)).getSession();
    verify(schedulerService, times(2)).getJob(jobId);
    verify(mockJob, times(2)).getState();
    verify(mockJob, times(1)).getUserName();
    verify(mockSession, times(1)).getName();
}
Also used : JobRequest(org.pentaho.platform.web.http.api.resources.JobRequest) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Matchers.anyString(org.mockito.Matchers.anyString) Job(org.pentaho.platform.api.scheduler2.Job) Test(org.junit.Test)

Example 3 with JobRequest

use of org.pentaho.platform.web.http.api.resources.JobRequest in project pentaho-platform by pentaho.

the class SolutionImportHandler method importSchedules.

protected void importSchedules(List<JobScheduleRequest> scheduleList) throws PlatformImportException {
    if (CollectionUtils.isNotEmpty(scheduleList)) {
        SchedulerResource schedulerResource = new SchedulerResource();
        schedulerResource.pause();
        for (JobScheduleRequest jobScheduleRequest : scheduleList) {
            boolean jobExists = false;
            List<Job> jobs = getAllJobs(schedulerResource);
            if (jobs != null) {
                // paramRequest to map<String, Serializable>
                Map<String, Serializable> mapParamsRequest = new HashMap<>();
                for (JobScheduleParam paramRequest : jobScheduleRequest.getJobParameters()) {
                    mapParamsRequest.put(paramRequest.getName(), paramRequest.getValue());
                }
                for (Job job : jobs) {
                    if ((mapParamsRequest.get(RESERVEDMAPKEY_LINEAGE_ID) != null) && (mapParamsRequest.get(RESERVEDMAPKEY_LINEAGE_ID).equals(job.getJobParams().get(RESERVEDMAPKEY_LINEAGE_ID)))) {
                        jobExists = true;
                    }
                    if (overwriteFile && jobExists) {
                        JobRequest jobRequest = new JobRequest();
                        jobRequest.setJobId(job.getJobId());
                        schedulerResource.removeJob(jobRequest);
                        jobExists = false;
                        break;
                    }
                }
            }
            if (!jobExists) {
                try {
                    Response response = createSchedulerJob(schedulerResource, jobScheduleRequest);
                    if (response.getStatus() == Response.Status.OK.getStatusCode()) {
                        if (response.getEntity() != null) {
                            // get the schedule job id from the response and add it to the import session
                            ImportSession.getSession().addImportedScheduleJobId(response.getEntity().toString());
                        }
                    }
                } catch (Exception e) {
                    // the space(s)
                    if (jobScheduleRequest.getInputFile().contains(" ") || jobScheduleRequest.getOutputFile().contains(" ")) {
                        log.info("Could not import schedule, attempting to replace spaces with underscores and retrying: " + jobScheduleRequest.getInputFile());
                        File inFile = new File(jobScheduleRequest.getInputFile());
                        File outFile = new File(jobScheduleRequest.getOutputFile());
                        String inputFileName = inFile.getParent() + RepositoryFile.SEPARATOR + inFile.getName().replaceAll(" ", "_");
                        String outputFileName = outFile.getParent() + RepositoryFile.SEPARATOR + outFile.getName().replaceAll(" ", "_");
                        jobScheduleRequest.setInputFile(inputFileName);
                        jobScheduleRequest.setOutputFile(outputFileName);
                        try {
                            if (File.separator != RepositoryFile.SEPARATOR) {
                                // on windows systems, the backslashes will result in the file not being found in the repository
                                jobScheduleRequest.setInputFile(inputFileName.replace(File.separator, RepositoryFile.SEPARATOR));
                                jobScheduleRequest.setOutputFile(outputFileName.replace(File.separator, RepositoryFile.SEPARATOR));
                            }
                            Response response = createSchedulerJob(schedulerResource, jobScheduleRequest);
                            if (response.getStatus() == Response.Status.OK.getStatusCode()) {
                                if (response.getEntity() != null) {
                                    // get the schedule job id from the response and add it to the import session
                                    ImportSession.getSession().addImportedScheduleJobId(response.getEntity().toString());
                                }
                            }
                        } catch (Exception ex) {
                            // log it and keep going. we should stop processing all schedules just because one fails.
                            log.error(Messages.getInstance().getString("SolutionImportHandler.ERROR_0001_ERROR_CREATING_SCHEDULE", e.getMessage()), ex);
                        }
                    } else {
                        // log it and keep going. we should stop processing all schedules just because one fails.
                        log.error(Messages.getInstance().getString("SolutionImportHandler.ERROR_0001_ERROR_CREATING_SCHEDULE", e.getMessage()));
                    }
                }
            } else {
                log.info(Messages.getInstance().getString("DefaultImportHandler.ERROR_0009_OVERWRITE_CONTENT", jobScheduleRequest.toString()));
            }
        }
        schedulerResource.start();
    }
}
Also used : JobScheduleParam(org.pentaho.platform.web.http.api.resources.JobScheduleParam) Serializable(java.io.Serializable) HashMap(java.util.HashMap) AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) IOException(java.io.IOException) Response(javax.ws.rs.core.Response) JobRequest(org.pentaho.platform.web.http.api.resources.JobRequest) SchedulerResource(org.pentaho.platform.web.http.api.resources.SchedulerResource) Job(org.pentaho.platform.api.scheduler2.Job) JobScheduleRequest(org.pentaho.platform.web.http.api.resources.JobScheduleRequest) ManifestFile(org.pentaho.platform.plugin.services.importexport.ImportSession.ManifestFile) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) File(java.io.File)

Example 4 with JobRequest

use of org.pentaho.platform.web.http.api.resources.JobRequest in project pentaho-platform by pentaho.

the class SchedulerServiceTest method testTriggerNow.

@Test
public void testTriggerNow() throws Exception {
    JobRequest jobRequest = mock(JobRequest.class);
    Job job = mock(Job.class);
    doReturn(job).when(schedulerService.scheduler).getJob(anyString());
    doReturn(true).when(schedulerService.policy).isAllowed(anyString());
    doNothing().when(schedulerService.scheduler).triggerNow(anyString());
    // Test 1
    Job resultJob1 = schedulerService.triggerNow(jobRequest.getJobId());
    assertEquals(job, resultJob1);
    // Test 2
    doReturn("test").when(job).getUserName();
    doReturn(false).when(schedulerService.policy).isAllowed(anyString());
    IPentahoSession pentahoSession = mock(IPentahoSession.class);
    doReturn("test").when(pentahoSession).getName();
    doReturn(pentahoSession).when(schedulerService).getSession();
    Job resultJob2 = schedulerService.triggerNow(jobRequest.getJobId());
    assertEquals(job, resultJob2);
    verify(schedulerService.scheduler, times(4)).getJob(anyString());
    verify(schedulerService.scheduler, times(2)).triggerNow(anyString());
    verify(schedulerService.policy, times(2)).isAllowed(anyString());
}
Also used : JobRequest(org.pentaho.platform.web.http.api.resources.JobRequest) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Job(org.pentaho.platform.api.scheduler2.Job) Test(org.junit.Test)

Example 5 with JobRequest

use of org.pentaho.platform.web.http.api.resources.JobRequest in project pentaho-platform by pentaho.

the class SchedulerServiceTest method testGetJobStateError.

@Test
public void testGetJobStateError() throws Exception {
    JobRequest mockJobRequest = mock(JobRequest.class);
    String jobId = "jobId";
    doReturn(jobId).when(mockJobRequest).getJobId();
    IPentahoSession mockSession = mock(IPentahoSession.class);
    doReturn(mockSession).when(schedulerService).getSession();
    Job mockJob = mock(Job.class);
    doReturn(mockJob).when(schedulerService).getJob(jobId);
    doReturn(Job.JobState.BLOCKED).when(mockJob).getState();
    String username = "username";
    doReturn(username).when(mockJob).getUserName();
    String sessionName = "notUsername";
    doReturn(sessionName).when(mockSession).getName();
    doReturn(false).when(schedulerService).isScheduleAllowed();
    try {
        schedulerService.getJobState(mockJobRequest);
        fail();
    } catch (UnsupportedOperationException e) {
    // Expected
    }
}
Also used : JobRequest(org.pentaho.platform.web.http.api.resources.JobRequest) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Matchers.anyString(org.mockito.Matchers.anyString) Job(org.pentaho.platform.api.scheduler2.Job) Test(org.junit.Test)

Aggregations

JobRequest (org.pentaho.platform.web.http.api.resources.JobRequest)5 Job (org.pentaho.platform.api.scheduler2.Job)4 Test (org.junit.Test)3 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)3 Response (javax.ws.rs.core.Response)2 Matchers.anyString (org.mockito.Matchers.anyString)2 File (java.io.File)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 DomainAlreadyExistsException (org.pentaho.metadata.repository.DomainAlreadyExistsException)1 DomainIdNullException (org.pentaho.metadata.repository.DomainIdNullException)1 DomainStorageException (org.pentaho.metadata.repository.DomainStorageException)1 AlreadyExistsException (org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException)1 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)1 ManifestFile (org.pentaho.platform.plugin.services.importexport.ImportSession.ManifestFile)1 JobScheduleParam (org.pentaho.platform.web.http.api.resources.JobScheduleParam)1 JobScheduleRequest (org.pentaho.platform.web.http.api.resources.JobScheduleRequest)1 SchedulerResource (org.pentaho.platform.web.http.api.resources.SchedulerResource)1