use of com.fathomdb.TimeSpan in project platformlayer by platformlayer.
the class ZookeeperUtils method sendCommand.
public static ZookeeperResponse sendCommand(InetSocketAddress socketAddress, String command) throws IOException {
TimeSpan connectionTimeout = TimeSpan.TEN_SECONDS;
Socket s = new Socket();
s.setTcpNoDelay(true);
s.setSoTimeout((int) connectionTimeout.getTotalMilliseconds());
s.connect(socketAddress);
s.getOutputStream().write(command.getBytes());
s.getOutputStream().flush();
// TODO: Timeout?
String response = IoUtils.readAll(s.getInputStream());
return new ZookeeperResponse(response);
}
use of com.fathomdb.TimeSpan in project platformlayer by platformlayer.
the class SshOpsTarget method executeCommandUnchecked.
@Override
protected ProcessExecution executeCommandUnchecked(Command command) throws ProcessExecutionException {
try {
String commandString = command.buildCommandString();
TimeSpan timeout = command.getTimeout();
if (command.getKeyPair() != null) {
SshConnection agentConnection = sshConnection.buildAgentConnection(command.getKeyPair());
try {
return agentConnection.sshExecute(commandString, timeout);
} finally {
agentConnection.close();
}
} else {
return sshConnection.sshExecute(commandString, timeout);
}
} catch (IOException e) {
throw new ProcessExecutionException("Error during command execution", e);
} catch (InterruptedException e) {
ExceptionUtils.handleInterrupted(e);
throw new ProcessExecutionException("Error during command execution", e);
} catch (SshException e) {
throw new ProcessExecutionException("Error during command execution", e);
}
}
use of com.fathomdb.TimeSpan in project platformlayer by platformlayer.
the class HttpProxyHelper method chooseProxy.
private String chooseProxy(OpsTarget target, List<String> proxies) {
String bestProxy = null;
TimeSpan bestTime = null;
for (String proxy : proxies) {
// {
// // We choose the fastest proxy that gives us a 200 response
// String url = proxy + "acng-report.html";
// CurlRequest request = new CurlRequest(url);
// request.setTimeout(5);
// try {
// CurlResult result = request.executeRequest(target);
// if (result.getHttpResult() != 200) {
// log.info("Unexpected response code while testing proxy: " + proxy + ". Code=" + result.getHttpResult());
// continue;
// }
// TimeSpan timeTotal = result.getTimeTotal();
// if (bestTime == null || timeTotal.isLessThan(bestTime)) {
// bestProxy = proxy;
// bestTime = timeTotal;
// }
// } catch (ProcessExecutionException e) {
// log.info("Error while testing proxy: " + proxy, e);
// }
// }
{
// We choose the fastest proxy that gives us a 200 response
String url = "http://ftp.debian.org/debian/dists/stable/Release.gpg";
CurlRequest request = new CurlRequest(url);
request.proxy = proxy;
request.timeout = TimeSpan.FIVE_SECONDS;
try {
CurlResult result = request.executeRequest(target);
if (result.getHttpResult() != 200) {
log.info("Unexpected response code while testing proxy: " + proxy + ". Code=" + result.getHttpResult());
continue;
}
TimeSpan timeTotal = result.getTimeTotal();
if (bestTime == null || timeTotal.isLessThan(bestTime)) {
bestProxy = proxy;
bestTime = timeTotal;
}
} catch (OpsException e) {
log.info("Error while testing proxy: " + proxy, e);
}
}
}
return bestProxy;
}
use of com.fathomdb.TimeSpan in project platformlayer by platformlayer.
the class SchedulerImpl method parseSchedule.
private JobScheduleCalculator parseSchedule(JobSchedule schedule, boolean tolerant) {
TimeSpan interval = null;
Date base = null;
if (schedule == null) {
if (tolerant) {
log.warn("Expected schedule; was null");
} else {
throw new IllegalArgumentException("Schedule is required");
}
} else {
if (!Strings.isNullOrEmpty(schedule.interval)) {
try {
interval = TimeSpan.parse(schedule.interval);
} catch (IllegalArgumentException e) {
if (tolerant) {
log.warn("Ignoring error parsing interval: " + schedule.interval, e);
} else {
throw new IllegalArgumentException("Invalid interval: " + schedule.interval, e);
}
}
}
if (schedule.base != null) {
base = schedule.base;
}
}
if (interval == null) {
if (tolerant) {
log.warn("Interval not provided; assuming default");
interval = TimeSpan.ONE_HOUR;
} else {
throw new IllegalArgumentException("Interval is required");
}
}
JobScheduleCalculator scheduleCalculator = new SimpleJobScheduleCalculator(interval, base);
return scheduleCalculator;
}
use of com.fathomdb.TimeSpan in project platformlayer by platformlayer.
the class MinaSshConnection method sshCopyData0.
@Override
protected void sshCopyData0(InputStream fileData, long dataLength, String remoteFile, String mode, boolean sudo) throws IOException, InterruptedException, SshException {
int lastSlash = remoteFile.lastIndexOf('/');
if (lastSlash == -1) {
throw new IllegalArgumentException("Expected dest file to be absolute path: " + remoteFile);
}
MinaSshConnectionWrapper sshConnection = ensureConnected();
MinaScpClient scp = new MinaScpClient(sshConnection);
String remoteDir = remoteFile.substring(0, lastSlash);
String filename = remoteFile.substring(lastSlash + 1);
try {
TimeSpan timeout = TimeSpan.FIVE_MINUTES;
scp.put(fileData, dataLength, filename, remoteDir, mode, timeout, sudo);
} catch (IOException ioException) {
throw new SshException("Cannot doing scp on file", ioException);
} catch (RuntimeSshException e) {
throw new SshException("Error doing scp on file", e);
}
}
Aggregations