@Override public List<com.enioka.jqm.api.JobInstance> getActiveJobs() { return Query.create().setQueryHistoryInstances(false).setQueryLiveInstances(true).addSortAsc(Sort.ID).run(); }
/** * Helper query (directly uses {@link Query}). Gives the count of all OK-ended job instances. */ public int getOkCount() { return Query.create().addStatusFilter(State.ENDED).run().size(); }
@Override public List<JobInstance> getJobs(Query query) { try { Query res = target.path("ji/query").request().post(Entity.entity(query, MediaType.APPLICATION_XML), Query.class); query.setResultSize(res.getResultSize()); query.setResults(res.getResults()); return query.getResults(); } catch (BadRequestException e) { throw new JqmInvalidRequestException(e.getResponse().readEntity(String.class), e); } catch (Exception e) { throw new JqmClientException(e); } }
if ((query.getFirstRow() != null || query.getPageSize() != null) && query.isQueryLiveInstances() && query.isQueryHistoryInstances()) if (query.isQueryLiveInstances() && query.isQueryHistoryInstances() && query.getSorts().size() > 0) if (!query.isQueryHistoryInstances() && !query.isQueryLiveInstances()) if (query.isQueryLiveInstances()) wh += getIntPredicate("ji.ID", query.getJobInstanceId(), prms); wh += getIntPredicate("ji.PARENT", query.getParentId(), prms); wh += getStringPredicate("ji.APPLICATION", query.getInstanceApplication(), prms); wh += getStringPredicate("ji.MODULE", query.getInstanceModule(), prms); wh += getStringPredicate("ji.KEYWORD1", query.getInstanceKeyword1(), prms); wh += getStringPredicate("ji.KEYWORD2", query.getInstanceKeyword2(), prms); wh += getStringPredicate("ji.KEYWORD3", query.getInstanceKeyword3(), prms); wh += getStringPredicate("ji.USERNAME", query.getUser(), prms); wh += getStringPredicate("ji.SESSION_KEY", query.getSessionId(), prms); wh += getStatusPredicate("ji.STATUS", query.getStatus(), prms); wh += getStringPredicate("jd.JD_KEY", query.getApplicationName(), prms); wh += getStringPredicate("jd.APPLICATION", query.getJobDefApplication(), prms); wh += getStringPredicate("jd.MODULE", query.getJobDefModule(), prms); wh += getStringPredicate("jd.KEYWORD1", query.getJobDefKeyword1(), prms); wh += getStringPredicate("jd.KEYWORD2", query.getJobDefKeyword2(), prms); wh += getStringPredicate("jd.KEYWORD3", query.getJobDefKeyword3(), prms); wh += getStringPredicate("n.NAME", query.getNodeName(), prms); wh += getStringPredicate("q.NAME", query.getQueueName(), prms);
/** * Version of {@link JqmClient#getDeliverableContent(Deliverable)} which does not require the web service APIs to be enabled to work. * Also, returned files do not self-destruct on stream close.<br> * See the javadoc of the original method for details. * * @throws FileNotFoundException */ public InputStream getDeliverableContent(Deliverable file) throws FileNotFoundException { List<com.enioka.jqm.model.Deliverable> dd = com.enioka.jqm.model.Deliverable.select(cnx, "deliverable_select_by_id", file.getId()); if (dd.isEmpty()) { throw new JqmInvalidRequestException("no deliverable with this ID"); } com.enioka.jqm.model.Deliverable d = dd.get(0); JobInstance ji = Query.create().setJobInstanceId(d.getJobId()).run().get(0); String nodeName = ji.getNodeName(); Node n = nodes.get(nodeName); return new FileInputStream(FilenameUtils.concat(n.getDlRepo(), file.getFilePath())); } }
@Test public void testRunnableInject() throws Exception { JqmSimpleTest.create(cnx, "pyl.EngineApiInjectThread").expectOk(3).run(this); Assert.assertEquals(3, (int) cnx.runSelectSingle("message_select_count_all", Integer.class)); Assert.assertEquals(100, (int) Query.create().addSortAsc(Sort.ID).run().get(0).getProgress()); }
@Override public void waitChildren() { JqmClient c = getJqmClient(); Query q = Query.create().setQueryHistoryInstances(false).setQueryLiveInstances(true).setParentId(ji.getId()); while (!c.getJobs(q).isEmpty()) { try { Thread.sleep(1000); handleInstructions(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); break; } } }
@Test // Commented - waiting for one minute is long. public void testDelayedJob() { CreationTools.createJobDef(null, true, "pyl.EngineApiSendMsg", null, "jqm-tests/jqm-test-pyl/target/test.jar", TestHelpers.qVip, 42, "MarsuApplication", null, "Franquin", "ModuleMachin", "other", "other", true, cnx); Calendar runAt = Calendar.getInstance(); runAt.set(Calendar.MILLISECOND, 0); // Not needed in normal operations, but we will compare at the end. runAt.set(Calendar.SECOND, 0); runAt.add(Calendar.MINUTE, 1); JobRequest.create("MarsuApplication", "testuser").setRunAfter(runAt).submit(); Assert.assertEquals(1, TestHelpers.getQueueAllCount(cnx)); Assert.assertEquals(State.SCHEDULED, Query.create().setQueryLiveInstances(true).run().get(0).getState()); Assert.assertTrue(Query.create().setQueryLiveInstances(true).run().get(0).isFromSchedule()); Assert.assertEquals(runAt, Query.create().setQueryLiveInstances(true).run().get(0).getRunAfter()); addAndStartEngine(); TestHelpers.waitFor(1, 150000, cnx); Assert.assertEquals(1, TestHelpers.getOkCount(cnx)); Assert.assertTrue(Query.create().run().get(0).isFromSchedule()); Assert.assertTrue(Query.create().run().get(0).getBeganRunningDate().after(runAt)); }
Assert.assertTrue(Query.create().setNodeName("localhost48").run().size() == 0L); Assert.assertTrue(Query.create().setNodeName("localhost").run().size() > 0L); Assert.assertTrue(Query.create().setNodeName("localhost4").run().size() > 0L); Assert.assertTrue(Query.create().setNodeName("localhost5").run().size() > 0L);
/** * A parent job can wait for all its children - then its end date should be after the end date of the children. */ @Test public void testWaitChildren() throws Exception { JqmSimpleTest.create(cnx, "pyl.EngineApiWaitAll").expectOk(6).run(this); List<JobInstance> jj = Query.create().addSortAsc(Sort.ID).addStatusFilter(State.ENDED).run(); Calendar parentEnd = jj.get(0).getEndDate(); for (int i = 1; i < 6; i++) { Assert.assertTrue(parentEnd.after(jj.get(i).getEndDate())); } }
Assert.assertEquals(0, TestHelpers.getNonOkCount(cnx)); List<JobInstance> jis = Query.create().setParentId(i).run(); Assert.assertEquals(1, jis.size());
@Test public void testHistoryFields() throws Exception { CreationTools.createJobDef(null, true, "App", null, "jqm-tests/jqm-test-datetimemaven/target/test.jar", TestHelpers.qVip, 42, "MarsuApplication", null, "Franquin", "ModuleMachin", "other", "other2", true, cnx); int i = JobRequest.create("MarsuApplication", "TestUser").setSessionID("session42").setKeyword1("k1").setKeyword2("k2").submit(); addAndStartEngine(); TestHelpers.waitFor(1, 10000, cnx); Assert.assertEquals(1, TestHelpers.getOkCount(cnx)); JobInstance h = JqmClientFactory.getClient().getJob(i); SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm"); jqmlogger.debug("enqueueDate: " + df.format(h.getEnqueueDate().getTime())); jqmlogger.debug("ExecutionDate: " + df.format(h.getBeganRunningDate().getTime())); jqmlogger.debug("EndDate: " + df.format(h.getEndDate().getTime())); Assert.assertTrue(h.getEnqueueDate() != null); Assert.assertTrue(h.getUser() != null); Assert.assertTrue(h.getEndDate() != null); Assert.assertTrue(h.getBeganRunningDate() != null); Assert.assertTrue(h.getSessionID() != null); Assert.assertEquals("session42", h.getSessionID()); JobInstance ji = Query.create().setApplicationName("MarsuApplication").run().get(0); Assert.assertEquals("ModuleMachin", ji.getDefinitionKeyword1()); Assert.assertEquals("other", ji.getDefinitionKeyword2()); Assert.assertEquals("other2", ji.getDefinitionKeyword3()); Assert.assertEquals("k1", ji.getKeyword1()); Assert.assertEquals("k2", ji.getKeyword2()); Assert.assertEquals(null, ji.getKeyword3()); }
for (JobInstance ji : Query.create().setPageSize(1000).run())
public Query(String applicationName, String instanceKeyword1) { this.setApplicationName(applicationName); this.instanceKeyword1 = instanceKeyword1; }
/** * Version of {@link JqmClient#getDeliverableContent(Deliverable)} which does not require the web service APIs to be enabled to work. * Also, returned files do not self-destruct on stream close.<br> * See the javadoc of the original method for details. * * @throws FileNotFoundException */ public InputStream getDeliverableContent(Deliverable file) throws FileNotFoundException { List<com.enioka.jqm.model.Deliverable> dd = com.enioka.jqm.model.Deliverable.select(cnx, "deliverable_select_by_id", file.getId()); if (dd.isEmpty()) { throw new JqmInvalidRequestException("no deliverable with this ID"); } com.enioka.jqm.model.Deliverable d = dd.get(0); JobInstance ji = Query.create().setJobInstanceId(d.getJobId()).run().get(0); String nodeName = ji.getNodeName(); Node n = nodes.get(nodeName); return new FileInputStream(FilenameUtils.concat(n.getDlRepo(), file.getFilePath())); } }
@Test public void testChangeQueue() throws Exception { CreationTools.createJobDef(null, true, "App", null, "jqm-tests/jqm-test-datetimemaven/target/test.jar", TestHelpers.qVip, 42, "MarsuApplication", null, "Franquin", "ModuleMachin", "other", "other", false, cnx); int i = JobRequest.create("MarsuApplication", "TestUser").submit(); JqmClientFactory.getClient().setJobQueue(i, TestHelpers.qSlow); addAndStartEngine(); TestHelpers.waitFor(1, 10000, cnx); List<JobInstance> res = Query.create().addSortAsc(Sort.DATEENQUEUE).run(); Assert.assertEquals(1, res.size()); Assert.assertEquals(State.ENDED, res.get(0).getState()); Assert.assertEquals("SlowQueue", res.get(0).getQueue().getName()); }
@Override public void waitChildren() { JqmClient c = getJqmClient(); Query q = Query.create().setQueryHistoryInstances(false).setQueryLiveInstances(true).setParentId(ji.getId()); while (!c.getJobs(q).isEmpty()) { try { Thread.sleep(1000); handleInstructions(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); break; } } }
@Test public void testStartHeld() { CreationTools.createJobDef(null, true, "pyl.EngineApiSendMsg", null, "jqm-tests/jqm-test-pyl/target/test.jar", TestHelpers.qVip, 42, "MarsuApplication", null, "Franquin", "ModuleMachin", "other", "other", true, cnx); int i = JobRequest.create("MarsuApplication", "testuser").startHeld().submit(); addAndStartEngine(); // Should not run. sleepms(1000); Assert.assertEquals(1, TestHelpers.getQueueAllCount(cnx)); Assert.assertEquals(State.HOLDED, Query.create().setQueryLiveInstances(true).run().get(0).getState()); // Resume at will. JqmClientFactory.getClient().resumeQueuedJob(i); TestHelpers.waitFor(1, 10000, cnx); Assert.assertEquals(1, TestHelpers.getOkCount(cnx)); } }