Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#263 OCL loader should throw an exception upon failure #264

Merged
merged 4 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ See the [documentation on Initializer's logging properties](readme/rtprops.md#lo
* Added support for 'queues' domain.
* Added support for 'addresshierarchy' domain.
* Fix for Liquibase Loader to ensure compatibility with OpenMRS versions 2.5.5+
* Fix for OCL Loader to ensure it throws an Exception if the OCL import fails

#### Version 2.6.0
* Added support for 'cohorttypes' and 'cohortattributetypes' domains.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package org.openmrs.module.initializer.api.loaders;

import java.io.File;
import java.util.zip.ZipFile;

import org.apache.commons.lang.StringUtils;
import org.openmrs.annotation.OpenmrsProfile;
import org.openmrs.api.context.Context;
import org.openmrs.module.initializer.Domain;
import org.openmrs.module.openconceptlab.Import;
import org.openmrs.module.openconceptlab.ImportService;
import org.openmrs.module.openconceptlab.importer.Importer;

import java.io.File;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipFile;

@OpenmrsProfile(modules = { "openconceptlab:1.2.9" })
public class OpenConceptLabLoader extends BaseFileLoader {

Expand All @@ -25,7 +29,32 @@ protected String getFileExtension() {
protected void load(File file) throws Exception {
ZipFile zip = new ZipFile(file);
Importer importer = Context.getRegisteredComponent("openconceptlab.importer", Importer.class);
ImportService importService = Context.getService(ImportService.class);
Import lastImport = importService.getLastImport();
log.debug("Starting OCL importer");
importer.run(zip);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will never throw an exception back here, so we need to inspect the ocl import table that OCL uses to report information, including errors, about the executed jobs. I've seen a few different scenarios:

  1. OCL fails to start an import at all, and so there is no ocl import entry written to the DB. This happens if, say, the server stopped during a previous import, and the previous import row was left with a null completion date. This will result in OCL logging stack traces to the console, but no row is written to the import table at all. So if the new import never started (no new row in the DB), we need to throw an exception here.

  2. OCL starts, and reports an error in the import table. If we have a specific error, throw and exception with those details

  3. OCL doesn't complete successfully and no specific error is logged. Not sure when or if this would happen, but it would be an error case.


// This is just a sanity check, this is never expected to be true
while (lastImport == importService.getLastImport()) {
log.warn("Waiting for OCL to start import");
TimeUnit.SECONDS.sleep(1);
}

Import oclImport = importService.getLastImport();

// This is just a sanity check, this is never expected to be true
while (!oclImport.isStopped()) {
log.debug("OCL import: " + importer.getBytesProcessed() + " / " + importer.getTotalBytesToProcess());
TimeUnit.SECONDS.sleep(1);
oclImport = importService.getLastImport();
}

// If the import stopped with errors, then throw an exception
if (StringUtils.isNotBlank(oclImport.getErrorMessage())) {
throw new IllegalStateException(oclImport.getErrorMessage());
}

log.debug("OCL import completed successfully: " + oclImport.getLocalDateStopped());
}

}
Loading