@Override public long getPendingCount() { try (DbSession dbSession = dbClient.openSession(false)) { return dbClient.ceQueueDao().countByStatus(dbSession, CeQueueDto.Status.PENDING); } }
@Override public void resetTasksWithUnknownWorkerUUIDs(Set<String> knownWorkerUUIDs) { try (DbSession dbSession = dbClient.openSession(false)) { dbClient.ceQueueDao().resetTasksWithUnknownWorkerUUIDs(dbSession, knownWorkerUUIDs); dbSession.commit(); } }
@Test public void test_queue_state_with_default_settings() { when(dbClient.ceQueueDao().countByStatus(any(), eq(CeQueueDto.Status.PENDING))).thenReturn(10); when(dbClient.ceQueueDao().countByStatus(any(), eq(CeQueueDto.Status.IN_PROGRESS))).thenReturn(1); CeQueueGlobalSection underTest = new CeQueueGlobalSection(dbClient); ProtobufSystemInfo.Section section = underTest.toProtobuf(); assertThatAttributeIs(section, "Total Pending", 10); assertThatAttributeIs(section, "Total In Progress", 1); assertThatAttributeIs(section, "Max Workers per Node", 1); }
int cancelAll(boolean includeInProgress) { int count = 0; try (DbSession dbSession = dbClient.openSession(false)) { for (CeQueueDto queueDto : dbClient.ceQueueDao().selectAllInAscOrder(dbSession)) { if (includeInProgress || !queueDto.getStatus().equals(IN_PROGRESS)) { cancelImpl(dbSession, queueDto); count++; } } return count; } }
private void verifyUnmodifiedTask(CeQueueDto originalDto) { CeQueueDto dto = db.getDbClient().ceQueueDao().selectByUuid(session, originalDto.getUuid()).get(); assertThat(dto.getStatus()).isEqualTo(originalDto.getStatus()); assertThat(dto.getCreatedAt()).isEqualTo(originalDto.getCreatedAt()); assertThat(dto.getUpdatedAt()).isEqualTo(originalDto.getUpdatedAt()); }
private void verifyResetTask(CeQueueDto originalDto) { CeQueueDto dto = db.getDbClient().ceQueueDao().selectByUuid(session, originalDto.getUuid()).get(); assertThat(dto.getStatus()).isEqualTo(CeQueueDto.Status.PENDING); assertThat(dto.getCreatedAt()).isEqualTo(originalDto.getCreatedAt()); assertThat(dto.getUpdatedAt()).isGreaterThan(originalDto.getUpdatedAt()); }
private void verifyReset(CeQueueDto original) { CeQueueDto dto = db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), original.getUuid()).get(); // We do not touch CreatedAt assertThat(dto.getCreatedAt()).isEqualTo(original.getCreatedAt()); // Status must have changed to PENDING and must not be equal to previous status assertThat(dto.getStatus()).isEqualTo(CeQueueDto.Status.PENDING).isNotEqualTo(original.getStatus()); // UpdatedAt must have been updated assertThat(dto.getUpdatedAt()).isNotEqualTo(original.getUpdatedAt()); assertThat(dto.getStartedAt()).isEqualTo(original.getStartedAt()); // WorkerUuid must be null assertThat(dto.getWorkerUuid()).isNull(); }
@Test public void resumeWorkers_resumes_pausing_workers() { submit(CeTaskTypes.REPORT, newComponent(randomAlphabetic(12))); db.getDbClient().ceQueueDao().peek(session, WORKER_UUID); // task is in-progress underTest.pauseWorkers(); assertThat(underTest.getWorkersPauseStatus()).isEqualTo(CeQueue.WorkersPauseStatus.PAUSING); underTest.resumeWorkers(); assertThat(underTest.getWorkersPauseStatus()).isEqualTo(CeQueue.WorkersPauseStatus.RESUMED); }
private CeQueueDto insertInProgress(String uuid, String workerUuid) { CeQueueDto dto = new CeQueueDto() .setUuid(uuid) .setTaskType("foo") .setStatus(CeQueueDto.Status.PENDING); db.getDbClient().ceQueueDao().insert(session, dto); makeInProgress(dto, workerUuid); db.commit(); return db.getDbClient().ceQueueDao().selectByUuid(session, uuid).get(); }
@Test public void cancel_pending() { CeTask task = submit(CeTaskTypes.REPORT, newComponent(randomAlphabetic(12))); CeQueueDto queueDto = db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), task.getUuid()).get(); underTest.cancel(db.getSession(), queueDto); Optional<CeActivityDto> activity = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), task.getUuid()); assertThat(activity.isPresent()).isTrue(); assertThat(activity.get().getStatus()).isEqualTo(CeActivityDto.Status.CANCELED); }
@Test public void peek_peeks_pending_task() { db.getDbClient().ceQueueDao().insert(session, new CeQueueDto() .setUuid("uuid") .setTaskType("foo") .setStatus(CeQueueDto.Status.PENDING)); db.commit(); assertThat(underTest.peek(WORKER_UUID_1).get().getUuid()).isEqualTo("uuid"); }
@Test public void fail_throws_exception_if_task_is_pending() { CeTask task = submit(CeTaskTypes.REPORT, newComponent(randomAlphabetic(12))); CeQueueDto queueDto = db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), task.getUuid()).get(); Throwable thrown = catchThrowable(() -> underTest.fail(db.getSession(), queueDto, "TIMEOUT", "Failed on timeout")); assertThat(thrown) .isInstanceOf(IllegalStateException.class) .hasMessage("Task is not in-progress and can't be marked as failed [uuid=" + task.getUuid() + "]"); }
@Test public void massSubmit_with_UNIQUE_QUEUE_PER_MAIN_COMPONENT_does_not_create_task_when_there_is_one_pending_task_for_same_main_component() { String mainComponentUuid = randomAlphabetic(5); CeTaskSubmit taskSubmit = createTaskSubmit("with_component", newComponent(mainComponentUuid), null); CeQueueDto dto = insertPendingInQueue(newComponent(mainComponentUuid)); List<CeTask> tasks = underTest.massSubmit(of(taskSubmit), UNIQUE_QUEUE_PER_MAIN_COMPONENT); assertThat(tasks).isEmpty(); assertThat(db.getDbClient().ceQueueDao().selectAllInAscOrder(db.getSession())) .extracting(CeQueueDto::getUuid) .containsOnly(dto.getUuid()); }
@Test public void submit_with_UNIQUE_QUEUE_PER_MAIN_COMPONENT_creates_task_without_component_when_there_is_a_pending_task_without_component() { CeTaskSubmit taskSubmit = createTaskSubmit("no_component"); CeQueueDto dto = insertPendingInQueue(null); Optional<CeTask> task = underTest.submit(taskSubmit, UNIQUE_QUEUE_PER_MAIN_COMPONENT); assertThat(task).isNotEmpty(); assertThat(db.getDbClient().ceQueueDao().selectAllInAscOrder(db.getSession())) .extracting(CeQueueDto::getUuid) .containsOnly(dto.getUuid(), task.get().getUuid()); }
private CeQueueDto createTaskSubmit(@Nullable ComponentDto componentDto) { CeTaskSubmit.Builder submission = queue.prepareSubmit() .setType(CeTaskTypes.REPORT) .setSubmitterUuid(null) .setCharacteristics(emptyMap()); if (componentDto != null) { submission.setComponent(CeTaskSubmit.Component.fromDto(componentDto)); } CeTask task = queue.submit(submission.build()); return db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), task.getUuid()).get(); } }
@Test public void submit_with_UNIQUE_QUEUE_PER_MAIN_COMPONENT_creates_task_when_there_is_a_pending_task_for_another_main_component() { String mainComponentUuid = randomAlphabetic(5); String otherMainComponentUuid = randomAlphabetic(6); CeTaskSubmit taskSubmit = createTaskSubmit("with_component", newComponent(mainComponentUuid), null); CeQueueDto dto = insertPendingInQueue(newComponent(otherMainComponentUuid)); Optional<CeTask> task = underTest.submit(taskSubmit, UNIQUE_QUEUE_PER_MAIN_COMPONENT); assertThat(task).isNotEmpty(); assertThat(db.getDbClient().ceQueueDao().selectAllInAscOrder(db.getSession())) .extracting(CeQueueDto::getUuid) .containsOnly(dto.getUuid(), task.get().getUuid()); }
@Test public void submit_with_UNIQUE_QUEUE_PER_MAIN_COMPONENT_does_not_create_task_when_there_is_one_pending_task_for_same_main_component() { String mainComponentUuid = randomAlphabetic(5); CeTaskSubmit taskSubmit = createTaskSubmit("with_component", newComponent(mainComponentUuid), null); CeQueueDto dto = insertPendingInQueue(newComponent(mainComponentUuid)); Optional<CeTask> task = underTest.submit(taskSubmit, UNIQUE_QUEUE_PER_MAIN_COMPONENT); assertThat(task).isEmpty(); assertThat(db.getDbClient().ceQueueDao().selectAllInAscOrder(db.getSession())) .extracting(CeQueueDto::getUuid) .containsOnly(dto.getUuid()); }
@Test public void fail_to_cancel_if_in_progress() { submit(CeTaskTypes.REPORT, newComponent(randomAlphabetic(11))); CeQueueDto ceQueueDto = db.getDbClient().ceQueueDao().peek(session, WORKER_UUID).get(); expectedException.expect(IllegalStateException.class); expectedException.expectMessage(startsWith("Task is in progress and can't be canceled")); underTest.cancel(db.getSession(), ceQueueDto); }
@Test public void peek_ignores_in_progress_tasks() { CeQueueDto dto = db.getDbClient().ceQueueDao().insert(session, new CeQueueDto() .setUuid("uuid") .setTaskType("foo") .setStatus(CeQueueDto.Status.PENDING)); makeInProgress(dto, "foo"); db.commit(); assertThat(underTest.peek(WORKER_UUID_1)).isEmpty(); }
private CeQueueDto insertCeQueueDto(String uuid) { CeQueueDto dto = new CeQueueDto() .setUuid(uuid) .setTaskType("foo") .setStatus(CeQueueDto.Status.PENDING); db.getDbClient().ceQueueDao().insert(db.getSession(), dto); db.commit(); return dto; }