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