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;
}
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();
}
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();
}
}
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());
}
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
}
}
Aggregations