001/* 002 * Copyright 2022 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.cms.indexing.solr; 017 018import java.io.IOException; 019import java.util.List; 020import java.util.concurrent.ExecutionException; 021 022import org.apache.avalon.framework.component.Component; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.avalon.framework.service.Serviceable; 026import org.apache.solr.client.solrj.SolrServerException; 027 028import org.ametys.cms.ObservationConstants; 029import org.ametys.cms.content.indexing.solr.SolrIndexer; 030import org.ametys.cms.indexing.IndexingObserver; 031import org.ametys.core.observation.ObservationManager; 032import org.ametys.core.observation.ObservationManager.ObserverFuture; 033import org.ametys.runtime.plugin.component.AbstractLogEnabled; 034 035/** 036 * Helper to control the Solr indexation 037 */ 038public class SolrIndexHelper extends AbstractLogEnabled implements Serviceable, Component 039{ 040 /** The Avalon Role */ 041 public static final String ROLE = SolrIndexHelper.class.getName(); 042 043 private ObservationManager _observationManager; 044 private SolrIndexer _solrIndexer; 045 046 public void service(ServiceManager manager) throws ServiceException 047 { 048 _observationManager = (ObservationManager) manager.lookup(ObservationManager.ROLE); 049 _solrIndexer = (SolrIndexer) manager.lookup(SolrIndexer.ROLE); 050 } 051 052 /** 053 * Add an argument to all events of the provided ids to disable the committing of content to Solr 054 * @param handledEventIds the event ids that will not be committed 055 */ 056 public void pauseSolrCommitForEvents(String[] handledEventIds) 057 { 058 _observationManager.addArgumentForEvents(handledEventIds, ObservationConstants.ARGS_CONTENT_COMMIT, false); 059 } 060 061 /** 062 * Restart the committing of content to Solr for the provided event ids after committing all pending modification 063 * @param handledEventIds the event ids to restart 064 */ 065 public void restartSolrCommitForEvents(String[] handledEventIds) 066 { 067 _observationManager.removeArgumentForEvents(handledEventIds, ObservationConstants.ARGS_CONTENT_COMMIT); 068 069 // Before trying to commit, be sure all the async observers of the current request are finished 070 List<ObserverFuture> futuresForRequest = _observationManager.getFuturesForRequest(); 071 for (ObserverFuture observerFuture : futuresForRequest) 072 { 073 if (observerFuture.traits().contains(IndexingObserver.INDEXING_OBSERVER)) 074 { 075 try 076 { 077 observerFuture.future().get(); 078 } 079 catch (ExecutionException | InterruptedException e) 080 { 081 getLogger().info("An exception occured when calling #get() on Future result of an observer." , e); 082 } 083 } 084 } 085 086 // Commit all uncommitted changes 087 try 088 { 089 _solrIndexer.commit(); 090 091 getLogger().debug("Copied contents are now committed into Solr."); 092 } 093 catch (IOException | SolrServerException e) 094 { 095 getLogger().error("Impossible to commit changes", e); 096 } 097 } 098}