Migration script failures during pod restarts

This article describes how to prevent failures of Groovy migration scripts in Adobe Experience Manager as a Cloud Service that can occur when author pods are restarted during long-running bulk content updates. The solution focuses on batching commits and refreshing repository sessions to improve migration resiliency.

Description description

Environment

  • Adobe Experience Manager (AEM) as a Cloud Service (all versions)
  • Bulk content migrations using Groovy scripts

Issue/Symptoms

  • Migration script fails after several hours with a repository unavailability error, such as: javax.jcr.RepositoryException: Repository unavailable due to session termination
  • Pod restarts are observed during the migration window
  • No OutOfMemoryError or resource exhaustion in logs
  • The same script completes successfully in pre-production environments or local development but fails in lower environments

Cause

The migration script’s use of a single, long-lived repository session with individual commits makes it susceptible to interruption by normal pod recycling in AEM as a Cloud Service. When the pod is recycled, the session is terminated and the script fails.

Resolution resolution

Resolution

To fix the migration script failure during pod restarts, follow these steps:

  1. Refactor the migration script to commit changes in batches (for example, every 1,000 nodes) instead of one at a time.

  2. After each batch commit, call session.refresh(true) to keep the session state current and minimize transient changes. For example:

    code language-none
    int batchSize = 1000;
    int count = 0;
    for (node in nodesToUpdate) {
        // ... perform update ...
        count++;
        if (count % batchSize == 0) {
            session.save();
            session.refresh(true);
        }
    }
    // Final save for any remaining changes
    session.save();
    session.refresh(true);
    
  3. Make the migration script resumable and idempotent by recording progress (such as flagging processed nodes) to enable it to resume after interruption.

  4. Implement retry logic for repository exceptions, with bounded retries and session refreshes.

  5. Run large migrations in pre-production environments, which are sized like production and less prone to frequent pod recycling. Reserve lower environments for script development and small-scale testing.

  6. Avoid running full migrations as part of deployments, as deployments roll author pods and interrupt the operation. Schedule migrations as separate, dedicated jobs.

After refactoring, run the migration script and confirm that it completes successfully without repository unavailability errors, even if pod restarts occur during execution.

recommendation-more-help
experience-cloud-kcs-help-kbarticles