private void flush(final TaskContext taskContext, final SQLStatisticAggregateMap map) {
        if (map != null) {
            logExecutionTime = new LogExecutionTime();
            counter = 0;
            savedCount = 0;
            total = map.size();

            final int mapSize = map.size();
            final int batchSize = sqlStatisticsConfigProvider.get().getStatisticFlushBatchSize();

            final Supplier<String> messageSupplier = () ->
                    "Flushing " + ModelStringUtil.formatCsv(mapSize)
                            + " statistics with batch size " + ModelStringUtil.formatCsv(batchSize);
            taskContext.info(messageSupplier);

            final List<SQLStatValSourceDO> batchInsert = new ArrayList<>();
            // Store all aggregated COUNT entries.
            for (final Entry<SQLStatKey, LongAdder> entry : map.countEntrySet()) {
                final long ms = entry.getKey().getMs();
                final String name = entry.getKey().getName();
                final long count = entry.getValue().longValue();

                // This does a flush when the batch is full
                addEntryToBatch(
                        taskContext,
                        batchInsert,
                        SQLStatValSourceDO.createCountStat(ms, name, count),
                        batchSize);
            }
            // Store all aggregated VALUE entries.
            for (final Entry<SQLStatKey, SQLStatisticAggregateMap.ValueStatValue> entry : map.valueEntrySet()) {
                final long ms = entry.getKey().getMs();
                final String name = entry.getKey().getName();
                // TODO should not be storing this as a long in the db
                final long value = (long) entry.getValue().getValue();
                final long count = entry.getValue().getCount();

                // This does a flush when the batch is full
                addEntryToBatch(
                        taskContext,
                        batchInsert,
                        SQLStatValSourceDO.createValueStat(ms, name, value, count),
                        batchSize);
            }

            // Flush of any remaining
            if (batchInsert.size() > 0) {
                doSaveBatch(taskContext, batchInsert);
            }
            LOGGER.info("Flushed {} stats to the database ({}) in {} ({}/sec)",
                    ModelStringUtil.formatCsv(total),
                    SQLStatisticNames.SQL_STATISTIC_VALUE_SOURCE_TABLE_NAME,
                    logExecutionTime.getDuration(),
                    total / ((double) logExecutionTime.getDurationMs() / 1_000));
        }
    }
We can't drop out if interrupted as that would mean losing data.
We need to flush it all the DB before we can return.
stroom-statistics/stroom-statistics-impl-sql/src/main/java/stroom/statistics/impl/sql/SQLStatisticFlushTaskHandler.java