|
| 1 | +package jsheets.runtime.evaluation.shell; |
| 2 | + |
| 3 | +import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 4 | +import jsheets.StartEvaluationRequest; |
| 5 | +import jsheets.runtime.evaluation.Evaluation; |
| 6 | +import jsheets.runtime.evaluation.EvaluationEngine; |
| 7 | +import jsheets.runtime.evaluation.shell.environment.ExecutionEnvironment; |
| 8 | + |
| 9 | +import java.time.Clock; |
| 10 | +import java.util.Objects; |
| 11 | +import java.util.concurrent.Executor; |
| 12 | +import java.util.concurrent.Executors; |
| 13 | + |
| 14 | +public final class ShellEvaluationEngine implements EvaluationEngine { |
| 15 | + private final Clock clock; |
| 16 | + private final Executor workerPool; |
| 17 | + private final ExecutionEnvironment executionEnvironment; |
| 18 | + |
| 19 | + private ShellEvaluationEngine( |
| 20 | + Clock clock, |
| 21 | + Executor workerPool, |
| 22 | + ExecutionEnvironment executionEnvironment |
| 23 | + ) { |
| 24 | + this.clock = clock; |
| 25 | + this.workerPool = workerPool; |
| 26 | + this.executionEnvironment = executionEnvironment; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public Evaluation start(StartEvaluationRequest request, Evaluation.Listener listener) { |
| 31 | + var evaluation = new ShellEvaluation(clock, executionEnvironment); |
| 32 | + workerPool.execute(() -> evaluation.start(request)); |
| 33 | + return evaluation; |
| 34 | + } |
| 35 | + |
| 36 | + public static Builder newBuilder() { |
| 37 | + return new Builder(); |
| 38 | + } |
| 39 | + |
| 40 | + public static final class Builder { |
| 41 | + private Clock clock = Clock.systemUTC(); |
| 42 | + private Executor workerPool; |
| 43 | + private ExecutionEnvironment environment; |
| 44 | + |
| 45 | + public Builder useClock(Clock clock) { |
| 46 | + Objects.requireNonNull(clock, "clock"); |
| 47 | + this.clock = clock; |
| 48 | + return this; |
| 49 | + } |
| 50 | + |
| 51 | + public Builder useWorkerPool(Executor pool) { |
| 52 | + Objects.requireNonNull(pool, "workerPool"); |
| 53 | + workerPool = pool; |
| 54 | + return this; |
| 55 | + } |
| 56 | + |
| 57 | + public Builder useEnvironment(ExecutionEnvironment environment) { |
| 58 | + Objects.requireNonNull(environment, "environment"); |
| 59 | + this.environment = environment; |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + public EvaluationEngine create() { |
| 64 | + return new ShellEvaluationEngine( |
| 65 | + clock, |
| 66 | + selectWorkerPool(), |
| 67 | + selectEnvironment() |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + private ExecutionEnvironment selectEnvironment() { |
| 72 | + return Objects.requireNonNull(environment, "environment"); |
| 73 | + } |
| 74 | + |
| 75 | + private Executor selectWorkerPool() { |
| 76 | + return workerPool == null ? createDefaultWorkerPool() : workerPool; |
| 77 | + } |
| 78 | + |
| 79 | + private Executor createDefaultWorkerPool() { |
| 80 | + return Executors.newCachedThreadPool( |
| 81 | + new ThreadFactoryBuilder() |
| 82 | + .setDaemon(true) |
| 83 | + .setNameFormat("shell-evaluation-executor-%d") |
| 84 | + .build() |
| 85 | + ); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments