Skip to content

Commit

Permalink
Use CountDownLatch to wait all copy taks completed.
Browse files Browse the repository at this point in the history
Source files will partially copied when using transient agent.

see: #82
  • Loading branch information
cizezsy committed May 13, 2019
1 parent 1e82b12 commit f4730a9
Showing 1 changed file with 20 additions and 6 deletions.
Expand Up @@ -40,8 +40,10 @@
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class DefaultSourceFileResolver extends SourceFileResolver {

Expand Down Expand Up @@ -79,15 +81,17 @@ public void resolveSourceFiles(Run<?, ?> run, FilePath workspace, TaskListener l
listener.getLogger().printf("%d source files need to be copied%n", paints.size());

ExecutorService service = Executors.newFixedThreadPool(5);
// wait until all tasks completed
CountDownLatch latch = new CountDownLatch(paints.entrySet().size());

paints.forEach((sourceFilePath, paint) -> {
service.submit(() -> {
paints.forEach((sourceFilePath, paint) -> service.submit(() -> {
try {
FilePath[] possibleFiles;
try {
if (getPossiblePaths() != null && getPossiblePaths().size() > 0) {
possibleFiles = workspace.act(new FindSourceFileCallable(sourceFilePath, getPossiblePaths()));
} else{
possibleFiles = workspace.act(new FindSourceFileCallable(sourceFilePath));
} else {
possibleFiles = workspace.act(new FindSourceFileCallable(sourceFilePath));
}
} catch (IOException | InterruptedException e) {
listener.getLogger().println(ExceptionUtils.getFullStackTrace(e));
Expand Down Expand Up @@ -115,9 +119,19 @@ public void resolveSourceFiles(Run<?, ?> run, FilePath workspace, TaskListener l
} else {
listener.getLogger().printf("Cannot found source file for %s%n", sourceFilePath);
}
});
});
} finally {
// ensure latch will count down
latch.countDown();
}
}));


try {
latch.await(1, TimeUnit.HOURS);
} catch (InterruptedException e) {
e.printStackTrace();
throw new IOException("Unable to copy source files", e);
}
}


Expand Down

0 comments on commit f4730a9

Please sign in to comment.