001/*
002 * (C) Copyright 2018 Nuxeo (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 *     Funsho David
018 */
019package org.nuxeo.ecm.core.bulk;
020
021import java.io.Serializable;
022import java.time.Duration;
023import java.util.List;
024import java.util.Map;
025
026import org.nuxeo.ecm.core.api.AsyncService;
027import org.nuxeo.ecm.core.bulk.message.BulkBucket;
028import org.nuxeo.ecm.core.bulk.message.BulkCommand;
029import org.nuxeo.ecm.core.bulk.message.BulkStatus;
030
031/**
032 * API to manage Bulk Computation.
033 * <p>
034 * At this level, there is no verification of the user calling the method against command's user.
035 * <p>
036 * This kind of verification has to be done by caller if needed.
037 *
038 * @since 10.2
039 */
040public interface BulkService extends AsyncService<String, BulkStatus, Map<String, Serializable>> {
041
042    /**
043     * Submits a {@link BulkCommand} that will be processed asynchronously, even if the current transaction rolls back
044     * (the method is not transactional).
045     *
046     * @param command the command to submit
047     * @return a unique bulk command identifier
048     */
049    String submit(BulkCommand command);
050
051    /**
052     * Returns the command or null if the command is not found or aborted.
053     */
054    BulkCommand getCommand(String commandId);
055
056    /**
057     * Waits for completion of given bulk command.
058     *
059     * @param commandId the command to wait
060     * @param duration the duration to wait
061     * @return {@code true} if bulk command completed or {@code false} if computation has not finished after the timeout
062     */
063    boolean await(String commandId, Duration duration) throws InterruptedException;
064
065    /**
066     * Waits for completion of all bulk commands.
067     *
068     * @param duration the duration to wait
069     * @return {@code true} if all bulk commands completed or {@code false} if one or more has not finished after the
070     *         timeout
071     * @since 10.3
072     */
073    boolean await(Duration duration) throws InterruptedException;
074
075    /**
076     * Gets the list of action statuses triggered by the given user.
077     *
078     * @param username the user name
079     * @return the list of statuses
080     * @since 10.3
081     */
082    List<BulkStatus> getStatuses(String username);
083
084    /**
085     * Sends a {@link BulkBucket} to the right bulk action.
086     * <p>
087     * This method is useful to produce bucket externally when using an external scroller.
088     *
089     * @param bucket the bucket to send to a bulk action
090     * @since 11.3
091     */
092    void appendExternalBucket(BulkBucket bucket);
093
094    /**
095     * Completes a running bulk computation having an external scroller.
096     *
097     * @param commandId the command id
098     * @param count the total number of elements produced by the external scroller
099     * @since 11.3
100     */
101    void completeExternalScroll(String commandId, long count);
102
103}