All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.jenkins.results.parser.Retryable Maven / Gradle / Ivy

The newest version!
/**
 * SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
 * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
 */

package com.liferay.jenkins.results.parser;

/**
 * @author Kenji Heigel
 */
public abstract class Retryable {

	public Retryable() {
		this(5, 30, true);
	}

	public Retryable(
		boolean exceptionOnFail, int maxRetries, int retryPeriod,
		boolean verbose) {

		_exceptionOnFail = exceptionOnFail;
		this.maxRetries = maxRetries;
		_retryPeriod = retryPeriod;
		_verbose = verbose;
	}

	public Retryable(int maxRetries, int retryPeriod, boolean verbose) {
		this(true, maxRetries, retryPeriod, verbose);
	}

	public abstract T execute();

	public final T executeWithRetries() {
		int retryCount = 0;

		while (true) {
			try {
				return execute();
			}
			catch (Exception exception) {
				retryCount++;

				if (_verbose) {
					System.out.println("An error has occurred: " + exception);
				}

				if ((maxRetries >= 0) && (retryCount > maxRetries)) {
					if (_exceptionOnFail) {
						throw exception;
					}

					return null;
				}

				sleep(_retryPeriod * 1000);

				String retryMessage = getRetryMessage(retryCount);

				if (!JenkinsResultsParserUtil.isNullOrEmpty(retryMessage)) {
					System.out.println(retryMessage);
				}
			}
		}
	}

	public void sleep(long duration) {
		try {
			Thread.sleep(duration);
		}
		catch (InterruptedException interruptedException) {
			throw new RuntimeException(interruptedException);
		}
	}

	protected String getRetryMessage(int retryCount) {
		return JenkinsResultsParserUtil.combine(
			"Retry attempt ", String.valueOf(retryCount), " of ",
			String.valueOf(maxRetries));
	}

	protected int maxRetries;

	private boolean _exceptionOnFail;
	private int _retryPeriod;
	private boolean _verbose;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy