Skip to content

Commit

Permalink
Merge pull request #423 from jamezp/WFMP-225-4.2
Browse files Browse the repository at this point in the history
[WFMP-228] Handle the server state after CLI commands are executed. F…
  • Loading branch information
jamezp committed Nov 18, 2023
2 parents 2614cdf + d0912f1 commit 772dfb9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package org.wildfly.plugin.cli;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -35,19 +36,23 @@
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.RemoteRepository;
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.client.helpers.ClientConstants;
import org.jboss.galleon.maven.plugin.util.MavenArtifactRepositoryManager;
import org.jboss.galleon.universe.maven.repo.MavenRepoManager;
import org.wildfly.plugin.common.AbstractServerConnection;
import org.wildfly.plugin.common.PropertyNames;
import org.wildfly.plugin.common.Utils;
import org.wildfly.plugin.core.MavenRepositoriesEnricher;
import org.wildfly.plugin.core.ServerHelper;

/**
* Execute commands to the running WildFly Application Server.
Expand Down Expand Up @@ -255,6 +260,25 @@ public void execute() throws MojoExecutionException, MojoFailureException {
cmdConfigBuilder.setJBossHome(getInstallation(buildDir.toPath().resolve(Utils.WILDFLY_DEFAULT_DIR)));
}
commandExecutor.execute(cmdConfigBuilder.build(), mavenRepoManager);
// Check the server state if we're not in offline mode
if (!offline) {
try (ModelControllerClient client = createClient()) {
final String serverState = ServerHelper.serverState(client);
if (!ClientConstants.CONTROLLER_PROCESS_STATE_RUNNING.equals(serverState)) {
getLog().warn(String.format(
"The server may be in an unexpected state for further interaction. The current state is %s",
serverState));
}
} catch (IOException e) {
final Log log = getLog();
log.warn(String.format(
"Failed to determine the server-state. The server may be in an unexpected state. Failure: %s",
e.getMessage()));
if (log.isDebugEnabled()) {
log.debug(e);
}
}
}
}

/**
Expand Down
7 changes: 6 additions & 1 deletion plugin/src/main/java/org/wildfly/plugin/dev/DevMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
.addCommands(commands)
.addScripts(scripts)
.setStdout("none")
.setAutoReload(true)
.setAutoReload(false)
.setTimeout(timeout);
if (context == null) {
builder.setOffline(false)
Expand All @@ -440,6 +440,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
.setFork(true);
}
commandExecutor.execute(builder.build(), mavenRepoManager);
// Check the server state. We may need to restart the process, assuming we control the context
if (context != null) {
context = actOnServerState(client, context);
}

final DeploymentManager deploymentManager = DeploymentManager.Factory.create(client);
final Deployment deployment = getDeploymentContent();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.maven.execution.MavenSession;
Expand All @@ -39,6 +40,8 @@
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.RemoteRepository;
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.client.helpers.ClientConstants;
import org.jboss.as.controller.client.helpers.Operations;
import org.jboss.as.controller.client.helpers.domain.DomainClient;
import org.jboss.galleon.ProvisioningException;
import org.jboss.galleon.maven.plugin.util.MavenArtifactRepositoryManager;
Expand Down Expand Up @@ -435,6 +438,44 @@ protected Path provisionIfRequired(final Path installDir) throws MojoFailureExce
}
}

/**
* Checks the current state of the server. If the server is in a state of
* {@link ClientConstants#CONTROLLER_PROCESS_STATE_RESTART_REQUIRED}, the process is restarted and a new
* {@link ServerContext} is returned. If the server is in a stat of
* {@link ClientConstants#CONTROLLER_PROCESS_STATE_RELOAD_REQUIRED}, the server will be reloaded and wait until
* the server is running. If the server is in any other state, other than
* {@link ClientConstants#CONTROLLER_PROCESS_STATE_RUNNING}, a warning message is logged to let the user know
* the state is unknown.
*
* @param client the client used to communicate with the server
* @param context the current server context
* @return a new context if a restart was required, otherwise the same context
* @throws IOException if an error occurs communicating with the server
* @throws MojoExecutionException if a failure occurs checking the state or reloading the server
* @throws MojoFailureException if a failure occurs checking the state or reloading the server
*/
protected ServerContext actOnServerState(final ModelControllerClient client, final ServerContext context)
throws IOException, MojoExecutionException, MojoFailureException {
final String serverState = ServerHelper.serverState(client);
if (ClientConstants.CONTROLLER_PROCESS_STATE_RESTART_REQUIRED.equals(serverState)) {
// Shutdown the server
ServerHelper.shutdownStandalone(client, timeout);
// Restart the server process
return startServer(ServerType.STANDALONE);
} else if (ClientConstants.CONTROLLER_PROCESS_STATE_RELOAD_REQUIRED.equals(serverState)) {
ServerHelper.executeReload(client, Operations.createOperation("reload"));
try {
ServerHelper.waitForStandalone(context.process(), client, timeout);
} catch (InterruptedException | TimeoutException e) {
throw new MojoExecutionException("Failed to wait for standalone server after a reload.", e);
}
} else if (!ClientConstants.CONTROLLER_PROCESS_STATE_RUNNING.equals(serverState)) {
getLog().warn(String.format(
"The server may be in an unexpected state for further interaction. The current state is %s", serverState));
}
return context;
}

private void addUsers(final Path wildflyHome, final Path javaHome) throws IOException {
if (addUser != null && addUser.hasUsers()) {
getLog().info("Adding users: " + addUser);
Expand Down
4 changes: 3 additions & 1 deletion plugin/src/main/java/org/wildfly/plugin/server/RunMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,14 @@ public void execute() throws MojoExecutionException, MojoFailureException {
.addCommands(commands)
.addScripts(scripts)
.setJBossHome(context.jbossHome())
.setAutoReload(true)
.setAutoReload(false)
.setFork(true)
.setStdout("none")
.setTimeout(timeout)
.build();
commandExecutor.execute(cmdConfig, mavenRepoManager);

process = actOnServerState(client, context).process();
// Create the deployment and deploy
final Deployment deployment = Deployment.of(deploymentContent)
.setName(name)
Expand Down

0 comments on commit 772dfb9

Please sign in to comment.