Skip to content

Commit

Permalink
DCM: call sr.py as well as ur.py IQSS#3145
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Jun 16, 2016
1 parent dccddeb commit 0ad7dbe
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,21 @@ public HttpResponse<JsonNode> requestRsyncScriptCreation(AuthenticatedUser user,
}
String jsonString = jab.build().toString();
logger.fine("JSON to send to Data Capture Module: " + jsonString);
HttpResponse<JsonNode> jsonResponse = Unirest.post(dcmBaseUrl + "/ur.py")
HttpResponse<JsonNode> uploadRequest = Unirest.post(dcmBaseUrl + "/ur.py")
.body(jsonString)
.asJson();
return jsonResponse;
return uploadRequest;
}

public HttpResponse<JsonNode> retreiveRequestedRsyncScript(AuthenticatedUser user, Dataset dataset) throws Exception {
String dcmBaseUrl = settingsService.getValueForKey(DataCaptureModuleUrl);
if (dcmBaseUrl == null) {
throw new Exception("Problem GETing JSON to Data Capture Module for dataset " + dataset.getId() + " The '" + DataCaptureModuleUrl + "' setting has not been configured.");
}
HttpResponse<JsonNode> scriptRequest = Unirest
.get(dcmBaseUrl + "/sr.py/" + dataset.getId())
.asJson();
return scriptRequest;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ protected void executeImpl(CommandContext ctxt) throws PermissionException, Runt
}
AuthenticatedUser au = (AuthenticatedUser) user;
HttpResponse<JsonNode> response;
/**
* @todo Refactor this building of JSON to make it testable.
*/
JsonObjectBuilder jab = Json.createObjectBuilder();
// The general rule should be to always pass the user id and dataset id to the DCM.
jab.add("userId", au.getId());
Expand All @@ -65,6 +68,10 @@ protected void executeImpl(CommandContext ctxt) throws PermissionException, Runt
throw new RuntimeException(errorPreamble + ex.getLocalizedMessage(), ex);
}
int statusCode = response.getStatus();
/**
* @todo Since we're creating something, maybe a 201 response would be
* more appropriate.
*/
if (statusCode != 200) {
/**
* @todo is the body too big to fit in the actionlogrecord? The
Expand All @@ -73,12 +80,29 @@ protected void executeImpl(CommandContext ctxt) throws PermissionException, Runt
*/
throw new RuntimeException(errorPreamble + "Rather than 200 the status code was " + statusCode + ". The body was \'" + response.getBody() + "\'.");
}
String message = response.getBody().getObject().getString("status");
logger.info("Message from Data Caputure Module upload request endpoint: " + message);
/**
* @todo Don't expect to get the script from ur.py (upload request). Go
* fetch it from sr.py (script request) after a minute or so. (Cron runs
* every minute.) Wait 90 seconds to be safe. Note that it's possible
* for a different user id to call ur.py vs. sr.py
* every minute.) Wait 90 seconds to be safe.
*/
long millisecondsToSleep = 0;
try {
Thread.sleep(millisecondsToSleep);
} catch (InterruptedException ex) {
throw new RuntimeException(errorPreamble + "Unable to wait " + millisecondsToSleep + " milliseconds: " + ex.getLocalizedMessage());
}
try {
response = ctxt.dataCaptureModule().retreiveRequestedRsyncScript(au, dataset);
} catch (Exception ex) {
throw new RuntimeException(errorPreamble + "Problem retrieving rsync script: " + ex.getLocalizedMessage());
}
statusCode = response.getStatus();
if (statusCode != 200) {
throw new RuntimeException(errorPreamble + "Rather than 200 the status code was " + statusCode + ". The body was \'" + response.getBody() + "\'.");
}
long datasetId = response.getBody().getObject().getLong("datasetId");
String script = response.getBody().getObject().getString("script");
if (script == null || script.isEmpty()) {
throw new RuntimeException(errorPreamble + "The script was null or empty.");
Expand All @@ -87,8 +111,7 @@ protected void executeImpl(CommandContext ctxt) throws PermissionException, Runt
* @todo Put this in the database somewhere. Will I be able to query the
* DCM at any time and GET the script again, based on an id?
*/
logger.info("script: " + script);

logger.info("script for dataset " + datasetId + ": " + script);
}

}

0 comments on commit 0ad7dbe

Please sign in to comment.