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 drains buffered logs with Triton.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)
public static void addLogs(List<pharos__Log__c> logs, String transactionId)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 buffertransactionId-- 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 throughTritonHelper.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.
Last updated