2828import com .github .dockerjava .core .DockerClientImpl ;
2929import com .github .dockerjava .core .NameParser ;
3030import com .github .dockerjava .httpclient5 .ApacheDockerHttpClient ;
31- import io .serverlessworkflow .api .types .Container ;
32- import io .serverlessworkflow .api .types .ContainerLifetime ;
33- import io .serverlessworkflow .api .types .TimeoutAfter ;
31+ import io .serverlessworkflow .api .types .ContainerLifetime .ContainerCleanupPolicy ;
3432import io .serverlessworkflow .impl .TaskContext ;
3533import io .serverlessworkflow .impl .WorkflowContext ;
36- import io .serverlessworkflow .impl .WorkflowDefinition ;
3734import io .serverlessworkflow .impl .WorkflowModel ;
3835import io .serverlessworkflow .impl .WorkflowUtils ;
3936import io .serverlessworkflow .impl .WorkflowValueResolver ;
37+ import io .serverlessworkflow .impl .executors .CallableTask ;
4038import java .io .IOException ;
4139import java .time .Duration ;
42- import java .util .ArrayList ;
4340import java .util .Collection ;
4441import java .util .Optional ;
4542import java .util .concurrent .CompletableFuture ;
4643import java .util .concurrent .TimeUnit ;
4744
48- class ContainerRunner {
45+ class ContainerRunner implements CallableTask {
4946
5047 private static final DefaultDockerClientConfig DEFAULT_CONFIG =
5148 DefaultDockerClientConfig .createDefaultConfigBuilder ().build ();
@@ -64,14 +61,19 @@ private static class DockerClientHolder {
6461 private final ContainerCleanupPolicy policy ;
6562 private final String containerImage ;
6663
67- private ContainerRunner (ContainerRunnerBuilder builder ) {
68- this .propertySetters = builder .propertySetters ;
69- this .timeout = Optional .ofNullable (builder .timeout );
70- this .policy = builder .policy ;
71- this .containerImage = builder .containerImage ;
64+ public ContainerRunner (
65+ Collection <ContainerPropertySetter > propertySetters ,
66+ Optional <WorkflowValueResolver <Duration >> timeout ,
67+ ContainerCleanupPolicy policy ,
68+ String containerImage ) {
69+ this .propertySetters = propertySetters ;
70+ this .timeout = timeout ;
71+ this .policy = policy ;
72+ this .containerImage = containerImage ;
7273 }
7374
74- CompletableFuture <WorkflowModel > start (
75+ @ Override
76+ public CompletableFuture <WorkflowModel > apply (
7577 WorkflowContext workflowContext , TaskContext taskContext , WorkflowModel input ) {
7678 return CompletableFuture .supplyAsync (
7779 () -> startSync (workflowContext , taskContext , input ),
@@ -215,52 +217,4 @@ private static RuntimeException mapExitCode(int exit) {
215217 private static RuntimeException failed (String message ) {
216218 return new RuntimeException (message );
217219 }
218-
219- static ContainerRunnerBuilder builder () {
220- return new ContainerRunnerBuilder ();
221- }
222-
223- public static class ContainerRunnerBuilder {
224- private Container container ;
225- private WorkflowDefinition definition ;
226- private WorkflowValueResolver <Duration > timeout ;
227- private ContainerCleanupPolicy policy ;
228- private String containerImage ;
229- private Collection <ContainerPropertySetter > propertySetters = new ArrayList <>();
230-
231- private ContainerRunnerBuilder () {}
232-
233- ContainerRunnerBuilder withContainer (Container container ) {
234- this .container = container ;
235- return this ;
236- }
237-
238- public ContainerRunnerBuilder withWorkflowDefinition (WorkflowDefinition definition ) {
239- this .definition = definition ;
240- return this ;
241- }
242-
243- ContainerRunner build () {
244- propertySetters .add (new NamePropertySetter (definition , container ));
245- propertySetters .add (new CommandPropertySetter (definition , container ));
246- propertySetters .add (new ContainerEnvironmentPropertySetter (definition , container ));
247- propertySetters .add (new LifetimePropertySetter (container ));
248- propertySetters .add (new PortsPropertySetter (container ));
249- propertySetters .add (new VolumesPropertySetter (definition , container ));
250-
251- containerImage = container .getImage ();
252- if (containerImage == null || container .getImage ().isBlank ()) {
253- throw new IllegalArgumentException ("Container image must be provided" );
254- }
255- ContainerLifetime lifetime = container .getLifetime ();
256- if (lifetime != null ) {
257- policy = lifetime .getCleanup ();
258- TimeoutAfter afterTimeout = lifetime .getAfter ();
259- if (afterTimeout != null )
260- timeout = WorkflowUtils .fromTimeoutAfter (definition .application (), afterTimeout );
261- }
262-
263- return new ContainerRunner (this );
264- }
265- }
266220}
0 commit comments