]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
Attempt to start ElasticSearchService when reindexing if connection failed on startup.
authorRay Lee <ray.lee@lyrasis.org>
Sat, 9 Mar 2024 01:33:48 +0000 (20:33 -0500)
committerRay Lee <ray.lee@lyrasis.org>
Sat, 9 Mar 2024 01:34:13 +0000 (20:34 -0500)
services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/NuxeoRepositoryClientImpl.java
services/index/service/src/main/java/org/collectionspace/services/index/IndexResource.java

index eed6e4f4be0941750c4456fdb5a988b095fadc3b..8621efb21376927222dbddf998510b55913f24e4 100644 (file)
@@ -87,6 +87,7 @@ import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
 import org.nuxeo.ecm.core.opencmis.bindings.NuxeoCmisServiceFactory;
 import org.nuxeo.ecm.core.opencmis.impl.server.NuxeoCmisService;
 import org.nuxeo.elasticsearch.ElasticSearchComponent;
+import org.nuxeo.elasticsearch.api.ESClient;
 import org.nuxeo.elasticsearch.api.ElasticSearchService;
 import org.nuxeo.runtime.api.Framework;
 import org.nuxeo.runtime.transaction.TransactionRuntimeException;
@@ -314,11 +315,36 @@ public class NuxeoRepositoryClientImpl implements RepositoryClient<PoxPayloadIn,
 
         CoreSessionInterface repoSession = null;
         ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = handler.getServiceContext();
+        ElasticSearchComponent es = (ElasticSearchComponent) Framework.getService(ElasticSearchService.class);
+        ESClient esClient = null;
+
+        try {
+            // Ensure an Elasticsearch connection has been established. This should have happened
+            // on startup, but may not have, if the Elasticsearch service wasn't reachable when
+            // Nuxeo started.
+
+            esClient = es.getClient();
+        } catch (Exception e) {
+            esClient = null;
+        }
 
         try {
             repoSession = getRepositorySession(ctx);
 
-            ElasticSearchComponent es = (ElasticSearchComponent) Framework.getService(ElasticSearchService.class);
+            if (esClient == null) {
+                // The connection to ES has not been established.
+                // Attempt to start the ElasticSearchService.
+
+                es.start(null);
+
+                try {
+                    // Wait for startup configuration to complete.
+
+                    Thread.sleep(5000);
+                } catch (InterruptedException ex) {
+                }
+            }
+
             String repositoryName = repoSession.getRepositoryName();
 
             logger.info(String.format("Rebuilding Elasticsearch index for repository %s", repositoryName));
index 4693e9363bd9c4520301c9fd7a76759876723f1c..2401a0b5d56482694022b7baa0ac1c388a9d3ae5 100644 (file)
@@ -75,11 +75,11 @@ public class IndexResource extends NuxeoBasedResource {
             return null;
         }
     }
-    
+
        /*
         * HTTP Methods
         */
-    
+
        @Override
        @POST
        public Response create(@Context ResourceMap resourceMap, @Context UriInfo ui, String xmlPayload) {
@@ -87,7 +87,7 @@ public class IndexResource extends NuxeoBasedResource {
                                .entity(ServiceMessages.POST_UNSUPPORTED).type("text/plain").build();
                return response;
        }
-       
+
     @Override
        @DELETE
     @Path("{csid}")
@@ -97,7 +97,7 @@ public class IndexResource extends NuxeoBasedResource {
                                .build();
                return response;
     }
-    
+
        @POST
     @Path("{indexid}")
        public Response reindex(
@@ -108,7 +108,7 @@ public class IndexResource extends NuxeoBasedResource {
                Response result = Response.ok().build();
                boolean success = false;
                String docType = null;
-               
+
         try {
             RemoteServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = (RemoteServiceContext<PoxPayloadIn, PoxPayloadOut>) createServiceContext(uriInfo);
             docType = ctx.getTenantQualifiedDoctype(); // this will used in the error message if an error occurs
@@ -117,19 +117,19 @@ public class IndexResource extends NuxeoBasedResource {
         } catch (Exception e) {
             throw bigReThrow(e, ServiceMessages.REINDEX_FAILED);
         }
-        
+
         if (success == false) {
             Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
                     ServiceMessages.REINDEX_FAILED + ServiceMessages.indexResourceNotFoundMsg(indexid)).type("text/plain").build();
             throw new CSWebApplicationException(response);
         }
-               
+
                return result;
     }
-    
+
        /* (non-Javadoc)
         * @see org.collectionspace.services.common.ResourceBase#getList(javax.ws.rs.core.UriInfo, java.lang.String)
-        * 
+        *
         * The index sub-resource does not support a getList operation.
         */
        @Override