Best SeLion code snippet using com.paypal.selion.grid.AbstractBaseProcessLauncher.shutdown
Source:AbstractBaseProcessLauncher.java
...51abstract class AbstractBaseProcessLauncher extends AbstractBaseLauncher {52 private static final SeLionGridLogger LOGGER = SeLionGridLogger.getLogger(AbstractBaseProcessLauncher.class);53 private DefaultExecuteResultHandler handler;54 private ProcessLauncherOptions launcherOptions;55 private boolean shutdownCalled;56 private final SeLionExecuteWatchDog watchdog = new SeLionExecuteWatchDog(ExecuteWatchdog.INFINITE_TIMEOUT);57 /*58 * The command line to run59 */60 private CommandLine cmdLine;61 /**62 * Get the sub-process pid as an integer63 */64 int getProcessPID() {65 return watchdog.getProcessId();66 }67 class SeLionExecuteWatchDog extends ExecuteWatchdog {68 boolean starting;69 Process process;70 SeLionExecuteWatchDog(long timeout) {71 super(timeout);72 }73 public int getProcessId() {74 if (SystemUtils.IS_OS_WINDOWS) {75 // TODO implement me76 throw new IllegalStateException(77 "Implementation missing.. No means to detect sub process pid on Windows");78 }79 try {80 Field f = process.getClass().getDeclaredField("pid");81 f.setAccessible(true);82 Integer pid = (Integer) f.get(process);83 return pid;84 } catch (Exception e) { // NOSONAR85 throw new RuntimeException("Couldn't detect sub process pid", e);86 }87 }88 @Override89 public synchronized void start(Process process) {90 this.process = process;91 starting = false;92 super.start(process);93 }94 public void reset() {95 starting = true;96 }97 private void waitForProcessStarted() {98 while (starting) {99 try {100 Thread.sleep(50);101 } catch (InterruptedException e) {102 throw new WebDriverException(e);103 }104 }105 }106 private void waitForTerminationAfterDestroy(int duration, TimeUnit unit) {107 long end = System.currentTimeMillis() + unit.toMillis(duration);108 while (isRunning() && System.currentTimeMillis() < end) {109 try {110 Thread.sleep(50);111 } catch (InterruptedException e) {112 throw new WebDriverException(e);113 }114 }115 }116 public void destroyProcessForcefully() {117 try {118 Process awaitFor = this.process.destroyForcibly();119 awaitFor.waitFor(10, SECONDS);120 } catch (InterruptedException e) {121 Thread.interrupted();122 throw new RuntimeException(e);123 }124 }125 }126 Thread shutDownHook = new Thread() {127 @Override128 public void run() {129 shutdown();130 }131 };132 /**133 * @return the command line to invoke as a {@link CommandLine}134 */135 CommandLine getCommandLine() {136 return cmdLine;137 }138 /**139 * Set the command line to invoke140 *141 * @param commandLine142 * a {@link CommandLine} to invoke143 */144 void setCommandLine(CommandLine commandLine) {145 this.cmdLine = commandLine;146 }147 /**148 * Init with the supplied {@code args} and a default {@link ProcessLauncherConfiguration}. All {@code args} take149 * precedence over any other values.150 *151 * @param args152 * The program arguments to use. Can be a mix of SeLion and selenium arguments.153 */154 void init(String[] args) {155 init(args, null);156 }157 /**158 * Init with the supplied dash {@code args} and the supplied {@link ProcessLauncherOptions}. All {@code args} take159 * precedence over the {@code options} and/or other values.160 *161 * @param args162 * The program arguments to use. Can be a mix of SeLion and selenium arguments.163 * @param options164 * {@link ProcessLauncherOptions} to consider165 */166 void init(String[] args, ProcessLauncherOptions options) {167 ProcessLauncherConfiguration plc = new ProcessLauncherConfiguration();168 plc.merge(options);169 JCommander commander = new JCommander();170 commander.setAcceptUnknownOptions(true);171 commander.addObject(plc);172 try {173 commander.parse(args);174 // we need to consider the selionConfig file when the caller is providing175 // a non-default selionConfig file location176 if (plc.getSeLionConfig() != SELION_CONFIG_FILE) {177 // reload the config from the file178 plc = ProcessLauncherConfiguration.loadFromFile(plc.getSeLionConfig());179 // re-merge the options180 plc.merge(options);181 // re-parse the args182 commander = new JCommander();183 commander.setAcceptUnknownOptions(true);184 commander.addObject(plc);185 commander.parse(args);186 }187 } catch (ParameterException | IOException e) {188 LOGGER.log(Level.SEVERE, e.getMessage(), e);189 System.exit(1);190 }191 setLauncherOptions(plc);192 // save off all the command line that were provided.193 List<String> commands = new LinkedList<>(Arrays.asList(args));194 setCommands(commands);195 // setup the SeLion config196 ConfigParser.setConfigFile(plc.getSeLionConfig());197 InstallHelper.firstTimeSetup();198 }199 /**200 * This method spawns a jar, and waits for it to exit [either cleanly or forcibly]201 *202 * @param interval203 * How often should the application check if the command is still running or if it exit.204 * @throws IOException205 * @throws InterruptedException206 */207 final void continuouslyRestart(long interval) throws IOException, InterruptedException {208 LOGGER.entering(interval);209 while (true) {210 if (!isInitialized()) {211 FileDownloader.checkForDownloads(getType(), getLauncherOptions()212 .isFileDownloadCheckTimeStampOnInvocation(), getLauncherOptions()213 .isFileDownloadCleanupOnInvocation());214 }215 startProcess(false);216 while (!handler.hasResult()) {217 LOGGER.fine("Child process still running. Going back to sleep.");218 Thread.sleep(interval);219 }220 if (handler.hasResult()) {221 ExecuteException e = handler.getException();222 if (e != null) {223 LOGGER.log(Level.SEVERE, handler.getException().getMessage(), handler.getException());224 }225 }226 LOGGER.info("Child process quit. Restarting it.");227 setInitialized(false);228 }229 }230 /**231 * Start a process based on the commands provided.232 *233 * @param squelch234 * Whether to show command executed as a logger.info message235 * @throws IOException236 */237 void startProcess(boolean squelch) throws IOException {238 LOGGER.entering(squelch);239 if (!squelch) {240 LOGGER.fine("Executing command " + cmdLine.toString());241 }242 watchdog.reset();243 DefaultExecutor executor = new DefaultExecutor();244 executor.setWatchdog(watchdog);245 executor.setStreamHandler(new PumpStreamHandler());246 executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());247 handler = new DefaultExecuteResultHandler();248 executor.execute(cmdLine, handler);249 LOGGER.exiting();250 }251 public void run() {252 try {253 if (!isInitialized()) {254 addJVMShutDownHook();255 FileDownloader.checkForDownloads(getType(), getLauncherOptions()256 .isFileDownloadCheckTimeStampOnInvocation(), getLauncherOptions()257 .isFileDownloadCleanupOnInvocation());258 setInitialized(true);259 }260 if (getCommands().contains("-help") || getCommands().contains("--help") || getCommands().contains("-h")) {261 startProcess(true);262 handler.waitFor();263 if (handler.getExitValue() == 0) {264 printUsageInfo();265 }266 return;267 }268 if (getCommands().contains("-version")) {269 getLauncherOptions().setContinuouslyRestart(false);270 }271 if (getLauncherOptions().isContinuouslyRestart()) {272 long interval = getLauncherOptions().getRestartCycle();273 LOGGER.fine("Restart cycle will check every " + interval + " ms");274 continuouslyRestart(interval);275 }276 // non-continuous process.277 startProcess(false);278 handler.waitFor();279 } catch (InterruptedException | IOException e) {280 // log the exception and exit, if shutdown was not called281 if (!shutdownCalled) {282 LOGGER.log(Level.SEVERE, e.getMessage(), e);283 System.exit(1);284 }285 }286 }287 abstract void printUsageInfo();288 /**289 * Shuts down the instance represented by this launcher. Uses the {@link ProcessHandlerFactory} to find sub290 * processes.291 */292 public void shutdown() {293 shutdownCalled = true;294 if (isRunning()) {295 watchdog.waitForProcessStarted();296 watchdog.destroyProcess();297 watchdog.waitForTerminationAfterDestroy(2, SECONDS);298 if (isRunning()) {299 watchdog.destroyProcessForcefully();300 watchdog.waitForTerminationAfterDestroy(1, SECONDS);301 if (isRunning()) {302 LOGGER.severe(String.format("Unable to kill process with PID %s", watchdog.getProcessId()));303 }304 }305 }306 // if shutdown() was called by something other than the shutdown hook, we don't need the shutdown hook anymore307 try {308 if (shutDownHook != null) {309 Runtime.getRuntime().removeShutdownHook(shutDownHook);310 }311 } catch (IllegalStateException e) {312 // ignore.. happens when the shutdown hook is in use, that's okay313 }314 }315 /**316 * Get the classpath for the child process. Determines all jars from CWD and SELION_HOME_DIR. Does not recurse into317 * sub directories. Filters out duplicates.318 *319 * @param jarNamePrefix320 * when adding jars from the {@link SeLionConstants#SELION_HOME_DIR}, only consider jars whose names321 * start with this prefix. If <code>null</code>, or <code>""</code> consider all jars.322 * @param mainClass323 * the main() Class to invoke324 * @return an array of {@link String} which represents the CLASSPATH to pass325 */326 String[] getJavaClassPathArguments(String jarNamePrefix, String mainClass) {...
shutdown
Using AI Code Generation
1import org.testng.annotations.Test;2import com.paypal.selion.grid.AbstractBaseProcessLauncher;3import com.paypal.selion.grid.ProcessLauncherOptions;4import com.paypal.selion.grid.ProcessLauncherOptions.ProcessLauncherOptionsBuilder;5public class AbstractBaseProcessLauncherTest {6 public void testShutdown() throws Exception {7 ProcessLauncherOptions options = new ProcessLauncherOptionsBuilder().setLogFileName("test.log")8 .setLogFolder("target").setPort(4444).build();9 AbstractBaseProcessLauncher launcher = new AbstractBaseProcessLauncher(options) {10 protected void launch() throws Exception {11 }12 public void shutdown() throws Exception {13 }14 };15 launcher.shutdown();16 }17}18import org.testng.annotations.Test;19import com.paypal.selion.grid.AbstractBaseProcessLauncher;20import com.paypal.selion.grid.ProcessLauncherOptions;21import com.paypal.selion.grid.ProcessLauncherOptions.ProcessLauncherOptionsBuilder;22public class AbstractBaseProcessLauncherTest {23 public void testShutdown() throws Exception {24 ProcessLauncherOptions options = new ProcessLauncherOptionsBuilder().setLogFileName("test.log")25 .setLogFolder("target").setPort(4444).build();26 AbstractBaseProcessLauncher launcher = new AbstractBaseProcessLauncher(options) {27 protected void launch() throws Exception {28 }29 public void shutdown() throws Exception {30 }31 };32 launcher.shutdown();33 }34}35 public void testShutdown() throws Exception {36 ProcessLauncherOptions options = new ProcessLauncherOptionsBuilder().setLogFileName("test.log")37 .setLogFolder("target").setPort(4444).build();38 AbstractBaseProcessLauncher launcher = new AbstractBaseProcessLauncher(options) {39 protected void launch() throws Exception {40 }41 public void shutdown() throws Exception {42 }43 };44 launcher.shutdown();45 }46 public void testShutdown() throws Exception {47 ProcessLauncherOptions options = new ProcessLauncherOptionsBuilder().setLogFileName("test.log")
shutdown
Using AI Code Generation
1import com.paypal.selion.grid.AbstractBaseProcessLauncher;2import com.paypal.selion.grid.ProcessLauncherOptions;3public class ShutdownExample {4 public static void main(String[] args) {5 AbstractBaseProcessLauncher launcher = AbstractBaseProcessLauncher.getProcessLauncher();6 launcher.shutdown(ProcessLauncherOptions.parse(args));7 }8}9import com.paypal.selion.grid.ProcessLauncher;10import com.paypal.selion.grid.ProcessLauncherOptions;11public class ShutdownExample {12 public static void main(String[] args) {13 ProcessLauncher launcher = new ProcessLauncher();14 launcher.shutdown(ProcessLauncherOptions.parse(args));15 }16}17import com.paypal.selion.grid.Launcher;18import com.paypal.selion.grid.ProcessLauncherOptions;19public class ShutdownExample {20 public static void main(String[] args) {21 Launcher launcher = new Launcher();22 launcher.shutdown(ProcessLauncherOptions.parse(args));23 }24}25import com.paypal.selion.grid.Launcher;26import com.paypal.selion.grid.ProcessLauncherOptions;27public class ShutdownExample {28 public static void main(String[] args) {29 Launcher launcher = new Launcher();30 launcher.shutdown(ProcessLauncherOptions.parse(args));31 }32}33import com.paypal.selion.grid.Launcher;34import com.paypal.selion.grid.ProcessLauncherOptions;35public class ShutdownExample {36 public static void main(String[] args) {37 Launcher launcher = new Launcher();38 launcher.shutdown(ProcessLauncherOptions.parse(args));39 }40}
shutdown
Using AI Code Generation
1package com.paypal.selion.grid;2import java.io.IOException;3import java.net.URL;4import java.util.concurrent.TimeUnit;5import org.openqa.grid.common.RegistrationRequest;6import org.openqa.grid.common.exception.GridException;7import org.openqa.grid.internal.Registry;8import org.openqa.grid.internal.RemoteProxy;9import org.openqa.grid.internal.TestSession;10import org.openqa.grid.selenium.proxy.DefaultRemoteProxy;11import org.openqa.grid.web.servlet.handler.SeleniumBasedRequest;12import org.openqa.grid.web.servlet.handler.WebDriverRequest;13import com.paypal.selion.internal.platform.grid.WebDriverPlatform;14public class CustomRemoteProxy extends DefaultRemoteProxy {15 public CustomRemoteProxy(RegistrationRequest request, Registry registry) {16 super(request, registry);17 }18 public TestSession getNewSession(Map<String, Object> requestedCapability) {19 TestSession session = super.getNewSession(requestedCapability);20 if (session == null) {21 return null;22 }23 String nodeHost = session.getSlot().getProxy().getRemoteHost().getHost();24 int nodePort = session.getSlot().getProxy().getRemoteHost().getPort();25 URL remoteURL = null;26 try {27 remoteURL = new URL(nodeURL);28 } catch (MalformedURLException e) {29 e.printStackTrace();30 }31 WebDriverPlatform platform = WebDriverPlatform.extractFromCapabilities(requestedCapability);32 AbstractBaseProcessLauncher launcher = AbstractBaseProcessLauncher.getProcessLauncher(platform);33 launcher.setRemoteURL(remoteURL);34 launcher.shutdown();35 return session;36 }37}38package com.paypal.selion.grid;39import java.io.File;40import java.io.FileReader;41import java.io.IOException;42import java.net.URL;43import java.util.concurrent.TimeUnit;44import org.openqa.grid.common.RegistrationRequest;45import org.openqa.grid.internal.GridRegistry;46import org.openqa.grid.internal.RemoteProxy;47import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;48import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration;49import org.openqa.grid.internal.utils.configuration.StandaloneConfiguration;50import org.openqa.grid.selenium.proxy.DefaultRemoteProxy;51import org.openqa.grid.web.Hub;52import org.openqa.grid.web.servlet.handler.SeleniumBasedRequest;53import org.openqa.grid.web.servlet.handler.WebDriverRequest;54import com.google.gson.Gson;55import com.google.gson.JsonIOException;56import com.google.gson.JsonSyntaxException;57import com.paypal.selion.internal
shutdown
Using AI Code Generation
1package com.paypal.selion.grid;2import java.io.IOException;3import org.openqa.grid.common.exception.GridException;4import org.openqa.selenium.remote.server.SeleniumServer;5public class NodeLauncher extends AbstractBaseProcessLauncher {6 private SeleniumServer server;7 public NodeLauncher(String host, int port) {8 super(host, port);9 }10 public void start() throws GridException {11 try {12 server = new SeleniumServer(isRemote(), getPort());13 server.boot();14 } catch (IOException e) {15 throw new GridException(e);16 }17 }18 public void stop() {19 server.stop();20 }21 public boolean isRunning() {22 return server.isRunning();23 }24 private boolean isRemote() {25 return !getHost().equals("localhost");26 }27 public static void main(String[] args) {28 AbstractBaseProcessLauncher launcher = new NodeLauncher("localhost", 5555);29 launcher.start();30 }31}32public class NodeLauncher extends AbstractBaseProcessLauncher {33 private SeleniumServer server;34 public NodeLauncher(String host, int port) {35 super(host, port);36 }37 public void start() throws GridException {38 try {39 server = new SeleniumServer(isRemote(), getPort());40 server.boot();41 } catch (IOException e) {42 throw new GridException(e);43 }44 }45 public void stop() {46 server.stop();47 }48 public boolean isRunning() {49 return server.isRunning();50 }51 private boolean isRemote() {52 return !getHost().equals("localhost");53 }54 public static void main(String[] args) {55 AbstractBaseProcessLauncher launcher = new NodeLauncher("localhost", 5555);56 launcher.start();57 }58}59public class NodeLauncher extends AbstractBaseProcessLauncher {60 private SeleniumServer server;61 public NodeLauncher(String host, int port) {62 super(host, port);63 }64 public void start() throws GridException {65 try {66 server = new SeleniumServer(isRemote(), getPort());67 server.boot();68 } catch (IOException e) {69 throw new GridException(e);70 }71 }72 public void stop() {73 server.stop();74 }75 public boolean isRunning() {
shutdown
Using AI Code Generation
1import com.paypal.selion.grid.AbstractBaseProcessLauncher;2import com.paypal.selion.grid.ProcessLauncherOptions;3public class ShutdownNode {4 public static void main(String[] args) {5 AbstractBaseProcessLauncher launcher = new AbstractBaseProcessLauncher();6 launcher.shutdownNode("localhost", 5557, 5);7 }8}9{SELION_HOME}/lib/selion-server-grid.jar
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!