handleJobFailure method

Future<void> handleJobFailure(
  1. JobContext context
)

Handle job failure with retry logic

Implementation

Future<void> handleJobFailure(JobContext context) async {
  if (context.shouldRetry) {
    // Schedule retry
    context.status = JobStatus.retrying;

    if (metrics != null) {
      metrics!.jobRetried(context.job.runtimeType.toString());
    }

    final retryDelay = context.job.retryDelay;
    await retryJob(context, delay: retryDelay);

    await onJobRetried(context);
  } else {
    // Job failed permanently, send to DLQ
    context.status = JobStatus.deadLettered;

    if (dlqHandler != null && config.useDLQ) {
      await dlqHandler!.recordFailure(
        id: context.id,
        jobType: context.job.runtimeType.toString(),
        payload: context.job.toJson(),
        error: context.error,
        stackTrace: context.stackTrace,
        attempts: context.attempts,
        metadata: context.metadata,
      );
    }

    await onJobFailed(context);
  }
}