process method
Process the queue (replay mutations) in strict FIFO order.
- If a job fails with a retryable error and has remaining retries
(
retryCount + 1 < maxRetries), its retry count is persisted and processing pauses to preserve strict FIFO ordering until the next pass. - If a job fails with a non-retryable error (
shouldRetry(e) == false) or reachesmaxRetries, it moves to deadLetterJobs,onDeadLetteris invoked, and FIFO replay continues with subsequent jobs.
Implementation
Future<void> process() async {
if (_isProcessing || _queue.isEmpty || !ZenQueryCache.instance.isOnline) {
return;
}
_isProcessing = true;
ZenLogger.logDebug(
'Processing offline mutation queue (${_queue.length} jobs)...');
try {
while (_queue.isNotEmpty && ZenQueryCache.instance.isOnline) {
final job = _queue.first;
try {
await _executeJob(job);
remove(job.id);
} catch (e) {
ZenLogger.logError('Failed to replay mutation ${job.id}', e);
// Guard against queue.remove() or queue.reset() during await
if (_queue.isEmpty || _queue.first.id != job.id) {
break;
}
final updated = job.copyWith(retryCount: job.retryCount + 1);
final retryable = _shouldRetry?.call(e) ?? true;
if (!retryable || updated.retryCount >= _maxRetries) {
_queue.removeFirst();
_deadLetterJobs.add(updated);
_persist();
try {
_onDeadLetter?.call(updated, e);
} catch (cbErr, cbStack) {
ZenLogger.logError(
'Error in ZenMutationQueue onDeadLetter callback',
cbErr,
cbStack);
}
ZenLogger.logWarning(
'Mutation ${updated.id} moved to dead-letter queue after ${updated.retryCount} attempt(s)');
continue;
}
// Retryable failure with retries remaining: update head & pause pass
// to preserve strict sequential ordering.
_queue.removeFirst();
_queue.addFirst(updated);
_persist();
break;
}
}
} finally {
_isProcessing = false;
}
}