001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Antoine Taillefer <ataillefer@nuxeo.com>
016 */
017package org.nuxeo.drive.elasticsearch.operations.test;
018
019import java.util.concurrent.ExecutionException;
020import java.util.concurrent.TimeUnit;
021import java.util.concurrent.TimeoutException;
022
023import org.nuxeo.drive.operations.test.NuxeoDriveIntegrationTestsHelper;
024import org.nuxeo.ecm.automation.core.Constants;
025import org.nuxeo.ecm.automation.core.annotations.Operation;
026import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
027import org.nuxeo.elasticsearch.ElasticSearchConstants;
028import org.nuxeo.elasticsearch.api.ElasticSearchAdmin;
029import org.nuxeo.runtime.api.Framework;
030
031/**
032 * Waits for Elasticsearch audit completion.
033 *
034 * @since 7.3
035 */
036@Operation(id = NuxeoDriveWaitForElasticsearchCompletion.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Wait for Elasticsearch audit completion")
037public class NuxeoDriveWaitForElasticsearchCompletion {
038
039    public static final String ID = "NuxeoDrive.WaitForElasticsearchCompletion";
040
041    @OperationMethod
042    public void run() throws InterruptedException, ExecutionException, TimeoutException {
043        NuxeoDriveIntegrationTestsHelper.checkOperationAllowed();
044        NuxeoDriveIntegrationTestsHelper.waitForAsyncCompletion();
045        ElasticSearchAdmin esa = Framework.getService(ElasticSearchAdmin.class);
046        // Wait for indexing
047        esa.prepareWaitForIndexing().get(20, TimeUnit.SECONDS);
048        // Explicit refresh
049        esa.refresh();
050        // Explicit refresh for the audit index until it is handled by esa.refresh
051        esa.getClient().admin().indices().prepareRefresh(esa.getIndexNameForType(ElasticSearchConstants.ENTRY_TYPE)).get();
052    }
053
054}