Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configurable number of retries and interval between retries #1098

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public final class InstallNodeAndNpmMojo extends AbstractFrontendMojo {
@Parameter(property = "skip.installnodenpm", defaultValue = "${skip.installnodenpm}")
private boolean skip;

@Parameter(property = "numberOfRetries", defaultValue = "1")
private int numberOfRetries;

@Parameter(property = "intervalBetweenRetriesMs", defaultValue = "10000")
private int intervalBetweenRetriesMs;

@Component(role = SettingsDecrypter.class)
private SettingsDecrypter decrypter;

Expand All @@ -82,12 +88,16 @@ public void execute(FrontendPluginFactory factory) throws InstallationException
.setNodeVersion(nodeVersion)
.setNodeDownloadRoot(nodeDownloadRoot)
.setNpmVersion(npmVersion)
.setNumberOfRetries(numberOfRetries)
.setIntervalBetweenRetries(intervalBetweenRetriesMs)
.setUserName(server.getUsername())
.setPassword(server.getPassword())
.install();
factory.getNPMInstaller(proxyConfig)
.setNodeVersion(nodeVersion)
.setNpmVersion(npmVersion)
.setNumberOfRetries(numberOfRetries)
.setIntervalBetweenRetries(intervalBetweenRetriesMs)
.setNpmDownloadRoot(npmDownloadRoot)
.setUserName(server.getUsername())
.setPassword(server.getPassword())
Expand All @@ -97,10 +107,14 @@ public void execute(FrontendPluginFactory factory) throws InstallationException
.setNodeVersion(nodeVersion)
.setNodeDownloadRoot(nodeDownloadRoot)
.setNpmVersion(npmVersion)
.setNumberOfRetries(numberOfRetries)
.setIntervalBetweenRetries(intervalBetweenRetriesMs)
.install();
factory.getNPMInstaller(proxyConfig)
.setNodeVersion(this.nodeVersion)
.setNpmVersion(this.npmVersion)
.setNumberOfRetries(numberOfRetries)
.setIntervalBetweenRetries(intervalBetweenRetriesMs)
.setNpmDownloadRoot(npmDownloadRoot)
.install();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public final class InstallNodeAndYarnMojo extends AbstractFrontendMojo {
@Parameter(property = "skip.installyarn", alias = "skip.installyarn", defaultValue = "${skip.installyarn}")
private boolean skip;

@Parameter(property = "numberOfRetries", defaultValue = "1")
private int numberOfRetries;

@Parameter(property = "intervalBetweenRetriesMs", defaultValue = "10000")
private int intervalBetweenRetriesMs;

@Component(role = SettingsDecrypter.class)
private SettingsDecrypter decrypter;

Expand Down Expand Up @@ -95,6 +101,7 @@ public void execute(FrontendPluginFactory factory) throws InstallationException
.setUserName(server.getUsername()).install();
factory.getYarnInstaller(proxyConfig).setYarnDownloadRoot(this.yarnDownloadRoot)
.setYarnVersion(this.yarnVersion).setUserName(server.getUsername())
.setNumberOfRetries(numberOfRetries).setIntervalBetweenRetries(intervalBetweenRetriesMs)
.setPassword(server.getPassword()).setIsYarnBerry(isYarnrcYamlFilePresent()).install();
} else {
factory.getNodeInstaller(proxyConfig).setNodeDownloadRoot(this.nodeDownloadRoot)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public class NPMInstaller {

private String nodeVersion, npmVersion, npmDownloadRoot, userName, password;

private int numberOfRetries;

private int intervalBetweenRetries;

private final Logger logger;

private final InstallConfig config;
Expand Down Expand Up @@ -60,6 +64,16 @@ public NPMInstaller setPassword(String password) {
return this;
}

public NPMInstaller setNumberOfRetries(int numberOfRetries) {
this.numberOfRetries = numberOfRetries;
return this;
}

public NPMInstaller setIntervalBetweenRetries(int intervalBetweenRetriesMs) {
this.intervalBetweenRetries = intervalBetweenRetriesMs;
return this;
}

private boolean npmProvided() throws InstallationException {
if ("provided".equals(this.npmVersion)) {
if (Integer.parseInt(this.nodeVersion.replace("v", "").split("[.]")[0]) < 4) {
Expand All @@ -79,7 +93,24 @@ public void install() throws InstallationException {
this.npmDownloadRoot = DEFAULT_NPM_DOWNLOAD_ROOT;
}
if (!npmProvided() && !npmIsAlreadyInstalled()) {
installNpm();
int attemptNumber = 0;
boolean status = false;
while (!status && numberOfRetries > attemptNumber++) {
try {
this.logger.info("Install npm attempt #{}", attemptNumber);
installNpm();
} catch (Exception e) {
if (numberOfRetries <= attemptNumber) {
throw e;
}
this.logger.error("Install npm attempt #{} failed", attemptNumber, e);
try {
Thread.sleep(intervalBetweenRetries);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}
}
}
copyNpmScripts();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class NodeInstaller {

private String npmVersion, nodeVersion, nodeDownloadRoot, userName, password;

private int numberOfRetries;
private int intervalBetweenRetries;

private final Logger logger;

private final InstallConfig config;
Expand Down Expand Up @@ -60,6 +63,16 @@ public NodeInstaller setPassword(String password) {
return this;
}

public NodeInstaller setNumberOfRetries(int numberOfRetries) {
this.numberOfRetries = numberOfRetries;
return this;
}

public NodeInstaller setIntervalBetweenRetries(int intervalBetweenRetries) {
this.intervalBetweenRetries = intervalBetweenRetries;
return this;
}

private boolean npmProvided() throws InstallationException {
if (this.npmVersion != null) {
if ("provided".equals(this.npmVersion)) {
Expand All @@ -85,14 +98,31 @@ public void install() throws InstallationException {
if (!this.nodeVersion.startsWith("v")) {
this.logger.warn("Node version does not start with naming convention 'v'.");
}
if (this.config.getPlatform().isWindows()) {
if (npmProvided()) {
installNodeWithNpmForWindows();
} else {
installNodeForWindows();
int attemptNumber = 0;
boolean status = false;
while (!status && numberOfRetries > attemptNumber++) {
try {
this.logger.info("Install node attempt #{}", attemptNumber);
if (this.config.getPlatform().isWindows()) {
if (npmProvided()) {
installNodeWithNpmForWindows();
} else {
installNodeForWindows();
}
} else {
installNodeDefault();
}
} catch (Exception e) {
if (numberOfRetries <= attemptNumber) {
throw e;
}
this.logger.error("Install node attempt #{} failed", attemptNumber, e);
try {
Thread.sleep(intervalBetweenRetries);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}
} else {
installNodeDefault();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public class YarnInstaller {

private String yarnVersion, yarnDownloadRoot, userName, password;

private int numberOfRetries;

private int intervalBetweenRetries;

private boolean isYarnBerry;

private final Logger logger;
Expand Down Expand Up @@ -65,6 +69,16 @@ public YarnInstaller setPassword(String password) {
return this;
}

public YarnInstaller setNumberOfRetries(int numberOfRetries) {
this.numberOfRetries = numberOfRetries;
return this;
}

public YarnInstaller setIntervalBetweenRetries(int intervalBetweenRetries) {
this.intervalBetweenRetries = intervalBetweenRetries;
return this;
}

public void install() throws InstallationException {
// use static lock object for a synchronized block
synchronized (LOCK) {
Expand All @@ -75,7 +89,24 @@ public void install() throws InstallationException {
if (!yarnVersion.startsWith("v")) {
throw new InstallationException("Yarn version has to start with prefix 'v'.");
}
installYarn();
int attemptNumber = 0;
boolean status = false;
while (!status && numberOfRetries > attemptNumber++) {
try {
this.logger.info("Install yarn attempt #{}", attemptNumber);
installYarn();
} catch (Exception e) {
if (numberOfRetries <= attemptNumber) {
throw e;
}
this.logger.error("Install yarn attempt #{} failed", attemptNumber, e);
try {
Thread.sleep(intervalBetweenRetries);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}
}
}
}
}
Expand Down