Skip to content

Commit

Permalink
Merge pull request #256 from BezrukovM/multiprocess-exit-codes
Browse files Browse the repository at this point in the history
Multiprocess exit codes
  • Loading branch information
carlwilson authored Jun 10, 2019
2 parents 1f9e45b + 68ef433 commit f730c11
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
8 changes: 4 additions & 4 deletions gui/src/main/java/org/verapdf/apps/Applications.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* This file is part of VeraPDF Library GUI, a module of the veraPDF project.
* Copyright (c) 2015, veraPDF Consortium <info@VERAPDF.org> All rights
* Copyright (c) 2015, veraPDF Consortium <info@verapdf.org> All rights
* reserved. VeraPDF Library GUI is free software: you can redistribute it
* and/or modify it under the terms of either: The GNU General public license
* GPLv3+. You should have received a copy of the GNU General Public License
Expand Down Expand Up @@ -46,10 +46,10 @@ public final class Applications {
public static final String UPDATE_SERVICE_NOT_AVAILABLE = "Update Service not available"; //$NON-NLS-1$
public static final String UPDATE_LATEST_VERSION = "You are currently running the latest version of veraPDF%s v%s"; //$NON-NLS-1$
public static final String UPDATE_OLD_VERSION = "You are NOT running the latest version of veraPDF.\nYou are running version %s, the latest version is %s.\n"; //$NON-NLS-1$
public static final String UPDATE_URI = "http://downloads.VERAPDF.org/rel/VERAPDF-installer.zip"; //$NON-NLS-1$
public static final String UPDATE_URI = "http://downloads.verapdf.org/rel/verapdf-installer.zip"; //$NON-NLS-1$

private static final String VERAPDF = "verapdf";
private static final String BAT_EXTENSION = ".bat";
private static final String VERAPDF_WITH_BAT = VERAPDF + ".bat";

private Applications() {
assert (false);
Expand All @@ -71,7 +71,7 @@ private static File getStartFile(File veraPdfDirectory) {
if (unixStartFile.isFile()) {
return unixStartFile;
}
File windowsStartFile = new File(veraPdfDirectory, VERAPDF+BAT_EXTENSION);
File windowsStartFile = new File(veraPdfDirectory, VERAPDF_WITH_BAT);
if (windowsStartFile.isFile()) {
return windowsStartFile;
}
Expand Down
18 changes: 16 additions & 2 deletions gui/src/main/java/org/verapdf/cli/CliConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,13 @@ public enum ExitCodes {
/** Some PDF files encrypted. */
ENCRYPTED_FILES(8, "Some PDFs encrypted."),
/** veraPDF exception thrown while processing */
VERAPDF_EXCEPTION(9, "veraPDF exception while processing"),
JAXB_EXCEPTION(10, "Java XML marshalling exception while processing result.");
VERAPDF_EXCEPTION(9, "VeraPDF exception while processing."),
/** JAXB exception thrown while processing */
JAXB_EXCEPTION(10, "Java XML marshalling exception while processing result."),
/** Failed to start multiprocess */
FAILED_MULTIPROCESS_START(11, "Failed to start multiprocess"),
/** Interrupted exception */
INTERRUPTED_EXCEPTION(12, "Interrupted exception while processing");

/** The numeric exit code for return to OS. */
public final int value;
Expand All @@ -81,6 +86,15 @@ public enum ExitCodes {
this.value = exitCode;
this.message = message;
}

public static ExitCodes fromValue(int code) {
for (ExitCodes exitCode : ExitCodes.values()) {
if (code == exitCode.value) {
return exitCode;
}
}
return null;
}
}

public static final String NAME_STDIN = "STDIN";
Expand Down
5 changes: 4 additions & 1 deletion gui/src/main/java/org/verapdf/cli/VeraPdfCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ public static void main(final String[] args) throws VeraPDFException {
if (cliArgParser.isServerMode() || cliArgParser.getNumberOfProcesses() < 2) {
System.exit(singleThreadProcess(cliArgParser).value);
} else {
MultiThreadProcessor.process(cliArgParser);
System.exit(MultiThreadProcessor.process(cliArgParser).value);
}
} catch (InterruptedException e) {
logger.log(Level.WARNING, "Interrupted", e);
System.exit(ExitCodes.INTERRUPTED_EXCEPTION.value);
} catch (OutOfMemoryError oome) {
final String message = "The JVM appears to have run out of memory"; //$NON-NLS-1$
logger.log(Level.WARNING, message, oome);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.verapdf.cli.multithread;

import org.verapdf.cli.CliConstants;
import org.verapdf.cli.VeraPdfCli;
import org.verapdf.processor.reports.ResultStructure;

Expand Down Expand Up @@ -95,6 +96,7 @@ private boolean closeProcess() {
} catch (InterruptedException e) {
LOGGER.log(Level.SEVERE, "Process interrupted exception", e);
}
this.multiThreadProcessor.countDown(CliConstants.ExitCodes.fromValue(process.exitValue()));
return isClosed;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.verapdf.apps.Applications;
import org.verapdf.apps.utils.ApplicationUtils;
import org.verapdf.cli.CliConstants;
import org.verapdf.cli.CliConstants.ExitCodes;
import org.verapdf.cli.commands.VeraCliArgParser;
import org.verapdf.processor.FormatOption;
import org.verapdf.processor.reports.ResultStructure;
Expand All @@ -13,9 +15,10 @@
import java.io.File;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.*;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -39,6 +42,9 @@ public class MultiThreadProcessor {

private boolean isFirstReport = true;

private ExitCodes currentExitCode = ExitCodes.VALID;
private CountDownLatch latch;

private MultiThreadProcessor(VeraCliArgParser cliArgParser) {
this.os = new BufferedOutputStream(System.out, DEFAULT_BUFFER_SIZE * COEFFICIENT_BUFFER_SIZE);

Expand All @@ -55,9 +61,12 @@ private MultiThreadProcessor(VeraCliArgParser cliArgParser) {
this.processingHandler = new MultiThreadProcessingHandlerImpl(reportWriter);
}

public static void process(VeraCliArgParser cliArgParser) {
public static ExitCodes process(VeraCliArgParser cliArgParser) throws InterruptedException {
MultiThreadProcessor processor = new MultiThreadProcessor(cliArgParser);
processor.startProcesses(cliArgParser.getNumberOfProcesses());
if (processor.currentExitCode != ExitCodes.VALID) {
return processor.currentExitCode;
}
return processor.startProcesses(cliArgParser.getNumberOfProcesses());
}

private File getVeraPdfStarterFile(VeraCliArgParser cliArgParser) {
Expand All @@ -70,6 +79,7 @@ private File getVeraPdfStarterFile(VeraCliArgParser cliArgParser) {
}
} catch (IllegalStateException e) {
LOGGER.log(Level.SEVERE, "Can't obtain veraPDF CLI script path", e);
this.currentExitCode = ExitCodes.FAILED_MULTIPROCESS_START;
}
}
return veraPDFPath;
Expand Down Expand Up @@ -102,12 +112,24 @@ private List<File> getFiles(List<String> pdfPaths, boolean isRecurse) {
return ApplicationUtils.filterPdfFiles(toFilter, isRecurse);
}

private void startProcesses(int numberOfProcesses) {
private ExitCodes startProcesses(int numberOfProcesses) throws InterruptedException {
int processesQuantity = Math.min(numberOfProcesses, filesToProcess.size());
latch = new CountDownLatch(processesQuantity);
ExecutorService executor = Executors.newFixedThreadPool(processesQuantity);
for (int i = 0; i < processesQuantity; i++) {
BaseCliRunner veraPDFRunner = new BaseCliRunner(this, veraPDFStarterPath.getAbsolutePath(), veraPDFParameters, filesToProcess);
new Thread(veraPDFRunner).start();
executor.submit(veraPDFRunner);
}
latch.await();
return this.currentExitCode;
}

public void countDown(ExitCodes exitCode) {
if (exitCode != null && exitCode.value > this.currentExitCode.value) {
this.currentExitCode = exitCode;
}
if (this.latch != null) {
this.latch.countDown();
}
}
}

0 comments on commit f730c11

Please sign in to comment.