001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others. 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 * 016 * Contributors: 017 * Benoit Delbosc 018 */ 019package org.nuxeo.ecm.automation.elasticsearch; 020 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023import org.nuxeo.ecm.automation.OperationContext; 024import org.nuxeo.ecm.automation.core.Constants; 025import org.nuxeo.ecm.automation.core.annotations.Context; 026import org.nuxeo.ecm.automation.core.annotations.Operation; 027import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 028import org.nuxeo.ecm.automation.core.annotations.Param; 029import org.nuxeo.ecm.core.api.CoreSession; 030import org.nuxeo.ecm.core.work.api.WorkManager; 031import org.nuxeo.elasticsearch.api.ElasticSearchAdmin; 032import org.nuxeo.runtime.api.Framework; 033 034import java.util.concurrent.ExecutionException; 035import java.util.concurrent.TimeUnit; 036import java.util.concurrent.TimeoutException; 037 038import static java.lang.Long.max; 039 040/** 041 * Wait for Elasticsearch indexing background job 042 * 043 * @since 8.1 044 */ 045@Operation(id = ElasticsearchWaitForIndexingOperation.ID, category = Constants.CAT_SERVICES, label = "Wait for Elasticsearch Indexing", 046 since = "8.1", 047 description = "Wait until Elasticsearch indexing is done.") 048public class ElasticsearchWaitForIndexingOperation { 049 050 public static final String ID = "Elasticsearch.WaitForIndexing"; 051 052 private static final Log log = LogFactory.getLog(Log.class); 053 054 @Context 055 protected OperationContext ctx; 056 057 @Context 058 protected ElasticSearchAdmin esa; 059 060 @Context 061 protected CoreSession repo; 062 063 @Param(name = "timeoutSecond", required = false) 064 protected Integer timeout = 60; 065 066 @Param(name = "refresh", required = false) 067 protected Boolean refresh = false; 068 069 @OperationMethod 070 public Boolean run() { 071 long start = System.currentTimeMillis(); 072 WorkManager workManager = Framework.getService(WorkManager.class); 073 try { 074 if (!workManager.awaitCompletion(timeout, TimeUnit.SECONDS)) { 075 throw new TimeoutException(); 076 } 077 esa.prepareWaitForIndexing().get(computeRemainingTime(start), TimeUnit.SECONDS); 078 } catch (TimeoutException e) { 079 return Boolean.FALSE; 080 } catch (InterruptedException e) { 081 return Boolean.FALSE; 082 } catch (ExecutionException e) { 083 return Boolean.FALSE; 084 } 085 if (refresh) { 086 esa.refreshRepositoryIndex(repo.getRepositoryName()); 087 } 088 return Boolean.TRUE; 089 } 090 091 protected long computeRemainingTime(long start) { 092 long elapsed = System.currentTimeMillis() - start; 093 // at least one second 094 return max(timeout - TimeUnit.MILLISECONDS.toSeconds(elapsed), 1); 095 } 096 097}