joerghoh commented on a change in pull request #18:
URL: 
https://github.com/apache/sling-org-apache-sling-jcr-repoinit/pull/18#discussion_r648076750



##########
File path: 
src/main/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializerFactory.java
##########
@@ -122,14 +124,58 @@ public void processRepository(final SlingRepository repo) 
throws Exception {
                             continue;
                         }
                         final List<Operation> ops = parser.parse(new 
StringReader(script));
-                        log.info("Executing {} repoinit operations", 
ops.size());
-                        processor.apply(s, ops);
-                        s.save();
+                        String msg = String.format("Executing %s repoinit 
operations", ops.size());
+                        log.info(msg);
+                        applyOperations(s,ops,msg);
                     }
                 }
             } finally {
                 s.logout();
             }
         }
     }
+
+
+    /**
+     * Apply the operations within a session, support retries
+     * @param session the JCR session to use
+     * @param ops the list of operations
+     * @param logMessage the messages to print when retry
+     * @throws Exception if the application fails despite the retry
+     */
+    private void applyOperations(Session session, List<Operation> ops, String 
logMessage) throws RepositoryException {
+
+        RetryableOperation retry = new 
RetryableOperation.Builder().withBackoffBaseMsec(1000).withMaxRetries(3).build();
+        RetryableOperation.RetryableOperationResult result = retry.apply(() -> 
{
+            try {
+                processor.apply(session, ops);
+                session.save();
+                return new 
RetryableOperation.RetryableOperationResult(true,false,null);
+            } catch (InvalidItemStateException ise) {
+                // a retry makes sense, because this exception might be caused 
by an concurrent operation
+                log.debug("(temporarily) failed to apply repoinit 
operations",ise);
+                try {
+                    session.refresh(false); // discard all pending changes
+                } catch (RepositoryException e1) {
+                    // ignore
+                }
+                return new 
RetryableOperation.RetryableOperationResult(false,true,ise);
+            } catch (RepositoryException re) {
+                // a permanent error, retry is not useful
+                try {
+                    session.refresh(false); // discard all pending changes
+                } catch (RepositoryException e1) {
+                    // ignore
+                }
+                return new 
RetryableOperation.RetryableOperationResult(false,false,re);
+            }
+        }, logMessage);
+        if (!result.isSuccessful()) {

Review comment:
       This is done as part of the RetryableOperation[1].
   
   [1] 
https://github.com/apache/sling-org-apache-sling-jcr-repoinit/blob/feature/SLING-10418-retry/src/main/java/org/apache/sling/jcr/repoinit/impl/RetryableOperation.java#L60




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to