public static String getJobReportDir(String rootPath, PlannedJob plannedJob) { String job = String.valueOf(plannedJob.getJobSpec().getId()); return rootPath + File.separator + job; }
public void addToQueue(PlannedJob aJob) { try { super.addToQueue(aJob); LOG.info("Queued job:" + aJob.getJobSpec().getName()); } catch (BackendException e) { throw new RuntimeException("Exception when queueing job:" + e.getMessage()); } }
public void begin() { String jobName = plannedJob.getJobSpec().getName(); reporting.mark("chronos.query." + jobName + "." + "launched"); start.set(System.currentTimeMillis()); dao.updateJobRun(this); }
public CallableJob assembleCallableJob(PlannedJob plannedJob, int attemptNumber) { switch(plannedJob.getJobSpec().getType()) { case Query: { SupportedDriver driver = SupportedDriver.getSupportedDriverFromString( plannedJob.getJobSpec().getDriver(), drivers); return new CallableQuery(plannedJob, dao, reporter, hostname, mailInfo, session, driver, reportRootPath, attemptNumber); } case Script: return new CallableScript(plannedJob, dao, reporter, -1L, hostname, mailInfo, session, attemptNumber); default: throw new UnsupportedOperationException( String.format("Unknown jobtype...%s", plannedJob.getJobSpec().getType())); } }
public int cancelJob(PlannedJob pj) { try { int cancelled = deleteFromQueue(pj); LOG.debug("Canceled job: " + pj.getJobSpec().getId() + " with replace time: " + pj.getReplaceTime()); return cancelled; } catch (BackendException e) { throw new RuntimeException("Exception when canceling job: " + e.getMessage()); } }
public void addToQueue(PlannedJob aJob) throws BackendException { Connection conn = null; PreparedStatement stat = null; try { conn = newConnection(); stat = conn.prepareStatement( String.format("INSERT INTO %s " + "(job_id, job_lastModified, replaceTime) " + "VALUES (?, ?, ?)", queueTableName), Statement.RETURN_GENERATED_KEYS); int i = 1; stat.setLong(i++, aJob.getJobSpec().getId()); Timestamp lm = new Timestamp(aJob.getJobSpec().getLastModified().getMillis()); stat.setTimestamp(i++, lm); Timestamp rt = new Timestamp(aJob.getReplaceTime().getMillis()); stat.setTimestamp(i++, rt); int rows = stat.executeUpdate(); LOG.info(String.format("Rows updated: %d", rows)); } catch (SQLException ex) { throw new BackendException(ex); } finally { closeConnections(conn, stat); } }
protected void end() { String jobName = plannedJob.getJobSpec().getName(); finish.set(System.currentTimeMillis()); reporting.histogram("chronos.query." + jobName + "." + "querytime", finish.get() - start.get()); dao.updateJobRun(this); final Long jobId = plannedJob.getJobSpec().getId(); List<JobSpec> children = dao.getChildren(jobId); for (JobSpec aChild : children) { dao.addToQueue(new PlannedJob(aChild, plannedJob.getReplaceTime())); } }
@Override public int deleteFromQueue(PlannedJob pj) throws BackendException { int toRet = 0; Connection conn = null; PreparedStatement stat = null; try { conn = newConnection(); stat = conn.prepareStatement( String.format("DELETE FROM %s WHERE job_id = ? AND job_lastModified = ? AND replaceTime = ?", queueTableName)); int i = 1; stat.setLong(i++, pj.getJobSpec().getId()); Timestamp lm = new Timestamp(pj.getJobSpec().getLastModified().getMillis()); stat.setTimestamp(i++, lm); Timestamp rt = new Timestamp(pj.getReplaceTime().getMillis()); stat.setTimestamp(i++, rt); toRet = stat.executeUpdate(); LOG.info(String.format("Rows deleted: %d", toRet)); } catch (SQLException ex) { throw new BackendException(ex); } finally { closeConnections(conn, stat); } return toRet; }
public void updateJobRun(DateTime dt, CallableJob cj) throws BackendException { Connection conn = null; PreparedStatement stat = null; try { conn = newConnection(); stat = conn.prepareStatement( String.format("UPDATE %s SET dt = ?, callable_job = ?, name = ?, `code` = ?, status = ?, exception = ?," + " start = ?, finish = ? WHERE id = ?", jobRunTableName)); int i = 1; stat.setTimestamp(i++, new Timestamp(dt.getMillis())); stat.setString(i++, objToString(cj)); stat.setString(i++, cj.getPlannedJob().getJobSpec().getName()); stat.setString(i++, cj.getPlannedJob().getJobSpec().getCode()); stat.setInt(i++, cj.getStatus().get()); stat.setString(i++, cj.getExceptionMessage() != null ? cj.getExceptionMessage().get() : ""); stat.setTimestamp(i++, new Timestamp(cj.getStart().get())); stat.setTimestamp(i++, new Timestamp(cj.getFinish().get())); stat.setLong(i++, cj.getJobId()); int rows = stat.executeUpdate(); LOG.info(String.format("Rows updated: %d", rows)); } catch (SQLException | IOException ex) { throw new BackendException(ex); } finally { closeConnections(conn, stat); } }
stat.setTimestamp(i++, new Timestamp(dt.getMillis())); stat.setString(i++, objToString(cj)); stat.setString(i++, cj.getPlannedJob().getJobSpec().getName()); stat.setString(i++, cj.getPlannedJob().getJobSpec().getCode()); stat.setInt(i++, cj.getStatus().get()); stat.setString(i++, cj.getExceptionMessage() != null ? cj.getExceptionMessage().get() : ""); stat.setTimestamp(i++, new Timestamp(cj.getStart().get())); stat.setTimestamp(i++, new Timestamp(cj.getFinish().get())); stat.setLong(i++, cj.getPlannedJob().getJobSpec().getId());
delStat.setLong(i++, toRet.getJobSpec().getId()); Timestamp lm = new Timestamp(toRet.getJobSpec().getLastModified().getMillis()); delStat.setTimestamp(i++, lm); Timestamp rt =
protected void handleException(Exception ex) { String jobName = plannedJob.getJobSpec().getName(); LOG.error(ex); setExceptionMessage(ex.getMessage()); JobSpec spec = plannedJob.getJobSpec(); if (spec.getStatusEmail() != null && spec.getStatusEmail().size() > 0 && !spec.getStatusEmail().get(0).equals("")) { for (String statusEmail : spec.getStatusEmail()) { MailInfo errMailInfo = new MailInfo(mailInfo.from, mailInfo.fromName, statusEmail, String.format("%s creator", jobName)); AgentConsumer.sendErrorReport(spec, replacedCode, ex, jobId, hostname, errMailInfo, session, attemptNumber); } } else { AgentConsumer.sendErrorReport(spec, replacedCode, ex, jobId, hostname, mailInfo, session, attemptNumber); } reporting.mark("chronos.query." + jobName + "." + "failed"); setStatus(1); }
public CallableQuery(PlannedJob plannedJob, JobDao dao, Reporting reporting, String hostname, MailInfo mailInfo, Session session, SupportedDriver driver, String reportRootPath, int attemptNumber) { this.plannedJob = plannedJob; this.dao = dao; this.reporting = reporting; this.hostname = hostname; this.mailInfo = mailInfo; this.session = session; this.driver = driver; this.reportRootPath = reportRootPath; this.attemptNumber = attemptNumber; setReplacedCode(); if (plannedJob.getJobSpec().getResultQuery() != null) { this.replacedReportQuery = QueryReplaceUtil.replaceDateValues( plannedJob.getJobSpec().getResultQuery(), plannedJob.getReplaceTime()); } else { this.replacedReportQuery = null; } this.parts = cleanupQuery(replacedCode.split(QUERY_SPLITTER)); }
final String jobName = pj.getJobSpec().getName(); if (!pj.getJobSpec().getShouldRerun()) { continue;
@Override protected void callInternal() throws BackendException { String jobName = plannedJob.getJobSpec().getName(); String aCommand = replacedCode; try { int exitCode = runner.exec(aCommand); if (exitCode == BashRunner.SUCCESS) { reporting.mark("chronos.query." + jobName + "." + "passed"); setStatus(0); } else { String error = runner.getError(); String aMessage = genErrorMessage(plannedJob.getJobSpec(), error); handleException(new Exception(aMessage)); } } catch (BackendException ex) { handleException(ex); } finally { runner.clean(); } }
public void setReplacedCode() { Long id = plannedJob.getJobSpec().getId(); JobSpec currJob = dao.getJob(id); this.replacedCode = QueryReplaceUtil.replaceDateValues( currJob.getCode(), plannedJob.getReplaceTime()); }
@Override protected void callInternal() throws BackendException { JobSpec currJob = plannedJob.getJobSpec(); String jobName = currJob.getName(); try (Connection conn = getConnectionForJobSpec(currJob)) { int step = 0; try (Statement statement = conn.createStatement()) { while (step < parts.length) { doStep(currJob, parts[step], statement); step++; } } catch (SQLException ex) { throw new BackendException(ex); } if (replacedReportQuery != null && !replacedReportQuery.isEmpty()) { PersistentResultSet results = doReportStep(conn, replacedReportQuery); String content = createMessageContent (results, currJob, this.getReplacedReportQuery()); DataSource attachment = createAttachment(results); if (reportRootPath != null) { writeReportToLocal(results, reportRootPath, plannedJob); } sendEmail(mailInfo, attachment, content, currJob); } } catch (Exception ex) { throw new RuntimeException(ex); } reporting.mark("chronos.query." + jobName + "." + "passed"); setStatus(0); }
String name = value.getPlannedJob().getJobSpec().getName(); CallableJob cj = consumer.assembleCallableJob(value.getPlannedJob(), 1); consumer.submitJob(cj); Assert.assertEquals(name, rerun.getPlannedJob().getJobSpec().getName());
@Test public void testQueueJob() throws Exception { JobSpec job = TestAgent.getTestJob("blah", dao); long id = dao.createJob(job); job = dao.getJob(id); PlannedJob aJob = new PlannedJob(job, Utils.getCurrentTime()); List<PlannedJob> expected = new ArrayList<>(); expected.add(aJob); try { dao.addToQueue(aJob); } catch (Exception ex) { ex.printStackTrace(); } List<PlannedJob> actual = dao.getQueue(aJob.getJobSpec().getId()); assertEquals(expected, actual); PlannedJob actualPJ = null; try { actualPJ = dao.removeFromQueue(); } catch (Exception ex) { ex.printStackTrace(); } assertEquals(aJob, actualPJ); expected = new ArrayList<>(); actual = dao.getQueue(null); assertEquals(expected, actual); }