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    public ComputationPolicy(ComputationPolicyBuilder builder) {
048        batchCapacity = builder.batchCapacity;
049        batchThreshold = builder.batchThreshold;
050        skipFailure = builder.skipFailure;
051        retryPolicy = builder.retryPolicy;
052    }
053
054    public RetryPolicy getRetryPolicy() {
055        return retryPolicy;
056    }
057
058    public int getBatchCapacity() {
059        return batchCapacity;
060    }
061
062    public Duration getBatchThreshold() {
063        return batchThreshold;
064    }
065
066    public boolean continueOnFailure() {
067        return skipFailure;
068    }
069
070    /**
071     * @deprecated since 10.3 use {@link #continueOnFailure()} instead
072     */
073    @Deprecated
074    public boolean isSkipFailure() {
075        return skipFailure;
076    }
077
078    @Override
079    public String toString() {
080        return "ComputationPolicy{" + "maxRetries=" + retryPolicy.getMaxRetries() + ", delay=" + retryPolicy.getDelay()
081                + ", delayMax=" + retryPolicy.getMaxDelay() + ", continueOnFailure=" + skipFailure + ", batchCapacity="
082                + batchCapacity + ", batchThreshold=" + batchThreshold + '}';
083    }
084}