forked from jenkinsci/pipeline-graph-view-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCachedPipelineNodeGraphAdaptor.java
More file actions
38 lines (30 loc) · 1.5 KB
/
CachedPipelineNodeGraphAdaptor.java
File metadata and controls
38 lines (30 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package io.jenkins.plugins.pipelinegraphview.utils;
import io.jenkins.plugins.pipelinegraphview.treescanner.PipelineNodeGraphAdapter;
import java.util.Map;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ConcurrentHashMap;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CachedPipelineNodeGraphAdaptor {
public static final CachedPipelineNodeGraphAdaptor instance = new CachedPipelineNodeGraphAdaptor();
private static final Logger log = LoggerFactory.getLogger(CachedPipelineNodeGraphAdaptor.class);
private final Map<String, CompletableFuture<PipelineNodeGraphAdapter>> tasks = new ConcurrentHashMap<>();
private CachedPipelineNodeGraphAdaptor() {}
public PipelineNodeGraphAdapter getFor(WorkflowRun run) {
String key = run.getExternalizableId();
CompletableFuture<PipelineNodeGraphAdapter> task = tasks.computeIfAbsent(key, (ignored) -> {
log.debug("Creating new PipelineNodeGraphAdapter for run: {}", key);
return CompletableFuture.supplyAsync(() -> new PipelineNodeGraphAdapter(run));
});
try {
return task.join();
} catch (CancellationException | CompletionException e) {
throw new RuntimeException("Failure computing graph for " + key, e);
} finally {
tasks.remove(key);
}
}
}