public void enableStuckJobs(LightblueClient cli, Date enableBefore) {
DataFindRequest findRequest = new DataFindRequest("migrationJob", null);
findRequest.where(Query.and(Query.withValue("status", Query.eq, MigrationJob.STATE_ACTIVE),
Query.arrayMatch("jobExecutions",
Query.and(Query.withValue("status", Query.eq, MigrationJob.STATE_ACTIVE),
Query.withValue("actualStartDate", Query.lt, new Literal(enableBefore))))));
findRequest.select(Projection.includeField("_id"));
findRequest.range(0, 250);
LOGGER.debug("Re-enabling stuck jobs");
for (int loop = 0; loop < 10; loop++) {
try {
MigrationJob[] jobs = cli.data(findRequest, MigrationJob[].class);
if (jobs != null && jobs.length > 0) {
LOGGER.warn("Re-enabling {} active stuck jobs", jobs.length);
DataUpdateRequest upd = new DataUpdateRequest("migrationJob", null);
upd.where(Query.withValues("_id", Query.in, ids(jobs)));
upd.updates(Update.set("status", MigrationJob.STATE_AVAILABLE));
LOGGER.debug("update:{}", upd.getBodyJson());
cli.data(upd);
} else {
break;
}
} catch (Exception e) {
LOGGER.error("Error re-activating jobs", e);
}
}
}