use of org.folio.rest.resource.interfaces.JobAPI in project raml-module-builder by folio-org.
the class JobsRunner method init.
public void init(Vertx vertx, Context context, Handler<AsyncResult<Boolean>> resultHandler) {
/**
* load all implementations in the class path of JobAPI
*/
this.vertx = vertx;
// get all jobAPI implementations in the class path
ArrayList<Class<?>> impls = new ArrayList<>();
try {
impls = InterfaceToImpl.convert2Impl(RTFConsts.PACKAGE_OF_IMPLEMENTATIONS, RTFConsts.PACKAGE_OF_HOOK_INTERFACES + ".JobAPI", true);
} catch (Exception e) {
// no impl found
log.error(e);
}
// loop on jobs implementations - create new instance and call init with vertx param
for (int i = 0; i < impls.size(); i++) {
JobAPI job = null;
String[] jobId = new String[] { null };
try {
job = (JobAPI) impls.get(i).newInstance();
job.init(vertx);
} catch (Exception e) {
log.error(e);
}
if (job != null) {
String[] jobNames = job.getName();
// with one implementation - which can internally handle differently based on the name
for (int j = 0; j < jobNames.length; j++) {
jobId[0] = job.getModule() + "_" + jobNames[j];
// cache the jobAPI implementations
jobCache.put(jobId[0], job);
log.info("Loaded Job implementation module: " + job.getModule() + " job name: " + jobNames[j]);
}
}
}
// set periodic to query db for pending states and run them if there is an open slot
try {
vertx.setPeriodic(60000, todo -> {
try {
// kick off the processing
process();
} catch (Exception e) {
log.error(e);
resultHandler.handle(io.vertx.core.Future.failedFuture(e.getMessage()));
}
});
} catch (NullPointerException e) {
// no mongo instance config found - so no processing
log.error(e.getMessage(), e);
}
// return
resultHandler.handle(io.vertx.core.Future.succeededFuture(true));
}