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

[WFMP-228] Handle the server state after CLI commands are executed. F… #422

Merged
merged 1 commit into from
Nov 18, 2023
Merged
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 @@ -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
9 changes: 7 additions & 2 deletions plugin/src/main/java/org/wildfly/plugin/dev/DevMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
// in the remote case, we have a war and must keep it.
requiresWarDeletion = !remote;
}
// We must start the server after compilation occured to get a deployment to scan
// We must start the server after compilation occurred to get a deployment to scan
if (!remote && isDiscoveryEnabled()) {
context = startServer(ServerType.STANDALONE);
}
Expand All @@ -467,7 +467,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 @@ -477,6 +477,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
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