/** * Can't be started as is. Needs to be bootstrapped by sonar-application */ public static void main(String[] args) { ProcessEntryPoint entryPoint = ProcessEntryPoint.createForArguments(args); Props props = entryPoint.getProps(); new WebServerProcessLogging().configure(props); WebServer server = new WebServer(props); entryPoint.launch(server); } }
@Test public void test_initial_state() throws Exception { Props props = createProps(); ProcessEntryPoint entryPoint = new ProcessEntryPoint(props, exit, commands); assertThat(entryPoint.getProps()).isSameAs(props); assertThat(entryPoint.isStarted()).isFalse(); assertThat(entryPoint.getState()).isEqualTo(State.INIT); }
/** * Launch process and waits until it's down */ public void launch(Monitored mp) { if (!lifecycle.tryToMoveTo(Lifecycle.State.STARTING)) { throw new IllegalStateException("Already started"); } monitored = mp; Logger logger = LoggerFactory.getLogger(getClass()); try { launch(logger); } catch (Exception e) { logger.warn("Fail to start {}", getKey(), e); } finally { stop(); } }
public static ProcessEntryPoint createForArguments(String[] args) { Props props = ConfigurationUtils.loadPropsFromCommandLineArgs(args); File sharedDir = getSharedDir(props); int processNumber = getProcessNumber(props); ProcessCommands commands = DefaultProcessCommands.main(sharedDir, processNumber); return new ProcessEntryPoint(props, processNumber, sharedDir, new SystemExit(), commands); }
public static void main(String[] args) { ProcessEntryPoint entryPoint = ProcessEntryPoint.createForArguments(args); entryPoint.launch(new HttpProcess(entryPoint.getProps().valueAsInt("httpPort"), entryPoint.getCommands())); } }
public static void main(String[] args) { ProcessEntryPoint entryPoint = ProcessEntryPoint.createForArguments(args); entryPoint.launch(new InfiniteTerminationProcess()); } }
@Test public void load_properties_from_file() throws Exception { File propsFile = temp.newFile(); FileUtils.write(propsFile, "sonar.foo=bar\nprocess.key=web\nprocess.index=1\nprocess.sharedDir=" + temp.newFolder().getAbsolutePath().replaceAll("\\\\", "/")); ProcessEntryPoint entryPoint = ProcessEntryPoint.createForArguments(new String[] {propsFile.getAbsolutePath()}); assertThat(entryPoint.getProps().value("sonar.foo")).isEqualTo("bar"); assertThat(entryPoint.getProps().value("process.key")).isEqualTo("web"); }
@Test public void terminate_if_startup_error() throws IOException { Props props = createProps(); final ProcessEntryPoint entryPoint = new ProcessEntryPoint(props, exit, commands); final Monitored process = new StartupErrorProcess(); entryPoint.launch(process); assertThat(entryPoint.getState()).isEqualTo(State.STOPPED); }
@Test public void fail_to_launch_multiple_times() throws IOException { Props props = createProps(); ProcessEntryPoint entryPoint = new ProcessEntryPoint(props, exit, commands); entryPoint.launch(new NoopProcess()); try { entryPoint.launch(new NoopProcess()); fail(); } catch (IllegalStateException e) { assertThat(e).hasMessage("Already started"); } }
@Override public void run() { // starts and waits until terminated entryPoint.launch(process); } };
private void launch(Logger logger) throws InterruptedException { logger.info("Starting {}", getKey()); Runtime.getRuntime().addShutdownHook(shutdownHook); stopWatcher.start(); monitored.start(); Monitored.Status status = waitForNotDownStatus(); if (status == Monitored.Status.UP || status == Monitored.Status.OPERATIONAL) { // notify monitor that process is ready commands.setUp(); if (lifecycle.tryToMoveTo(Lifecycle.State.STARTED)) { Monitored.Status newStatus = waitForOperational(status); if (newStatus == Monitored.Status.OPERATIONAL && lifecycle.tryToMoveTo(Lifecycle.State.OPERATIONAL)) { commands.setOperational(); } monitored.awaitStop(); } } else { stop(); } }
@Test public void launch_then_request_graceful_stop() throws Exception { Props props = createProps(); final ProcessEntryPoint entryPoint = new ProcessEntryPoint(props, exit, commands); final StandardProcess process = new StandardProcess(); Thread runner = new Thread() { @Override public void run() { // starts and waits until terminated entryPoint.launch(process); } }; runner.start(); while (process.getState() != State.STARTED) { Thread.sleep(10L); } // requests for graceful stop -> waits until down // Should terminate before the timeout of 30s entryPoint.stop(); assertThat(process.getState()).isEqualTo(State.STOPPED); }
/** * Launch process and waits until it's down */ public void launch(Monitored mp) { if (!lifecycle.tryToMoveTo(Lifecycle.State.STARTING)) { throw new IllegalStateException("Already started"); } monitored = mp; try { LoggerFactory.getLogger(getClass()).info("Starting " + getKey()); Runtime.getRuntime().addShutdownHook(shutdownHook); stopWatcher.start(); monitored.start(); boolean ready = false; while (!ready) { ready = monitored.isReady(); Thread.sleep(200L); } // notify monitor that process is ready commands.setReady(); if (lifecycle.tryToMoveTo(Lifecycle.State.STARTED)) { monitored.awaitStop(); } } catch (Exception e) { LoggerFactory.getLogger(getClass()).warn("Fail to start " + getKey(), e); } finally { stop(); } }
@Test public void terminate_if_unexpected_shutdown() throws Exception { Props props = createProps(); final ProcessEntryPoint entryPoint = new ProcessEntryPoint(props, exit, commands); final StandardProcess process = new StandardProcess(); Thread runner = new Thread() { @Override public void run() { // starts and waits until terminated entryPoint.launch(process); } }; runner.start(); while (process.getState() != State.STARTED) { Thread.sleep(10L); } // emulate signal to shutdown process entryPoint.getShutdownHook().start(); // hack to prevent JUnit JVM to fail when executing the shutdown hook a second time Runtime.getRuntime().removeShutdownHook(entryPoint.getShutdownHook()); while (process.getState() != State.STOPPED) { Thread.sleep(10L); } // exit before test timeout, ok ! }
@Override public void run() { exit.setInShutdownHook(); stop(); } });
public static ProcessEntryPoint createForArguments(String[] args) { Props props = ConfigurationUtils.loadPropsFromCommandLineArgs(args); ProcessCommands commands = new ProcessCommands( props.nonNullValueAsFile(PROPERTY_SHARED_PATH), Integer.parseInt(props.nonNullValue(PROPERTY_PROCESS_INDEX))); return new ProcessEntryPoint(props, new SystemExit(), commands); } }
/** * Blocks until stopped in a timely fashion (see {@link org.sonar.process.StopperThread}) */ void stop() { stopAsync(); try { // stopperThread is not null for sure // join() does nothing if thread already finished stopperThread.join(); lifecycle.tryToMoveTo(Lifecycle.State.STOPPED); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } exit.exit(0); }
public static void main(String[] args) { ProcessEntryPoint entryPoint = ProcessEntryPoint.createForArguments(args); entryPoint.launch(new StandardProcess()); System.exit(0); } }
@Override public void run() { // starts and waits until terminated entryPoint.launch(process); } };
@Override public void run() { exit.setInShutdownHook(); stop(); } });