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.concurrent.ExecutionException;
020import java.util.concurrent.Future;
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.core.observation.ObservationManager;
031import org.ametys.runtime.plugin.component.AbstractLogEnabled;
032
033/**
034 * Helper to control the Solr indexation
035 */
036public class SolrIndexHelper extends AbstractLogEnabled implements Serviceable, Component
037{
038    /** The Avalon Role */
039    public static final String ROLE = SolrIndexHelper.class.getName();
040    
041    private ObservationManager _observationManager;
042    private SolrIndexer _solrIndexer;
043
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        _observationManager = (ObservationManager) manager.lookup(ObservationManager.ROLE);
047        _solrIndexer = (SolrIndexer) manager.lookup(SolrIndexer.ROLE);
048    }
049    
050    /**
051     * Add an argument to all events of the provided ids to disable the committing of content to Solr
052     * @param handledEventIds the event ids that will not be committed
053     */
054    public void pauseSolrCommitForEvents(String[] handledEventIds)
055    {
056        _observationManager.addArgumentForEvents(handledEventIds, ObservationConstants.ARGS_CONTENT_COMMIT, false);
057    }
058    
059    /**
060     * Restart the committing of content to Solr for the provided event ids after committing all pending modification
061     * @param handledEventIds the event ids to restart
062     */
063    public void restartSolrCommitForEvents(String[] handledEventIds)
064    {
065        _observationManager.removeArgumentForEvents(handledEventIds, ObservationConstants.ARGS_CONTENT_COMMIT);
066        
067        // Before trying to commit, be sure all the async observers of the current request are finished
068        for (Future future : _observationManager.getFuturesForRequest())
069        {
070            try
071            {
072                future.get();
073            }
074            catch (ExecutionException | InterruptedException e)
075            {
076                getLogger().info("An exception occured when calling #get() on Future result of an observer." , e);
077            }
078        }
079        
080        // Commit all uncommitted changes
081        try
082        {
083            _solrIndexer.commit();
084            
085            getLogger().debug("Copied contents are now committed into Solr.");
086        }
087        catch (IOException | SolrServerException e)
088        {
089            getLogger().error("Impossible to commit changes", e);
090        }
091    }
092}