001/*
002 * (C) Copyright 2018 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 *     bdelbosc
018 */
019package org.nuxeo.lib.stream.computation;
020
021import java.time.Duration;
022
023import net.jodah.failsafe.RetryPolicy;
024
025/**
026 * Defines how a computation should handle the retries, fallback and batching if any.
027 *
028 * @since 10.3
029 */
030public class ComputationPolicy {
031
032    public static final RetryPolicy NO_RETRY = new RetryPolicy().withMaxRetries(0);
033
034    /* No retry, abort on failure, no batching */
035    public static final ComputationPolicy NONE = new ComputationPolicyBuilder().retryPolicy(NO_RETRY)
036                                                                               .continueOnFailure(false)
037                                                                               .build();
038
039    protected final RetryPolicy retryPolicy;
040
041    protected final int batchCapacity;
042
043    protected final Duration batchThreshold;
044
045    protected final boolean skipFailure;
046
047    protected final int skipFirstFailures;
048
049    public ComputationPolicy(ComputationPolicyBuilder builder) {
050        batchCapacity = builder.batchCapacity;
051        batchThreshold = builder.batchThreshold;
052        skipFailure = builder.skipFailure;
053        retryPolicy = builder.retryPolicy;
054        skipFirstFailures = builder.skipFirstFailures;
055    }
056
057    public RetryPolicy getRetryPolicy() {
058        return retryPolicy;
059    }
060
061    public int getBatchCapacity() {
062        return batchCapacity;
063    }
064
065    public Duration getBatchThreshold() {
066        return batchThreshold;
067    }
068
069    public boolean continueOnFailure() {
070        return skipFailure;
071    }
072
073    public int getSkipFirstFailures() {
074        return skipFirstFailures;
075    }
076
077    /**
078     * @deprecated since 10.3 use {@link #continueOnFailure()} instead
079     */
080    @Deprecated
081    public boolean isSkipFailure() {
082        return skipFailure;
083    }
084
085    @Override
086    public String toString() {
087        return "ComputationPolicy{" + "maxRetries=" + retryPolicy.getMaxRetries() + ", delay=" + retryPolicy.getDelay()
088                + ", delayMax=" + retryPolicy.getMaxDelay() + ", continueOnFailure=" + skipFailure + ", batchCapacity="
089                + batchCapacity + ", batchThreshold=" + batchThreshold + '}';
090    }
091}