Skip to content

Commit dbe994b

Browse files
authored
Feature/recreate 2022 (#345)
* seqware-2023 Validate metadata-parent-accession-file * Fix up tests for invalid accession files * Fix runner metadata failures
1 parent 5be1d26 commit dbe994b

File tree

3 files changed

+143
-55
lines changed

3 files changed

+143
-55
lines changed

seqware-pipeline/src/main/java/net/sourceforge/seqware/pipeline/runner/Runner.java

+36-55
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import net.sourceforge.seqware.common.metadata.Metadata;
1010
import net.sourceforge.seqware.common.metadata.MetadataDB;
1111
import net.sourceforge.seqware.common.metadata.MetadataFactory;
12+
import net.sourceforge.seqware.common.model.ParentAccessionModel;
1213
import net.sourceforge.seqware.common.model.Processing;
1314
import net.sourceforge.seqware.common.module.FileMetadata;
1415
import net.sourceforge.seqware.common.module.ReturnValue;
@@ -45,21 +46,21 @@
4546
import java.util.zip.ZipFile;
4647

4748
// FIXME: auto-adding to rc.data, support "," delimited
48-
// FIXME: When adding STDOUT/STDERR to metadb, we should add a timestamp or something else to make it easier to merge. Right now, it is hard to tell which stdout message corresponds to which step in stderr
49+
// FIXME: When adding STDOUT/STDERR to metadb, we should add a timestamp or something else to make it easier to merge. Right now, it is hard to tell which stdout message corresponds to which step in stderr
4950
// FIXME: is parent accession comma separated?
5051

5152
/*
5253
* Run each method for the requested module object. Based on return value, either continue or exit:
5354
* Return values > 0 are errors that will cause the runner to exit.
5455
* Return of 0 implies success and the runner will continue, assuming all is well.
55-
* Return of -1 implies the method was not implemented for that Module, AND IS NOT AN ERROR!!! By default the runner will continue on with steps as if it succeeded!
56+
* Return of -1 implies the method was not implemented for that Module, AND IS NOT AN ERROR!!! By default the runner will continue on with steps as if it succeeded!
5657
*/
5758
//Create a main() function here, which will be compiled in, to parse input and run all steps
5859
/**
5960
* <p>
6061
* Runner class.
6162
* </p>
62-
*
63+
*
6364
* @author boconnor
6465
* @version $Id: $Id
6566
*/
@@ -187,7 +188,7 @@ public Runner() {
187188
* <p>
188189
* getSyntax.
189190
* </p>
190-
*
191+
*
191192
* @param parser
192193
* a {@link joptsimple.OptionParser} object.
193194
* @param errorMessage
@@ -213,19 +214,20 @@ public static void getSyntax(OptionParser parser, String errorMessage) {
213214
System.exit(-1);
214215
}
215216

216-
private void writeProcessingAccessionToFile(File file, boolean append) {
217+
private void writeStringToFile(File file, boolean append, String output) {
217218
int maxTries = (Integer) options.valueOf("metadata-tries-number");
218-
for (int i = 0; i < maxTries; i++) {
219+
for (int i = 0; i <= maxTries; i++) {
220+
Log.fatal("On try " + i + " of " + maxTries);
219221
// Break on success
220-
if (LockingFileTools.lockAndWrite(file, processingAccession + System.getProperty("line.separator"), append)) {
222+
if (LockingFileTools.lockAndWrite(file, output, append)) {
221223
break;
222224
} // Sleep if going to try again
223225
else if (i < maxTries) {
224226
ProcessTools.sleep((Integer) options.valueOf("metadata-tries-delay"));
225227
} // Return error if failed on last try
226-
else {
228+
if (maxTries == i) {
227229
ReturnValue retval = new ReturnValue();
228-
retval.printAndAppendtoStderr("Could not write to processingAccession File for metadata");
230+
retval.printAndAppendtoStderr("Could not write to " + file.getAbsolutePath() + " for metadata");
229231
retval.setExitStatus(ReturnValue.METADATAINVALIDIDCHAIN);
230232
meta.update_processing_event(processingID, retval);
231233
meta.update_processing_status(processingID, ProcessingStatus.failed);
@@ -238,7 +240,7 @@ else if (i < maxTries) {
238240
* <p>
239241
* printAndAppendtoStderr.
240242
* </p>
241-
*
243+
*
242244
* @param buffer
243245
* a {@link java.lang.String} object.
244246
*/
@@ -251,7 +253,7 @@ public void printAndAppendtoStderr(String buffer) {
251253
* <p>
252254
* printAndAppendtoStdout.
253255
* </p>
254-
*
256+
*
255257
* @param buffer
256258
* a {@link java.lang.String} object.
257259
*/
@@ -268,7 +270,7 @@ public void printAndAppendtoStdout(String buffer) {
268270
* <p>
269271
* evaluateReturn.
270272
* </p>
271-
*
273+
*
272274
* @param app
273275
* a {@link net.sourceforge.seqware.pipeline.module.Module} object.
274276
* @param methodName
@@ -372,7 +374,7 @@ public void evaluateReturn(Module app, String methodName) {
372374

373375
/**
374376
* FIXME: this needs to be migrated to something that is ZIP64 aware. Try using the unzip feature of FileTools.java
375-
*
377+
*
376378
* @param zipFile
377379
* a {@link java.lang.String} object.
378380
* @throws java.util.zip.ZipException
@@ -440,7 +442,7 @@ public static void unzipPkg(String zipFile) throws ZipException, IOException {
440442
* <p>
441443
* main.
442444
* </p>
443-
*
445+
*
444446
* @param args
445447
* an array of {@link java.lang.String} objects.
446448
*/
@@ -644,24 +646,7 @@ private void preProcessMetadata() {
644646

645647
try {
646648
if ((file.exists() || file.createNewFile()) && file.canWrite()) {
647-
int maxTries = (Integer) options.valueOf("metadata-tries-number");
648-
for (int i = 0; i < maxTries; i++) {
649-
// Break on success
650-
if (LockingFileTools.lockAndAppend(file, workflowRunAccession + System.getProperty("line.separator"))) {
651-
break;
652-
} // Sleep if going to try again
653-
else if (i < maxTries) {
654-
ProcessTools.sleep((Integer) options.valueOf("metadata-tries-delay"));
655-
} // Return error if failed on last try
656-
else {
657-
ReturnValue retval = new ReturnValue();
658-
retval.printAndAppendtoStderr("Could not write to processingID File for metadata");
659-
retval.setExitStatus(ReturnValue.METADATAINVALIDIDCHAIN);
660-
meta.update_processing_event(workflowRunAccession, retval);
661-
meta.update_processing_status(workflowRunAccession, ProcessingStatus.failed);
662-
System.exit(retval.getExitStatus());
663-
}
664-
}
649+
this.writeStringToFile(file, true, workflowRunAccession + System.getProperty("line.separator"));
665650
} else {
666651
Log.error("Could not create processingAccession File for metadata");
667652
System.exit(ReturnValue.METADATAINVALIDIDCHAIN);
@@ -721,16 +706,29 @@ else if (i < maxTries) {
721706
BufferedReader r;
722707
String line;
723708
r = new BufferedReader(new FileReader(file));
724-
709+
boolean accessionFound = false;
710+
// seqware-2023, we need to ensure that there is at least one accession found
725711
while ((line = r.readLine()) != null) {
726712
try {
727-
parentAccessions.add(Integer.parseInt(line));
713+
int parseInt = Integer.parseInt(line);
714+
parentAccessions.add(parseInt);
715+
int[] accessions = new int[] { parseInt };
716+
List<ParentAccessionModel> viaParentAccessions = meta.getViaParentAccessions(accessions);
717+
if (viaParentAccessions == null) {
718+
Log.error("Invalid accession found when parsing parent accession file '" + line + "'");
719+
System.exit(ReturnValue.METADATAINVALIDIDCHAIN);
720+
}
721+
accessionFound = true;
728722
} catch (NumberFormatException ex) {
729723
Log.error("Non number found when parsing parent accession file '" + line + "'");
730724
System.exit(ReturnValue.METADATAINVALIDIDCHAIN);
731725
}
732726
}
733727
r.close();
728+
if (!accessionFound) {
729+
Log.error("No number found when parsing parent accession file '" + file + "'");
730+
System.exit(ReturnValue.METADATAINVALIDIDCHAIN);
731+
}
734732
} catch (Exception e) {
735733
Log.error("Could not open parent accession file for metadata-parent-accession-file: " + e.getMessage());
736734
System.exit(ReturnValue.METADATAINVALIDIDCHAIN);
@@ -882,35 +880,18 @@ private void postProcessMetadata() {
882880
// Try to write to each processingIDFile until success or timeout
883881
for (File file : processingIDFiles) {
884882
Log.debug("Writing out accession to " + file.toString());
885-
int maxTries = (Integer) options.valueOf("metadata-tries-number");
886-
for (int i = 0; i < maxTries; i++) {
887-
// Break on success
888-
if (LockingFileTools.lockAndAppend(file, processingID + System.getProperty("line.separator"))) {
889-
break;
890-
} // Sleep if going to try again
891-
else if (i < maxTries) {
892-
ProcessTools.sleep((Integer) options.valueOf("metadata-tries-delay"));
893-
} // Return error if failed on last try
894-
else {
895-
ReturnValue retval = new ReturnValue();
896-
retval.printAndAppendtoStderr("Could not write to processingID File for metadata");
897-
retval.setExitStatus(ReturnValue.METADATAINVALIDIDCHAIN);
898-
meta.update_processing_event(processingID, retval);
899-
meta.update_processing_status(processingID, ProcessingStatus.failed);
900-
System.exit(retval.getExitStatus());
901-
}
902-
}
883+
this.writeStringToFile(file, true, processingID + System.getProperty("line.separator"));
903884
}
904885
Log.debug("Completed processingIDFiles");
905886

906887
// Try to write to each processingAccessionFile until success or timeout
907888
for (File file : processingAccessionFiles) {
908889
Log.debug("Writing out to " + file.toString());
909-
writeProcessingAccessionToFile(file, true);
890+
writeStringToFile(file, true, processingAccession + System.getProperty("line.separator"));
910891
}
911892
Log.debug("Completed processingAccessionFiles");
912893
if (processingAccessionFileCheck != null) {
913-
writeProcessingAccessionToFile(processingAccessionFileCheck, false);
894+
writeStringToFile(processingAccessionFileCheck, true, processingAccession + System.getProperty("line.separator"));
914895
}
915896
Log.debug("Completed processingAccessionFileCheck");
916897
meta.update_processing_status(processingID, ProcessingStatus.success);
@@ -974,7 +955,7 @@ private void invokeModuleMethods() {
974955
* <p>
975956
* run.
976957
* </p>
977-
*
958+
*
978959
* @param args
979960
* an array of {@link java.lang.String} objects.
980961
*/

seqware-webservice/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
<include>**/WSResourceTestSuite.java</include>
142142
<include>**/plugins/*.java</include>
143143
<include>**/deciders/*.java</include>
144+
<include>**/runner/*.java</include>
144145
</includes>
145146
</configuration>
146147
</plugin>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright (C) 2012 SeqWare
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package net.sourceforge.seqware.pipeline.runner;
18+
19+
import io.seqware.Reports;
20+
import java.io.IOException;
21+
import java.nio.file.Files;
22+
import java.nio.file.Path;
23+
import net.sourceforge.seqware.common.module.ReturnValue;
24+
import net.sourceforge.seqware.common.util.testtools.BasicTestDatabaseCreator;
25+
import net.sourceforge.seqware.pipeline.plugins.ExtendedPluginTest;
26+
import net.sourceforge.seqware.pipeline.plugins.ModuleRunner;
27+
import org.apache.commons.io.FileUtils;
28+
import org.junit.Before;
29+
import org.junit.BeforeClass;
30+
import org.junit.Rule;
31+
import org.junit.Test;
32+
import org.junit.contrib.java.lang.system.ExpectedSystemExit;
33+
34+
/**
35+
* Runs tests for the ModuleRunner class.
36+
*
37+
* @author dyuen
38+
*/
39+
40+
public class ModuleRunnerTest extends ExtendedPluginTest {
41+
42+
/**
43+
* This allows us to intercept and ignore the various System.exit calls within runner so they don't take down the tests as well
44+
*/
45+
@Rule
46+
public final ExpectedSystemExit exit = ExpectedSystemExit.none();
47+
48+
@BeforeClass
49+
public static void beforeClass() {
50+
BasicTestDatabaseCreator.resetDatabaseWithUsers();
51+
Reports.triggerProvenanceReport();
52+
}
53+
54+
@Before
55+
@Override
56+
public void setUp() {
57+
instance = new ModuleRunner();
58+
super.setUp();
59+
}
60+
61+
public ModuleRunnerTest() {
62+
}
63+
64+
@Test
65+
public void normalParentAccessionRunTest() throws IOException {
66+
System.out.println("normalParentAccessionRunTest");
67+
Path parentAccession = Files.createTempFile("accession", "test");
68+
FileUtils.write(parentAccession.toFile(), "836");
69+
exit.expectSystemExitWithStatus(ReturnValue.SUCCESS);
70+
launchPlugin("--metadata", "--metadata-parent-accession-file", parentAccession.toAbsolutePath().toString(), "--module",
71+
"net.sourceforge.seqware.pipeline.modules.GenericCommandRunner", "--", "--gcr-algorithm", "bash_cp", "--gcr-command",
72+
"echo");
73+
}
74+
75+
@Test
76+
public void normalAccessionFileRunTest() throws IOException {
77+
System.out.println("normalAccessionFileRunTest");
78+
Path resultAccession = Files.createTempFile("accession", "test");
79+
exit.expectSystemExitWithStatus(ReturnValue.SUCCESS);
80+
launchPlugin("--metadata", "--metadata-processing-accession-file", resultAccession.toAbsolutePath().toString(), "--module",
81+
"net.sourceforge.seqware.pipeline.modules.GenericCommandRunner", "--", "--gcr-algorithm", "bash_cp", "--gcr-command",
82+
"echo");
83+
}
84+
85+
@Test
86+
public void testBlankParentAccessionFileFail() throws IOException {
87+
System.out.println("testBlankParentAccessionFile");
88+
Path parentAccession = Files.createTempFile("accession", "test");
89+
exit.expectSystemExitWithStatus(ReturnValue.METADATAINVALIDIDCHAIN);
90+
launchPlugin("--metadata", "--metadata-parent-accession-file", parentAccession.toAbsolutePath().toString(), "--module",
91+
"net.sourceforge.seqware.pipeline.modules.GenericCommandRunner", "--", "--gcr-algorithm", "bash_cp", "--gcr-command",
92+
"echo");
93+
}
94+
95+
@Test
96+
public void testInvalidAccessionFileFail() throws IOException {
97+
System.out.println("testInvalidAccessionFile");
98+
Path parentAccession = Files.createTempFile("accession", "test");
99+
FileUtils.write(parentAccession.toFile(), "-1");
100+
exit.expectSystemExitWithStatus(ReturnValue.SQLQUERYFAILED);
101+
launchPlugin("--metadata", "--metadata-parent-accession-file", parentAccession.toAbsolutePath().toString(), "--module",
102+
"net.sourceforge.seqware.pipeline.modules.GenericCommandRunner", "--", "--gcr-algorithm", "bash_cp", "--gcr-command",
103+
"echo");
104+
}
105+
106+
}

0 commit comments

Comments
 (0)