> For the complete documentation index, see [llms.txt](https://triton.pharos.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://triton.pharos.ai/methods-reference/apex-methods-reference/tritonflowflushbatch.md).

# TritonFlowFlushBatch

Singleton batch that defers and consolidates Flow log publishing to minimize platform event consumption.

## Overview

`TritonFlowFlushBatch` is a singleton `Database.Batchable` that publishes Flow logs on a short delay instead of immediately. [`TritonFlow`](https://triton.pharos.ai/methods-reference/apex-methods-reference/tritonflow.md) drains buffered logs with [`Triton.drain()`](https://triton.pharos.ai/methods-reference/apex-methods-reference/triton.md#drain) and hands them to this batch, which buffers them in Org Cache (keyed by transaction ID) across invocable calls and concurrent transactions, then publishes everything in a single `pharos.Logger.flush()`.

Before scheduling, a check against `AsyncApexJob` skips scheduling when a flush batch is already queued or running, so concurrent Flows normally share one batch. If two Flows race past that check, a second batch can be scheduled; that's harmless, because each batch publishes only the logs it took from the cache.

Nothing here throws into the Flow. If the cache write fails, the logs are published immediately. If the batch can't be scheduled, the logs it already took from the cache are published immediately. If a publish fails, those logs are dropped and the failure is written to the Apex debug log. Logs still cached when the **Cache TTL** expires are never published, so keep the batch delay below the TTL.

You do not schedule this batch yourself — `TritonFlow` invokes it via `addLogs()`.

## Methods

#### addLogs(List\<pharos\_\_Log\_\_c> logs, String transactionId)

{% code lineNumbers="true" %}

```apex
public static void addLogs(List<pharos__Log__c> logs, String transactionId)
```

{% endcode %}

Buffers logs for deferred flush. Adds the logs to the per-transaction buffer and writes them to Org Cache under `transactionId`. If the cache write fails (size exceeded or cache unavailable), the pending logs are published immediately and the transaction's cache entry is removed, so no later batch publishes them again. Otherwise a flush batch is scheduled if one is not already active; the logs that batch takes from the cache are no longer held by this transaction, so none is published twice. Never throws.

**Parameters:**

* `logs` -- the log records to buffer
* `transactionId` -- the current transaction ID, used as the cache key

## Batchable Lifecycle

The batch implements `Database.Batchable<SObject>`:

* **Constructor** — reads all pending flow-log cache entries (one per transaction), removing each key after reading to prevent double-processing. It runs when the batch is scheduled, so logs cached after that wait for the next batch.
* **`start`** — returns the collected logs as the batch scope.
* **`execute`** — publishes the scope in a single flush through [`TritonHelper.publishLogs`](https://triton.pharos.ai/methods-reference/apex-methods-reference/tritonhelper.md#publishlogs); a failed publish drops those logs and is written to the debug log.
* **`finish`** — if new logs arrived in cache since the batch was scheduled, schedules another batch for them, publishing them immediately if that schedule fails.

{% hint style="info" %}
The batch runs after a short delay — **Flow Flush Batch Delay**, 1 minute by default. Its constructor collects the cached logs when the batch is scheduled, so logs added during the delay are picked up by the next batch, which `finish` schedules. Keep the delay below the **Cache TTL** (5 minutes by default): logs still cached when the TTL expires are never published.
{% endhint %}
