@Override public List<InputStream> getJobDeliverablesContent(int idJob) { DbConn cnx = null; ArrayList<InputStream> streams = new ArrayList<>(); try { cnx = getDbSession(); for (Deliverable del : Deliverable.select(cnx, "deliverable_select_all_for_ji", idJob)) { streams.add(getDeliverableContent(del)); } } catch (Exception e) { throw new JqmClientException("could not retrieve file streams", e); } finally { closeQuietly(cnx); } return streams; }
@Override public int getQueueEnabledCapacity(com.enioka.jqm.api.Queue q) { int capacity = 0; DbConn cnx = null; try { cnx = getDbSession(); ResultSet rs = cnx.runSelect("dp_select_sum_queue_capacity", q.getId()); while (rs.next()) { capacity = rs.getInt(1); } } catch (Exception e) { throw new JqmClientException("could not query queue capacity around nodes", e); } finally { closeQuietly(cnx); } return capacity; }
@Override public List<com.enioka.jqm.api.Queue> getQueues() { List<com.enioka.jqm.api.Queue> res = new ArrayList<>(); DbConn cnx = null; com.enioka.jqm.api.Queue tmp = null; try { cnx = getDbSession(); for (Queue q : Queue.select(cnx, "q_select_all")) { tmp = getQueue(q); res.add(tmp); } return res; } catch (Exception e) { throw new JqmClientException("could not query queues", e); } finally { closeQuietly(cnx); } }
@Override public InputStream getDeliverableContent(int delId) { DbConn cnx = null; Deliverable deliverable = null; try { cnx = getDbSession(); List<Deliverable> dd = Deliverable.select(cnx, "deliverable_select_by_id", delId); if (dd.size() == 0) { throw new JqmClientException("There is no deliverable with the given ID - check your ID"); } deliverable = dd.get(0); } catch (Exception e) { throw new JqmInvalidRequestException("Could not get find deliverable description inside DB - your ID may be wrong", e); } finally { closeQuietly(cnx); } return getDeliverableContent(deliverable); }
@Override public int enqueueFromHistory(int jobIdToCopy) { DbConn cnx = null; try { cnx = getDbSession(); return enqueue(getJobRequest(jobIdToCopy, cnx)); } catch (NoResultException e) { throw new JqmInvalidRequestException("No job for this ID in the history"); } finally { closeQuietly(cnx); } }
@Override public void resumeRunningJob(int jobId) { jqmlogger.trace("Job instance number {}, supposed to be paused, will receive a RUN instruction", jobId); DbConn cnx = null; try { cnx = getDbSession(); QueryResult qr = cnx.runUpdate("jj_update_instruction_resume_by_id", jobId); if (qr.nbUpdated != 1) { throw new JqmInvalidRequestException("An attempt was made to resume a job instance that did not exist or was not paused."); } cnx.commit(); } catch (Exception e) { throw new JqmClientException("could not resume a job (internal error)", e); } finally { closeQuietly(cnx); } }
@Override public void resumeQueuedJob(int idJob) { jqmlogger.trace("Job status number " + idJob + " will be resumed"); DbConn cnx = null; try { cnx = getDbSession(); QueryResult qr = cnx.runUpdate("jj_update_resume_by_id", idJob); if (qr.nbUpdated != 1) { throw new JqmInvalidRequestException("An attempt was made to pause a job instance that did not exist or was not paused."); } cnx.commit(); } catch (Exception e) { throw new JqmClientException("could not resume a job (internal error)", e); } finally { closeQuietly(cnx); } }
@Override public void pauseRunningJob(int jobId) { jqmlogger.trace("Job instance number " + jobId + " will receive a PAUSE instruction"); DbConn cnx = null; try { cnx = getDbSession(); QueryResult qr = cnx.runUpdate("jj_update_instruction_pause_by_id", jobId); if (qr.nbUpdated != 1) { throw new JqmInvalidRequestException("An attempt was made to pause a job instance that did not exist or was already done."); } cnx.commit(); } catch (Exception e) { throw new JqmClientException("could not pause a job (internal error)", e); } finally { closeQuietly(cnx); } }
@Override public void pauseQueuedJob(int idJob) { jqmlogger.trace("Job instance number " + idJob + " status will be set to HOLDED"); DbConn cnx = null; try { cnx = getDbSession(); QueryResult qr = cnx.runUpdate("jj_update_pause_by_id", idJob); if (qr.nbUpdated != 1) { throw new JqmInvalidRequestException( "An attempt was made to pause a job instance that did not exist or was already running."); } cnx.commit(); } catch (Exception e) { throw new JqmClientException("could not pause a job (internal error)", e); } finally { closeQuietly(cnx); } }
@Override public void clearQueue(com.enioka.jqm.api.Queue q) { DbConn cnx = null; try { cnx = getDbSession(); QueryResult qr = cnx.runUpdate("ji_delete_waiting_in_queue_id", q.getId()); cnx.commit(); jqmlogger.info("{} waiting job instances were removed from queue {}", qr.nbUpdated, q.getId()); } catch (Exception e) { throw new JqmClientException("could not clear queue", e); } finally { closeQuietly(cnx); } }
@Override public void pauseQueue(com.enioka.jqm.api.Queue q) { DbConn cnx = null; try { cnx = getDbSession(); cnx.runUpdate("dp_update_enable_by_queue_id", Boolean.FALSE, q.getId()); cnx.commit(); jqmlogger.info("Queue {} has been paused", q.getId()); } catch (Exception e) { throw new JqmClientException("could not pause queue", e); } finally { closeQuietly(cnx); } }
@Override public void resumeQueue(com.enioka.jqm.api.Queue q) { DbConn cnx = null; try { cnx = getDbSession(); cnx.runUpdate("dp_update_enable_by_queue_id", Boolean.TRUE, q.getId()); cnx.commit(); jqmlogger.info("Queue {} has been resumed", q.getId()); } catch (Exception e) { throw new JqmClientException("could not pause queue", e); } finally { closeQuietly(cnx); } }
@Override public List<com.enioka.jqm.api.Deliverable> getJobDeliverables(int idJob) { DbConn cnx = null; try { cnx = getDbSession(); // TODO: no intermediate entity here: directly SQL => API object. List<Deliverable> deliverables = Deliverable.select(cnx, "deliverable_select_all_for_ji", idJob); List<com.enioka.jqm.api.Deliverable> res = new ArrayList<>(); for (Deliverable d : deliverables) { res.add(new com.enioka.jqm.api.Deliverable(d.getFilePath(), d.getFileFamily(), d.getId(), d.getOriginalFileName())); } return res; } catch (Exception e) { throw new JqmClientException("Could not query files for job instance " + idJob, e); } finally { closeQuietly(cnx); } }
private int createSchedule(JobRequest jr, DbConn cnx) { // The job def JobDef jobDef = null; try { jobDef = JobDef.select_key(cnx, jr.getApplicationName()); } catch (NonUniqueResultException ex) { jqmlogger.error("There are multiple Job definition named " + jr.getApplicationName() + ". Inconsistent configuration."); closeQuietly(cnx); throw new JqmInvalidRequestException("There are multiple Job definition named " + jr.getApplicationName()); } catch (NoResultException ex) { jqmlogger.error("Job definition named " + jr.getApplicationName() + " does not exist"); closeQuietly(cnx); throw new JqmInvalidRequestException("no job definition named " + jr.getApplicationName()); } // The queue Integer queueId = null; // No override = use JD queue. if (jr.getQueueName() != null) { // use requested key if given. queueId = cnx.runSelectSingle("q_select_by_key", 1, Integer.class, jr.getQueueName()); } // The new schedule return ScheduledJob.create(cnx, jr.getRecurrence(), jobDef.getId(), queueId, jr.getPriority(), jr.getParameters()); }
@Override public void setScheduleRecurrence(int scheduleId, String cronExpression) { DbConn cnx = null; try { cnx = getDbSession(); QueryResult qr = cnx.runUpdate("sj_update_cron_by_id", cronExpression, scheduleId); if (qr.nbUpdated != 1) { throw new JqmClientException("Schedule does not exist"); } cnx.commit(); } catch (DatabaseException e) { throw new JqmClientException("could not change the cron expression of a schedule (internal database error)", e); } catch (Exception e) { throw new JqmClientException("could not change the cron expression of a schedule (internal error)", e); } finally { closeQuietly(cnx); } }
@Override public void setSchedulePriority(int scheduleId, int priority) { DbConn cnx = null; try { cnx = getDbSession(); QueryResult qr = cnx.runUpdate("sj_update_priority_by_id", priority, scheduleId); if (qr.nbUpdated != 1) { throw new JqmClientException("Schedule does not exist"); } cnx.commit(); } catch (DatabaseException e) { throw new JqmClientException("could not change the priority of a schedule (internal database error)", e); } catch (Exception e) { throw new JqmClientException("could not change the priority of a schedule (internal error)", e); } finally { closeQuietly(cnx); } }
@Override public void setJobPriority(int jobId, int priority) { DbConn cnx = null; try { cnx = getDbSession(); QueryResult qr = cnx.runUpdate("jj_update_priority_by_id", priority, jobId); if (qr.nbUpdated != 1) { throw new JqmClientException("Job instance does not exist or has already ended"); } cnx.commit(); } catch (DatabaseException e) { throw new JqmClientException("could not change the priority of a job (internal database error)", e); } catch (Exception e) { throw new JqmClientException("could not change the priority of a job (internal error)", e); } finally { closeQuietly(cnx); } }
@Override public void setScheduleQueue(int scheduleId, int queueId) { DbConn cnx = null; try { cnx = getDbSession(); QueryResult qr = cnx.runUpdate("sj_update_queue_by_id", queueId, scheduleId); if (qr.nbUpdated != 1) { throw new JqmClientException("Schedule does not exist"); } cnx.commit(); } catch (DatabaseException e) { throw new JqmClientException("could not change the queue of a schedule (internal database error)", e); } catch (Exception e) { throw new JqmClientException("could not change the queue of a schedule (internal error)", e); } finally { closeQuietly(cnx); } }
@Override public void setJobRunAfter(int jobId, Calendar whenToRun) { DbConn cnx = null; try { cnx = getDbSession(); QueryResult qr = cnx.runUpdate("jj_update_notbefore_by_id", whenToRun, jobId); if (qr.nbUpdated != 1) { throw new JqmClientException("Job instance does not exist or has already started"); } cnx.commit(); } catch (DatabaseException e) { throw new JqmClientException("could not change the 'not before time' of a job (internal database error)", e); } catch (Exception e) { throw new JqmClientException("could not change the 'not before time' of a job (internal error)", e); } finally { closeQuietly(cnx); } }
@Override public void setJobQueue(int idJob, int idQueue) { DbConn cnx = null; try { cnx = getDbSession(); QueryResult qr = cnx.runUpdate("jj_update_queue_by_id", idQueue, idJob); if (qr.nbUpdated != 1) { throw new JqmClientException("Job instance does not exist or has already started"); } cnx.commit(); } catch (DatabaseException e) { if (e.getCause() instanceof SQLIntegrityConstraintViolationException) throw new JqmClientException("Queue does not exist", e); else throw new JqmClientException("could not change the queue of a job (internal error)", e); } catch (Exception e) { throw new JqmClientException("could not change the queue of a job (internal error)", e); } finally { closeQuietly(cnx); } }