Skip to content

Commit

Permalink
add service registration
Browse files Browse the repository at this point in the history
  • Loading branch information
howardchung committed Jan 14, 2024
1 parent a5cb30c commit 711c239
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/main/java/opendota/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package opendota;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
Expand All @@ -11,6 +13,8 @@
import java.net.URLDecoder;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
Expand All @@ -24,6 +28,11 @@ public static void main(String[] args) throws Exception {
server.createContext("/blob", new BlobHandler());
server.setExecutor(java.util.concurrent.Executors.newCachedThreadPool());
server.start();

// Re-register ourselves
Timer timer = new Timer();
TimerTask task = new RegisterTask();
timer.schedule(task, 0, 10000);
}

static class MyHandler implements HttpHandler {
Expand Down Expand Up @@ -118,3 +127,38 @@ private static long copy(InputStream source, OutputStream sink) throws IOExcepti
return nread;
}
}

class RegisterTask extends TimerTask
{
public void run()
{
if (System.getenv().containsKey("SERVICE_REGISTRY_HOST")) {
try {
String ip = "";
if (System.getenv().containsKey("EXTERNAL")) {
// If configured as external, request external IP and report it
ip = RegisterTask.shellExec("curl api.ipify.org");
} else {
// Otherwise, use hostname -i to get internal IP
ip = RegisterTask.shellExec("hostname -i");
}
int nproc = Runtime.getRuntime().availableProcessors();
RegisterTask.shellExec("curl -X POST http://" + System.getenv().get("SERVICE_REGISTRY_HOST") + "/register/parser/" + ip + "?size=" + nproc);
} catch (Exception e) {
System.err.println(e);
}
}
}

public static String shellExec(String cmdCommand) throws IOException {
final StringBuilder stringBuilder = new StringBuilder();
final Process process = Runtime.getRuntime().exec(cmdCommand);
final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
return stringBuilder.toString();
}
}

0 comments on commit 711c239

Please sign in to comment.