|
| 1 | +/* |
| 2 | + * Copyright 2020-Present The Serverless Workflow Specification Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.serverlessworkflow.impl.container.executors; |
| 17 | + |
| 18 | +import static io.serverlessworkflow.api.types.ContainerLifetime.ContainerCleanupPolicy.*; |
| 19 | + |
| 20 | +import com.github.dockerjava.api.DockerClient; |
| 21 | +import com.github.dockerjava.api.command.CreateContainerCmd; |
| 22 | +import com.github.dockerjava.api.command.CreateContainerResponse; |
| 23 | +import com.github.dockerjava.api.command.WaitContainerResultCallback; |
| 24 | +import com.github.dockerjava.api.exception.DockerClientException; |
| 25 | +import com.github.dockerjava.core.DefaultDockerClientConfig; |
| 26 | +import com.github.dockerjava.core.DockerClientImpl; |
| 27 | +import com.github.dockerjava.httpclient5.ApacheDockerHttpClient; |
| 28 | +import io.serverlessworkflow.api.types.Container; |
| 29 | +import io.serverlessworkflow.impl.TaskContext; |
| 30 | +import io.serverlessworkflow.impl.WorkflowContext; |
| 31 | +import io.serverlessworkflow.impl.WorkflowDefinition; |
| 32 | +import io.serverlessworkflow.impl.WorkflowModel; |
| 33 | +import io.serverlessworkflow.impl.WorkflowUtils; |
| 34 | +import io.serverlessworkflow.impl.WorkflowValueResolver; |
| 35 | +import java.io.IOException; |
| 36 | +import java.time.Duration; |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.List; |
| 39 | +import java.util.concurrent.CompletableFuture; |
| 40 | +import java.util.concurrent.TimeUnit; |
| 41 | + |
| 42 | +class ContainerRunner { |
| 43 | + |
| 44 | + private static final DefaultDockerClientConfig DEFAULT_CONFIG = |
| 45 | + DefaultDockerClientConfig.createDefaultConfigBuilder().build(); |
| 46 | + |
| 47 | + private static final DockerClient dockerClient = |
| 48 | + DockerClientImpl.getInstance( |
| 49 | + DEFAULT_CONFIG, |
| 50 | + new ApacheDockerHttpClient.Builder().dockerHost(DEFAULT_CONFIG.getDockerHost()).build()); |
| 51 | + |
| 52 | + private final CreateContainerCmd createContainerCmd; |
| 53 | + private final Container container; |
| 54 | + private final List<ContainerPropertySetter> propertySetters; |
| 55 | + private final WorkflowDefinition definition; |
| 56 | + |
| 57 | + private ContainerRunner( |
| 58 | + CreateContainerCmd createContainerCmd, WorkflowDefinition definition, Container container) { |
| 59 | + this.createContainerCmd = createContainerCmd; |
| 60 | + this.definition = definition; |
| 61 | + this.container = container; |
| 62 | + this.propertySetters = new ArrayList<>(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Blocking container execution according to the lifetime policy. Returns an already completed |
| 67 | + * CompletableFuture: - completedFuture(input) if exitCode == 0 - exceptionally completed if the |
| 68 | + * exit code is non-zero or an error occurs. The method blocks the calling thread until the |
| 69 | + * container finishes or the timeout expires. |
| 70 | + */ |
| 71 | + CompletableFuture<WorkflowModel> startSync( |
| 72 | + WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) { |
| 73 | + |
| 74 | + StringExpressionResolver resolver = |
| 75 | + new StringExpressionResolver(workflowContext, taskContext, input); |
| 76 | + |
| 77 | + propertySetters.forEach(setter -> setter.accept(resolver)); |
| 78 | + |
| 79 | + CreateContainerResponse createContainerResponse = createContainerCmd.exec(); |
| 80 | + String containerId = createContainerResponse.getId(); |
| 81 | + |
| 82 | + if (containerId == null || containerId.isEmpty()) { |
| 83 | + return failed("Container creation failed: empty container ID"); |
| 84 | + } |
| 85 | + |
| 86 | + dockerClient.startContainerCmd(containerId).exec(); |
| 87 | + |
| 88 | + int exitCode; |
| 89 | + try (WaitContainerResultCallback resultCallback = |
| 90 | + dockerClient.waitContainerCmd(containerId).exec(new WaitContainerResultCallback())) { |
| 91 | + if (container.getLifetime() != null |
| 92 | + && container.getLifetime().getCleanup() != null |
| 93 | + && container.getLifetime().getCleanup().equals(EVENTUALLY)) { |
| 94 | + try { |
| 95 | + WorkflowValueResolver<Duration> durationResolver = |
| 96 | + WorkflowUtils.fromTimeoutAfter( |
| 97 | + definition.application(), container.getLifetime().getAfter()); |
| 98 | + Duration timeout = durationResolver.apply(workflowContext, taskContext, input); |
| 99 | + exitCode = resultCallback.awaitStatusCode(timeout.toMillis(), TimeUnit.MILLISECONDS); |
| 100 | + } catch (DockerClientException e) { |
| 101 | + return failed( |
| 102 | + String.format("Error while waiting for container to finish: %s ", e.getMessage())); |
| 103 | + } finally { |
| 104 | + dockerClient.removeContainerCmd(containerId).withForce(true).exec(); |
| 105 | + } |
| 106 | + } else { |
| 107 | + exitCode = resultCallback.awaitStatusCode(); |
| 108 | + } |
| 109 | + } catch (IOException e) { |
| 110 | + return failed( |
| 111 | + String.format("Error while waiting for container to finish: %s ", e.getMessage())); |
| 112 | + } |
| 113 | + |
| 114 | + return switch (exitCode) { |
| 115 | + case 0 -> CompletableFuture.completedFuture(input); |
| 116 | + case 1 -> failed("General error (exit code 1)"); |
| 117 | + case 2 -> failed("Shell syntax error (exit code 2)"); |
| 118 | + case 126 -> failed("Command found but not executable (exit code 126)"); |
| 119 | + case 127 -> failed("Command not found (exit code 127)"); |
| 120 | + case 130 -> failed("Interrupted by SIGINT (exit code 130)"); |
| 121 | + case 137 -> failed("Killed by SIGKILL (exit code 137)"); |
| 122 | + case 139 -> failed("Segmentation fault (exit code 139)"); |
| 123 | + case 143 -> failed("Terminated by SIGTERM (exit code 143)"); |
| 124 | + default -> failed("Process exited with code " + exitCode); |
| 125 | + }; |
| 126 | + } |
| 127 | + |
| 128 | + private static <T> CompletableFuture<T> failed(String message) { |
| 129 | + CompletableFuture<T> f = new CompletableFuture<>(); |
| 130 | + f.completeExceptionally(new RuntimeException(message)); |
| 131 | + return f; |
| 132 | + } |
| 133 | + |
| 134 | + static ContainerRunnerBuilder builder() { |
| 135 | + return new ContainerRunnerBuilder(); |
| 136 | + } |
| 137 | + |
| 138 | + public static class ContainerRunnerBuilder { |
| 139 | + private Container container = null; |
| 140 | + private WorkflowDefinition workflowDefinition; |
| 141 | + |
| 142 | + private ContainerRunnerBuilder() {} |
| 143 | + |
| 144 | + ContainerRunnerBuilder withContainer(Container container) { |
| 145 | + this.container = container; |
| 146 | + return this; |
| 147 | + } |
| 148 | + |
| 149 | + public ContainerRunnerBuilder withWorkflowDefinition(WorkflowDefinition definition) { |
| 150 | + this.workflowDefinition = definition; |
| 151 | + return this; |
| 152 | + } |
| 153 | + |
| 154 | + ContainerRunner build() { |
| 155 | + if (container.getImage() == null || container.getImage().isEmpty()) { |
| 156 | + throw new IllegalArgumentException("Container image must be provided"); |
| 157 | + } |
| 158 | + |
| 159 | + CreateContainerCmd createContainerCmd = dockerClient.createContainerCmd(container.getImage()); |
| 160 | + |
| 161 | + ContainerRunner runner = |
| 162 | + new ContainerRunner(createContainerCmd, workflowDefinition, container); |
| 163 | + |
| 164 | + runner.propertySetters.add(new CommandPropertySetter(createContainerCmd, container)); |
| 165 | + runner.propertySetters.add( |
| 166 | + new ContainerEnvironmentPropertySetter(createContainerCmd, container)); |
| 167 | + runner.propertySetters.add(new NamePropertySetter(createContainerCmd, container)); |
| 168 | + runner.propertySetters.add(new PortsPropertySetter(createContainerCmd, container)); |
| 169 | + runner.propertySetters.add(new VolumesPropertySetter(createContainerCmd, container)); |
| 170 | + runner.propertySetters.add(new LifetimePropertySetter(createContainerCmd, container)); |
| 171 | + return runner; |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments