/** * Gets the job status * * @param id * @return SchedulerJobStatus * @throws SQLException */ public SchedulerJobState getJobState(String id) throws SQLException { String fetchSQL = "SELECT " + COLUMN_STATUS + " FROM " + JOB_TABLE + " WHERE " + COLUMN_ID + "=?"; List<Object[]> result = runner.query(fetchSQL, multipleRowsHandler, id); if (result.size() == 0) { return null; } else { return SchedulerJobState.valueOf((String) result.get(0)[0]); } }
private void setStateOfJob(SchedulerJobInfo info, SchedulerJobEvent event) throws LensException { SchedulerJobState currentState = info.getJobState(); try { SchedulerJobState nextState = currentState.nextTransition(event); info.setJobState(nextState); info.setModifiedOn(System.currentTimeMillis()); int ret = schedulerDAO.updateJobStatus(info); if (ret == 1) { log.info("Successfully changed the status of job with handle {} from {} to {}", info.getId(), currentState, nextState); } else { throw new LensException(LensSchedulerErrorCode.CANT_UPDATE_RESOURCE_WITH_HANDLE.getLensErrorInfo(), null, "job", info.getId().getHandleIdString()); } } catch (InvalidStateTransitionException e) { throw new LensException(LensSchedulerErrorCode.INVALID_EVENT_FOR_JOB.getLensErrorInfo(), e, event.name(), currentState.name(), info.getId().getHandleIdString()); } }
@Test(priority = 2) public void testUpdateJob() throws Exception { // Get all the stored jobs. // update one and check if it successful. SchedulerJobInfo jobInfo = schedulerDAO.getSchedulerJobInfo(jobHandle); XJob newJob = getTestJob(); jobInfo.setJob(newJob); schedulerDAO.updateJob(jobInfo); XJob storedJob = schedulerDAO.getJob(jobInfo.getId()); Assert.assertEquals(storedJob, newJob); // Change SchedulerJobInstanceState jobInfo.setJobState(jobInfo.getJobState().nextTransition(SchedulerJobEvent.ON_SCHEDULE)); schedulerDAO.updateJobStatus(jobInfo); Assert.assertEquals(schedulerDAO.getJobState(jobInfo.getId()), SchedulerJobState.SCHEDULED); }
/** * Updates the job status form the new SchedulerJobInfo * * @param info: Updated info objects * @return number of rows updated. */ public int updateJobStatus(SchedulerJobInfo info) { try { return store.updateJobStatus(info.getId().getHandleIdString(), info.getJobState().name(), info.getModifiedOn()); } catch (SQLException e) { log.error("Error while updating job status for {}", info.getId().getHandleIdString(), e); return 0; } }
@Test(priority = 3) public void testSearchStoreJob() throws Exception { // Store more jobs with the one user and search XJob job = getTestJob(); long currentTime = System.currentTimeMillis(); SchedulerJobInfo info = new SchedulerJobInfo(SchedulerJobHandle.fromString(UUID.randomUUID().toString()), job, "lens", SchedulerJobState.NEW, currentTime, currentTime); // Store the job schedulerDAO.storeJob(info); info = new SchedulerJobInfo(SchedulerJobHandle.fromString(UUID.randomUUID().toString()), job, "lens", SchedulerJobState.NEW, currentTime, currentTime); schedulerDAO.storeJob(info); // There should be 3 jobs till now. Assert.assertEquals(schedulerDAO.getJobs("lens", null, null, SchedulerJobState.values()).size(), 3); //Assert.assertEquals(schedulerDAO.getJobs("lens", 1L, System.currentTimeMillis(), SchedulerJobState.NEW).size(), 2); Assert.assertEquals(schedulerDAO.getJobs("Alice", null, null, SchedulerJobState.NEW).size(), 0); } }
/** * Inserts the Job info object into job table * * @param jobInfo * @return number of rows inserted. * @throws SQLException */ public int insertIntoJobTable(SchedulerJobInfo jobInfo) throws SQLException { String insertSQL = "INSERT INTO " + JOB_TABLE + " VALUES(?,?,?,?,?,?,?)"; JAXBElement<XJob> xmlJob = jobFactory.createJob(jobInfo.getJob()); return runner.update(insertSQL, jobInfo.getId().toString(), ToXMLString.toString(xmlJob), jobInfo.getUserName(), jobInfo.getJobState().name(), jobInfo.getCreatedOn(), jobInfo.getModifiedOn(), jobInfo.getJob().getName()); }
/** * Gets the Job info object * * @param idStr * @return SchedulerJobInfo object corresponding to the job handle. * @throws SQLException */ public SchedulerJobInfo getSchedulerJobInfo(String idStr) throws SQLException { String fetchSQL = "SELECT * FROM " + JOB_TABLE + " WHERE " + COLUMN_ID + "=?"; List<Object[]> result = runner.query(fetchSQL, multipleRowsHandler, idStr); if (result.size() == 0) { return null; } else { Object[] jobInfo = result.get(0); SchedulerJobHandle id = SchedulerJobHandle.fromString((String) jobInfo[0]); XJob xJob = ToXMLString.valueOf((String) jobInfo[1], ObjectFactory.class); String userName = (String) jobInfo[2]; String status = (String) jobInfo[3]; long createdOn = (Long) jobInfo[4]; long modifiedOn = (Long) jobInfo[5]; SchedulerJobState jobState = SchedulerJobState.valueOf(status); return new SchedulerJobInfo(id, xJob, userName, jobState, createdOn, modifiedOn); } }