Skip to content

Commit

Permalink
Adding serverReadyAttempts option (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-brandizi authored and André Hartmann committed Aug 26, 2018
1 parent 3803623 commit 32bbacd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
3 changes: 2 additions & 1 deletion integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>com.github.harti2006</groupId>
<artifactId>integration-tests</artifactId>
<version>0.2-SNAPSHOT</version>
<version>1.1-SNAPSHOT</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
Expand Down Expand Up @@ -82,6 +82,7 @@
<password>${neo4j-server.password}</password>
<version>${neo4j-server.version}</version>
<deleteDb>true</deleteDb>
<serverReadyAttempts>5</serverReadyAttempts>
</configuration>
<executions>
<execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ public abstract class Neo4jServerMojoSupport extends AbstractMojo {
@Parameter(required = true, property = "neo4j-server.deleteDb", defaultValue = "false")
protected boolean deleteDb;

/**
* Makes this number of attempts to wait for server ready conditions when the Neo4j server is started
* in {@link StartNeo4jServerMojo}. After the start command, we check the server by connecting it via
* BOLT. If that doesn't work, we start making a connection attempt every second, until we connect it
* successfully or we reach a number of attempts equal to this parameter. The time spent this way can be
* configured and adapted to your particular situation, e.g., in some busy servers Neo4j can take over
* 1 min to start.
*/
@Parameter(required = true, property = "neo4j-server.serverReadyAttempts", defaultValue = "10")
protected int serverReadyAttempts = 10;

protected Path getServerLocation() {
return Paths.get(directory, ARTIFACT_NAME + version);
}
Expand Down
25 changes: 16 additions & 9 deletions src/main/java/com/github/harti2006/neo4j/StartNeo4jServerMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
@Mojo(name = "start", defaultPhase = PRE_INTEGRATION_TEST)
public class StartNeo4jServerMojo extends Neo4jServerMojoSupport {

private static final int INITIAL_WAIT_MILLIS = 1500;
private static final int WAIT_MILLIS = 1000;

public void execute() throws MojoExecutionException {
installNeo4jServer();
configureNeo4jServer();
Expand Down Expand Up @@ -145,26 +148,30 @@ private void startNeo4jServer() throws MojoExecutionException {
}
}

/**
* @see Neo4jServerMojoSupport#serverReadyAttempts
*/
private void checkServerReady() throws MojoExecutionException, InterruptedException {
// If the deleteDb parameter is true, at this point we have created a new server, which have the
// default
// password, and we're about to change this.
//
String pwd = deleteDb ? "neo4j" : password;
int maxAttempts = 10;
Thread.sleep(1500); // It takes some time anyway
Thread.sleep(INITIAL_WAIT_MILLIS); // It takes some time anyway

for (int attempts = 1; attempts <= maxAttempts; attempts++) {
for (int attempts = 1; attempts <= serverReadyAttempts; attempts++) {
getLog().debug("Trying to connect Neo4j, attempt " + attempts);

try (Driver driver = GraphDatabase.driver("bolt://127.0.0.1:" + boltPort,
AuthTokens.basic("neo4j", pwd));) {
try (Driver ignored = GraphDatabase.driver("bolt://127.0.0.1:" + boltPort,
AuthTokens.basic("neo4j", pwd))) {
return;
} catch (ServiceUnavailableException ex) {
Thread.sleep(1000);
} catch (ServiceUnavailableException ignored) {
Thread.sleep(WAIT_MILLIS);
}
}
throw new MojoExecutionException("Server doesn't result started after waiting for its boot");
throw new MojoExecutionException(
format("Server doesn't result started after waiting %sms for its boot",
INITIAL_WAIT_MILLIS + serverReadyAttempts * WAIT_MILLIS));
}

private void setNewPassword() {
Expand All @@ -174,7 +181,7 @@ private void setNewPassword() {

try (Driver driver = GraphDatabase.driver("bolt://127.0.0.1:" + boltPort,
AuthTokens.basic("neo4j", "neo4j"));
Session session = driver.session();) {
Session session = driver.session()) {
session.run("CALL dbms.changePassword( '" + password + "' )");
}
}
Expand Down

0 comments on commit 32bbacd

Please sign in to comment.