use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.
the class PromiseSampleService method getDataSimple1.
public IPromise<String> getDataSimple1() {
Promise result = new Promise();
result.resolve("Data");
return result;
}
use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.
the class PromiseSampleService method getDataAsync.
// //////////////////////////////////////////////////
public IPromise<String> getDataAsync() {
Promise p = new Promise();
// simulate async long running op
delayed(3000, () -> p.resolve("Data"));
// returns before result is present
return p;
}
use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.
the class RESTActor method getBooks.
// curl -i -X GET http://localhost:8080/api/books/234234
public IPromise getBooks(int id) {
Promise res = promise();
// simulate blocking operation (e.g. database query)
execInThreadPool(() -> new Book().title("Title " + id).id("" + id).author("kontraktor")).then(res);
return res;
}
use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.
the class AsyncHttpActor method post.
public IPromise<HttpResponse> post(String url, String postData, String... headers) {
Promise res = new Promise();
if (url == null) {
int debug = 1;
}
try {
HttpPost req = new HttpPost(url);
setHeaders(req, headers);
beChrome(req);
req.setEntity(new StringEntity(postData));
getClient().execute(req, new FutureCallback<HttpResponse>() {
@Override
public void completed(HttpResponse result) {
// switch to actor thread
execute(() -> res.resolve(result));
}
@Override
public void failed(Exception ex) {
// switch to actor thread
execute(() -> res.reject(ex));
}
@Override
public void cancelled() {
// switch to actor thread
execute(() -> res.reject("cancelled"));
}
});
} catch (Throwable th) {
Log.Warn(this, "get fail " + th + " " + url);
res.reject(th);
}
return res;
}
use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.
the class JNPM method npmInstall.
public IPromise<InstallResult> npmInstall(String module, String versionSpec, File importingModuleDir) {
if (versionSpec == null) {
versionSpec = "latest";
}
File nodeModule = new File(nodeModulesDir, module);
boolean installPrivate = false;
if (nodeModule.exists()) {
File targetDir = importingModuleDir.getName().equals("node_modules") ? nodeModulesDir : new File(importingModuleDir, "node_modules");
String moduleKey = createModuleKey(module, targetDir);
List<Promise> promises = packagesUnderway.get(moduleKey);
if (promises != null) {
int debug = 1;
}
String finalVersionSpec1 = versionSpec;
if (// timing: not unpacked
promises != null && promises.size() > 0) {
Log.Warn(this, "Delaying because in transfer:" + module + " " + targetDir.getAbsolutePath());
Promise p = new Promise();
delayed(1000, () -> npmInstall(module, finalVersionSpec1, importingModuleDir).then(p));
return p;
}
File pack = new File(nodeModule, "package.json");
if (pack.exists() && versionSpec.indexOf(".") > 0) {
String version = null;
String vspec = null;
try {
JsonObject pjson = Json.parse(new FileReader(pack)).asObject();
version = pjson.getString("version", null);
vspec = versionSpec;
if (!matches(version, vspec)) {
Log.Warn(this, "version mismatch for module '" + module + "'. requested:" + versionSpec + " from '" + importingModuleDir.getName() + "' installed:" + version + ". (delete module dir for update)");
if (config.getVersion(module) == null) {
installPrivate = true;
Log.Warn(this, " installing private " + module);
} else {
Log.Warn(this, " stick with mismatch because of jnpm.kson config entry for " + module);
}
}
} catch (Exception e) {
Log.Error(this, "can't parse package.json in " + module + " " + importingModuleDir.getAbsolutePath() + ". Retry");
Promise p = new Promise();
delayed(1000, () -> npmInstall(module, finalVersionSpec1, importingModuleDir).then(p));
return p;
}
} else
return resolve(InstallResult.EXISTS);
}
Promise p = new Promise();
// fire in parallel
IPromise<List<String>> versionProm = getVersions(module);
IPromise<JsonObject> distributionsProm = getDistributions(module);
String finalVersionSpec = versionSpec == null ? "latest" : versionSpec;
boolean finalInstallPrivate = installPrivate;
distributionsProm.then((dist, derr) -> {
if (dist == null) {
dist = new JsonObject();
Log.Error(this, "distribution is empty or error " + derr + " in module:" + module);
}
JsonObject finalDist = dist;
versionProm.then((versions, verr) -> {
if (versions == null) {
p.reject(verr);
return;
}
String resolvedVersion = getVersion(module, finalVersionSpec, versions, finalDist);
http.getContent(config.getRepo() + "/" + module + "/" + resolvedVersion).then((cont, err) -> {
if (cont != null) {
JsonObject pkg = Json.parse(cont).asObject();
String tarUrl = pkg.get("dist").asObject().get("tarball").asString();
JsonValue dependencies = pkg.get("dependencies");
if (dependencies == null)
dependencies = new JsonObject();
else
dependencies = dependencies.asObject();
JsonValue peerdependencies = pkg.get("peerDependencies");
if (peerdependencies != null) {
((JsonObject) dependencies).merge(peerdependencies.asObject());
}
File targetDir = finalInstallPrivate ? new File(importingModuleDir, "node_modules") : nodeModulesDir;
if (finalInstallPrivate)
targetDir.mkdirs();
IPromise depP = downLoadAndInstall(tarUrl, module, resolvedVersion, targetDir);
List deps = new ArrayList<>();
deps.add(depP);
File importingDir = new File(nodeModulesDir, module);
((JsonObject) dependencies).forEach(member -> {
deps.add(npmInstall(member.getName(), member.getValue().asString(), importingDir));
});
all(deps).then((r, e) -> {
p.resolve(InstallResult.INSTALLED);
});
} else {
p.reject("no such module");
}
});
});
});
return p;
}
Aggregations