private long releaseTasks(final List<Condition> conditions) {
        final AtomicLong minId = new AtomicLong(-1);
        boolean complete = false;
        long count = 0;

        while (!complete) {
            final List<Record2<Long, Byte>> results = JooqUtil.contextResult(processorDbConnProvider,
                    context ->
                            context
                                    .select(PROCESSOR_TASK.ID, PROCESSOR_TASK.STATUS)
                                    .from(PROCESSOR_TASK)
                                    .where(conditions)
                                    .and(PROCESSOR_TASK.ID.gt(minId.get()))
                                    .orderBy(PROCESSOR_TASK.ID)
                                    .limit(BATCH_SIZE)
                                    .fetch());
            if (results.isEmpty()) {
                complete = true;
            } else {
                // Group by status.
                final Map<TaskStatus, Set<Long>> idSetMap = new HashMap<>();
                for (final Record2<Long, Byte> record : results) {
                    minId.set(record.value1());

                    final TaskStatus status = TaskStatus.PRIMITIVE_VALUE_CONVERTER.fromPrimitiveValue(record.value2());
                    final long id = record.value1();
                    idSetMap.computeIfAbsent(status, k -> new HashSet<>()).add(id);
                }

                for (final Entry<TaskStatus, Set<Long>> entry : idSetMap.entrySet()) {
                    final Condition updateCondition = PROCESSOR_TASK.ID.in(entry.getValue())
                            .and(PROCESSOR_TASK.STATUS.eq(entry.getKey().getPrimitiveValue()));
                    count += JooqUtil.contextResult(processorDbConnProvider, context ->
                            changeStatus(context,
                                    null,
                                    TaskStatus.CREATED,
                                    System.currentTimeMillis(),
                                    updateCondition));
                }
            }
        }

        return count;
    }
Retain task ownership

@param retainForNodes  A set of nodes to retain task ownership for.
@param statusOlderThan Change task ownership for tasks that have a status older than this.
@return The number of tasks released.
stroom-processor/stroom-processor-impl-db/src/main/java/stroom/processor/impl/db/ProcessorTaskDaoImpl.java