/** * Waits the given amount of time. * <p> * If the given value is <tt>null</tt>, the method returns immediately. If an <tt>InterruptedException</tt> * is thrown while waiting, the method will return immediately but ignore the exception. * * @param durationToWait the duration to wait */ public static void duration(@Nullable Duration durationToWait) { if (durationToWait != null) { seconds(durationToWait.getSeconds()); } }
/** * Manually blocks the current thread for n seconds, to make e.g. a bulk write visible in ES. * <p> * Consider using {@link #callAfterUpdate(Runnable)} which does not block system resources. Only use this method * if absolutely necessary. * * @param seconds the number of seconds to block */ public void blockThreadForUpdate(int seconds) { blocks.inc(); Wait.seconds(seconds); }
private void awaitContainerStart(Container container) { LOG.INFO("Waiting for '%s' to become ready...", container.getContainerName()); try { int retries = MAX_WAIT_SECONDS; while (container.areAllPortsOpen().failed()) { Wait.seconds(1); if (retries-- <= 0) { LOG.WARN("Failed to start '%s' - Ports: %s", container.getContainerName(), container.ports().stream().map(Object::toString).collect(Collectors.joining(", "))); return; } } LOG.INFO("Container '%s' is ONLINE - Ports: %s", container.getContainerName(), container.ports().stream().map(Object::toString).collect(Collectors.joining(", "))); } catch (Exception e) { LOG.SEVERE(e); } }
private void waitForElasticsearchToBecomReady() { int retries = 15; while (retries-- > 0) { try { if (client.getRestClient().performRequest("GET", "/_cat/indices").getStatusLine().getStatusCode() == 200) { return; } } catch (Exception e) { Exceptions.ignore(e); } Elastic.LOG.INFO("Sleeping two seconds to wait until Elasticsearch is ready..."); Wait.seconds(2); } Elastic.LOG.WARN("Elasticsearch was not ready after waiting 30s..."); }
private void subscribe(Subscriber subscriber, JedisPubSub subscription) { while (subscriptionsActive.get()) { try (Jedis redis = getConnection()) { LOG.INFO("Starting subscription for: %s", subscriber.getTopic()); redis.subscribe(subscription, subscriber.getTopic()); if (subscriptionsActive.get()) { Wait.seconds(5); } } catch (Exception e) { Exceptions.handle() .to(LOG) .error(e) .withSystemErrorMessage("Failed to subscribe to a topic: %s (%s)") .handle(); Wait.seconds(1); } } LOG.INFO("Terminated subscription for: %s", subscriber.getTopic()); }
/** * When executing several scenarios via Docker, we observed, that especially MySQL isn't entirely ready, * when then port 3306 is open. Therefore we try to establish a real connection (with up to 5 retries * in a one...five second interval). */ private void waitForDatabaseToBecomeReady(String realm, String initSql) { if (Strings.isEmpty(initSql)) { return; } int retries = 5; int waitInSeconds = 1; while (retries-- > 0) { Database database = getDatabase(realm); try (Connection connection = database.getHostConnection()) { executeInitialStatement(database, initSql, connection); return; } catch (SQLException e) { Exceptions.ignore(e); Wait.seconds(waitInSeconds++); } } }