Search in sources :

Example 6 with Timeout

use of com.peterphi.std.threading.Timeout in project stdlib by petergeneric.

the class NginxService method reload.

/**
 * Reload the nginx configuration
 */
public void reload() {
    try {
        final Execed process = Exec.rootUtility(new File(binPath, "nginx-reload").getAbsolutePath());
        process.waitForExit(new Timeout(30, TimeUnit.SECONDS).start(), 0);
    } catch (IOException e) {
        throw new RuntimeException("Error executing nginx-reload command", e);
    }
}
Also used : Timeout(com.peterphi.std.threading.Timeout) Execed(com.peterphi.std.system.exec.Execed) IOException(java.io.IOException) File(java.io.File)

Example 7 with Timeout

use of com.peterphi.std.threading.Timeout in project stdlib by petergeneric.

the class TimeoutConverterTest method test.

@Test
public void test() {
    PropertyFile props = new PropertyFile();
    props.set("timeout1msNoUnit", "1");
    props.set("timeout1ms", "1ms");
    props.set("timeout1s", "1s");
    props.set("timeout1h", "1h");
    props.set("timeout60m", "60m");
    props.set("timeout50h", "50h");
    final Injector injector = Guice.createInjector(new ServicePropertiesModule(props));
    injector.injectMembers(this);
    // extract values
    assertEquals(new Timeout(1, TimeUnit.MILLISECONDS), timeout1msNoUnit);
    assertEquals(new Timeout(1, TimeUnit.MILLISECONDS), timeout1ms);
    assertEquals(new Timeout(1, TimeUnit.SECONDS), timeout1s);
    assertEquals(new Timeout(60, TimeUnit.MINUTES), timeout60m);
    assertEquals(new Timeout(1, TimeUnit.HOURS), timeout1h);
    // compare against one another
    assertEquals(timeout60m, timeout1h);
    assertEquals(timeout1msNoUnit, timeout1ms);
    assertFalse(timeout50h.equals(timeout1h));
}
Also used : ServicePropertiesModule(com.peterphi.std.guice.common.serviceprops.ServicePropertiesModule) Injector(com.google.inject.Injector) Timeout(com.peterphi.std.threading.Timeout) PropertyFile(com.peterphi.std.io.PropertyFile) Test(org.junit.Test)

Example 8 with Timeout

use of com.peterphi.std.threading.Timeout in project stdlib by petergeneric.

the class CarbonClientImpl method createSocket.

private Socket createSocket() {
    int attempt = 0;
    Timeout sleep = reconnectSleep;
    while (attempt++ < MAX_CONNECT_ATTEMPTS) try {
        return new Socket(host, port);
    } catch (Exception e) {
        if (attempt < MAX_CONNECT_ATTEMPTS) {
            // back off
            sleep.sleep();
            // Sleep longer next time
            sleep = sleep.multiply(reconnectSleepMultiplier);
        } else {
            throw new CarbonConnectException("Could not connect to Carbon API in " + MAX_CONNECT_ATTEMPTS + " attempts", e);
        }
    }
    throw new CarbonConnectException("Could not conect to Carbon API socket!");
}
Also used : Timeout(com.peterphi.std.threading.Timeout) CarbonConnectException(com.peterphi.carbon.exception.CarbonConnectException) Socket(java.net.Socket) CarbonException(com.peterphi.carbon.exception.CarbonException) CarbonConnectException(com.peterphi.carbon.exception.CarbonConnectException) JDOMException(org.jdom2.JDOMException) IOException(java.io.IOException)

Aggregations

Timeout (com.peterphi.std.threading.Timeout)8 IOException (java.io.IOException)4 Execed (com.peterphi.std.system.exec.Execed)3 File (java.io.File)3 Injector (com.google.inject.Injector)1 CarbonConnectException (com.peterphi.carbon.exception.CarbonConnectException)1 CarbonException (com.peterphi.carbon.exception.CarbonException)1 Retry (com.peterphi.std.guice.common.retry.annotation.Retry)1 RetryManager (com.peterphi.std.guice.common.retry.retry.RetryManager)1 ExponentialBackoff (com.peterphi.std.guice.common.retry.retry.backoff.ExponentialBackoff)1 ServicePropertiesModule (com.peterphi.std.guice.common.serviceprops.ServicePropertiesModule)1 PropertyFile (com.peterphi.std.io.PropertyFile)1 Socket (java.net.Socket)1 Matcher (java.util.regex.Matcher)1 JDOMException (org.jdom2.JDOMException)1 Test (org.junit.Test)1 Authorization (org.shredzone.acme4j.Authorization)1 Registration (org.shredzone.acme4j.Registration)1 Dns01Challenge (org.shredzone.acme4j.challenge.Dns01Challenge)1 AcmeException (org.shredzone.acme4j.exception.AcmeException)1