Created
October 13, 2011 09:41
-
-
Save hwartig/1283858 to your computer and use it in GitHub Desktop.
Collecting stats of a remote play server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package jobs; | |
| import com.google.gson.JsonObject; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.concurrent.TimeUnit; | |
| import play.Logger; | |
| import play.Play; | |
| import play.jobs.Every; | |
| import play.jobs.Job; | |
| import play.libs.Crypto; | |
| import play.libs.F.Promise; | |
| import play.libs.WS; | |
| import play.libs.WS.HttpResponse; | |
| /** | |
| * Collects stats of remote play servers. | |
| * @author Harald Wartig | |
| */ | |
| @Every("1mn") | |
| public class StatsCollector extends Job { | |
| @Override | |
| public void doJob() throws Exception { | |
| super.doJob(); | |
| String remoteSecretKey = Play.configuration.getProperty("remote.secret"); | |
| List<String> remoteAdresses = new ArrayList<String>(); | |
| if (Play.configuration.containsKey("remote.host")) { | |
| remoteAdresses.add(Play.configuration.getProperty("remote.host")); | |
| } else if (Play.configuration.containsKey("remote.1.host")) { | |
| int nb = 1; | |
| while (Play.configuration.containsKey("remote." + nb + ".host")) { | |
| remoteAdresses.add(Play.configuration.getProperty("remote." + nb + ".host")); | |
| } | |
| } | |
| String authorization = Crypto.sign("@status", remoteSecretKey.getBytes()); | |
| List<Promise<HttpResponse>> responses = new ArrayList<Promise<HttpResponse>>(); | |
| for(String adress : remoteAdresses) { | |
| responses.add(WS.url("http://" + adress + "/@status.json").setHeader("Authorization", authorization).getAsync()); | |
| } | |
| for(Promise<HttpResponse> promise : responses) { | |
| HttpResponse response = promise.get(5, TimeUnit.SECONDS); | |
| if (response.getStatus() == 200) { | |
| JsonObject stats = response.getJson().getAsJsonObject(); | |
| Logger.info(stats.toString()); | |
| } else { | |
| Logger.warn("could not read status update from remote got status code %d", response.getStatus()); | |
| } | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment