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

PAYARA-2171 Add timeout parameter to start instance command #2299

Merged
merged 2 commits into from
Jan 22, 2018
Merged
Changes from 1 commit
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 @@ -36,31 +36,43 @@
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*
* Portions Copyright [2018] [Payara Foundation and/or its affiliates]
*/

package com.sun.enterprise.v3.admin.cluster;

import com.sun.enterprise.config.serverbeans.*;
import com.sun.enterprise.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import com.sun.enterprise.util.SystemPropertyConstants;
import javax.inject.Inject;
import javax.validation.constraints.Min;

import com.sun.enterprise.config.serverbeans.Node;
import com.sun.enterprise.config.serverbeans.Nodes;
import com.sun.enterprise.config.serverbeans.Server;
import com.sun.enterprise.config.serverbeans.Servers;
import com.sun.enterprise.util.OS;
import com.sun.enterprise.util.StringUtils;

import org.glassfish.api.ActionReport;
import org.glassfish.api.I18n;
import org.glassfish.api.Param;
import org.glassfish.api.admin.*;
import javax.inject.Inject;

import org.jvnet.hk2.annotations.Service;

import java.util.ArrayList;
import java.util.List;

import org.glassfish.api.admin.AdminCommand;
import org.glassfish.api.admin.AdminCommandContext;
import org.glassfish.api.admin.CommandLock;
import org.glassfish.api.admin.RestEndpoint;
import org.glassfish.api.admin.RestEndpoints;
import org.glassfish.api.admin.RestParam;
import org.glassfish.api.admin.ServerEnvironment;
import org.glassfish.hk2.api.PerLookup;
import org.glassfish.hk2.api.ServiceLocator;

import com.sun.enterprise.config.serverbeans.Node;
import org.jvnet.hk2.annotations.Service;


/**
Expand All @@ -85,6 +97,7 @@
})
})
public class StartInstanceCommand implements AdminCommand {

@Inject
ServiceLocator habitat;

Expand All @@ -111,7 +124,11 @@ public class StartInstanceCommand implements AdminCommand {

@Param(optional = true, obsolete = true)
private String setenv;


@Min(message = "Timeout must be at least 1 second long.", value = 1)
@Param(optional = true, defaultValue = "120")
private int timeout;

private Logger logger;

private Node node;
Expand Down Expand Up @@ -277,21 +294,29 @@ private void startInstance(AdminCommandContext ctx) {
}

// return null means A-OK
private String pollForLife(Server instance) {
int counter = 0; // 120 seconds

while (++counter < 240) {
if (instance.isRunning())
return null;

try {
Thread.sleep(500);
}
catch (Exception e) {
// ignore
private String pollForLife(final Server instance) {

// Start a new thread to check when the instance has started
final CountDownLatch instanceTimeout = new CountDownLatch(1);
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.schedule(new Runnable() {
@Override
public void run() {
if (instance.isRunning()) {
instanceTimeout.countDown();
}
}
}, 500, TimeUnit.MILLISECONDS);

// If the timeout is reached, return the timeout message. Otherwise return null (success).
try {
instanceTimeout.await(timeout, TimeUnit.SECONDS);
} catch (InterruptedException e) {
return Strings.get("start.instance.timeout", instanceName);
} finally {
executor.shutdown();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe put the "try" higher up? If executor.schedule throws an exception, the executor might not be shut down.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I've moved it higher up

}
return Strings.get("start.instance.timeout", instanceName);
return null;
}

private String makeCommandHuman(List<String> command) {
Expand Down