From 2090601a74429ec84469dbd1eea218c4e0100ab0 Mon Sep 17 00:00:00 2001 From: Ray Lee Date: Fri, 8 Mar 2024 20:33:48 -0500 Subject: [PATCH] Attempt to start ElasticSearchService when reindexing if connection failed on startup. --- .../java/NuxeoRepositoryClientImpl.java | 28 ++++++++++++++++++- .../services/index/IndexResource.java | 18 ++++++------ 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/NuxeoRepositoryClientImpl.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/NuxeoRepositoryClientImpl.java index eed6e4f4b..8621efb21 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/NuxeoRepositoryClientImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/NuxeoRepositoryClientImpl.java @@ -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 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)); diff --git a/services/index/service/src/main/java/org/collectionspace/services/index/IndexResource.java b/services/index/service/src/main/java/org/collectionspace/services/index/IndexResource.java index 4693e9363..2401a0b5d 100644 --- a/services/index/service/src/main/java/org/collectionspace/services/index/IndexResource.java +++ b/services/index/service/src/main/java/org/collectionspace/services/index/IndexResource.java @@ -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 ctx = (RemoteServiceContext) 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 -- 2.47.3