@Override public Optional<CeTask> peek(String workerUuid) { requireNonNull(workerUuid, "workerUuid can't be null"); if (computeEngineStatus.getStatus() != ComputeEngineStatus.Status.STARTED || getWorkersPauseStatus() != WorkersPauseStatus.RESUMED) { return Optional.empty(); } try (DbSession dbSession = dbClient.openSession(false)) { CeQueueDao ceQueueDao = dbClient.ceQueueDao(); int i = ceQueueDao.resetToPendingForWorker(dbSession, workerUuid); if (i > 0) { dbSession.commit(); LOG.debug("{} in progress tasks reset for worker uuid {}", i, workerUuid); } Optional<CeQueueDto> opt = ceQueueDao.peek(dbSession, workerUuid); if (opt.isPresent()) { CeQueueDto taskDto = opt.get(); Map<String, ComponentDto> componentsByUuid = loadComponentDtos(dbSession, taskDto); Map<String, String> characteristics = dbClient.ceTaskCharacteristicsDao().selectByTaskUuids(dbSession, singletonList(taskDto.getUuid())).stream() .collect(uniqueIndex(CeTaskCharacteristicDto::getKey, CeTaskCharacteristicDto::getValue)); CeTask task = convertToTask(dbSession, taskDto, characteristics, ofNullable(taskDto.getComponentUuid()).map(componentsByUuid::get).orElse(null), ofNullable(taskDto.getMainComponentUuid()).map(componentsByUuid::get).orElse(null)); queueStatus.addInProgress(); return Optional.of(task); } return Optional.empty(); } }
@Test public void resetToPendingForWorker_resets_status_of_non_pending_tasks_only_for_specified_workerUuid() { CeQueueDto[] worker1 = {insertPending("u1"), insertPending("u2"), insertPending("u3"), insertPending("u4")}; CeQueueDto[] worker2 = {insertPending("o1"), insertPending("o2"), insertPending("o3"), insertPending("o4")}; long startedAt = alwaysIncreasingSystem2.now(); makeInProgress(WORKER_UUID_1, startedAt, worker1[0]); makeInProgress(WORKER_UUID_1, startedAt, worker1[3]); makeInProgress(WORKER_UUID_2, startedAt, worker2[0]); makeInProgress(WORKER_UUID_2, startedAt, worker2[3]); underTestAlwaysIncreasingSystem2.resetToPendingForWorker(db.getSession(), WORKER_UUID_1); verifyResetToPendingForWorker(worker1[0], WORKER_UUID_1, startedAt); verifyUnchangedByResetToPendingForWorker(worker1[1]); verifyUnchangedByResetToPendingForWorker(worker1[2]); verifyResetToPendingForWorker(worker1[3], WORKER_UUID_1, startedAt); verifyInProgressUnchangedByResetToPendingForWorker(worker2[0], WORKER_UUID_2, startedAt); verifyUnchangedByResetToPendingForWorker(worker2[1]); verifyUnchangedByResetToPendingForWorker(worker2[2]); verifyInProgressUnchangedByResetToPendingForWorker(worker2[3], WORKER_UUID_2, startedAt); }
@Override public Optional<CeTask> peek(String workerUuid) { requireNonNull(workerUuid, "workerUuid can't be null"); if (computeEngineStatus.getStatus() != ComputeEngineStatus.Status.STARTED) { return Optional.empty(); } try (DbSession dbSession = dbClient.openSession(false)) { CeQueueDao ceQueueDao = dbClient.ceQueueDao(); int i = ceQueueDao.resetToPendingForWorker(dbSession, workerUuid); if (i > 0) { LOG.debug("{} in progress tasks reset for worker uuid {}", i, workerUuid); } Optional<CeQueueDto> dto = ceQueueDao.peek(dbSession, workerUuid, MAX_EXECUTION_COUNT); CeTask task = null; if (dto.isPresent()) { task = loadTask(dbSession, dto.get()); queueStatus.addInProgress(); } return Optional.ofNullable(task); } }