Search in sources :

Example 1 with TaskTemplate

use of org.flyte.api.v1.TaskTemplate in project flytekit-java by flyteorg.

the class ProjectClosureTest method testCreateTaskTemplateForRunnableTaskWithResources.

@Test
public void testCreateTaskTemplateForRunnableTaskWithResources() {
    // given
    Map<Resources.ResourceName, String> resourceValues = new HashMap<>();
    resourceValues.put(Resources.ResourceName.MEMORY, "0.5Gi");
    Resources expectedResources = Resources.builder().limits(resourceValues).requests(resourceValues).build();
    RunnableTask task = createRunnableTask(expectedResources);
    String image = "my-image";
    // when
    TaskTemplate result = ProjectClosure.createTaskTemplateForRunnableTask(task, image);
    // then
    Container container = result.container();
    assertNotNull(container);
    assertThat(container.image(), equalTo(image));
    assertThat(container.resources(), equalTo(expectedResources));
    assertThat(result.interface_(), equalTo(TypedInterface.builder().inputs(SdkTypes.nulls().getVariableMap()).outputs(SdkTypes.nulls().getVariableMap()).build()));
    assertThat(result.custom(), equalTo(Struct.of(emptyMap())));
    assertThat(result.retries(), equalTo(RetryStrategy.builder().retries(0).build()));
    assertThat(result.type(), equalTo("java-task"));
}
Also used : TaskTemplate(org.flyte.api.v1.TaskTemplate) Container(org.flyte.api.v1.Container) HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) Resources(org.flyte.api.v1.Resources) RunnableTask(org.flyte.api.v1.RunnableTask) Test(org.junit.jupiter.api.Test)

Example 2 with TaskTemplate

use of org.flyte.api.v1.TaskTemplate in project flytekit-java by flyteorg.

the class ProtoUtilTest method shouldSerDeTaskTemplate.

@Test
void shouldSerDeTaskTemplate() {
    Container container = Container.builder().command(ImmutableList.of("echo")).args(ImmutableList.of("hello world")).env(ImmutableList.of(KeyValuePair.of("key", "value"))).image("alpine:3.7").build();
    Variable stringVar = ApiUtils.createVar(SimpleType.STRING);
    Variable integerVar = ApiUtils.createVar(SimpleType.INTEGER);
    TypedInterface interface_ = TypedInterface.builder().inputs(ImmutableMap.of("x", stringVar)).outputs(ImmutableMap.of("y", integerVar)).build();
    RetryStrategy retries = RetryStrategy.builder().retries(4).build();
    TaskTemplate template = TaskTemplate.builder().container(container).type("custom-task").interface_(interface_).retries(retries).custom(Struct.of(ImmutableMap.of("custom-prop", Struct.Value.ofStringValue("custom-value")))).build();
    Tasks.TaskTemplate templateProto = Tasks.TaskTemplate.newBuilder().setContainer(Tasks.Container.newBuilder().setImage("alpine:3.7").addCommand("echo").addArgs("hello world").addEnv(Literals.KeyValuePair.newBuilder().setKey("key").setValue("value").build()).build()).setMetadata(Tasks.TaskMetadata.newBuilder().setRuntime(Tasks.RuntimeMetadata.newBuilder().setType(Tasks.RuntimeMetadata.RuntimeType.FLYTE_SDK).setFlavor(ProtoUtil.RUNTIME_FLAVOR).setVersion(ProtoUtil.RUNTIME_VERSION).build()).setRetries(Literals.RetryStrategy.newBuilder().setRetries(4).build()).build()).setInterface(Interface.TypedInterface.newBuilder().setInputs(Interface.VariableMap.newBuilder().putVariables("x", Interface.Variable.newBuilder().setType(Types.LiteralType.newBuilder().setSimple(Types.SimpleType.STRING).build()).build()).build()).setOutputs(Interface.VariableMap.newBuilder().putVariables("y", Interface.Variable.newBuilder().setType(Types.LiteralType.newBuilder().setSimple(Types.SimpleType.INTEGER).build()).build()).build()).build()).setType("custom-task").setCustom(com.google.protobuf.Struct.newBuilder().putFields("custom-prop", Value.newBuilder().setStringValue("custom-value").build()).build()).build();
    Tasks.TaskTemplate serializedTemplate = ProtoUtil.serialize(template);
    TaskTemplate deserializedTemplate = ProtoUtil.deserialize(templateProto);
    assertThat(serializedTemplate, equalTo(templateProto));
    assertThat(deserializedTemplate, equalTo(template));
}
Also used : TaskTemplate(org.flyte.api.v1.TaskTemplate) TypedInterface(org.flyte.api.v1.TypedInterface) Container(org.flyte.api.v1.Container) Variable(org.flyte.api.v1.Variable) Tasks(flyteidl.core.Tasks) RetryStrategy(org.flyte.api.v1.RetryStrategy) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with TaskTemplate

use of org.flyte.api.v1.TaskTemplate in project flytekit-java by flyteorg.

the class ProjectClosure method applyCustom.

private static TaskSpec applyCustom(TaskSpec taskSpec, JFlyteCustom custom) {
    Struct rewrittenCustom = merge(custom.serializeToStruct(), taskSpec.taskTemplate().custom());
    TaskTemplate rewrittenTaskTemplate = taskSpec.taskTemplate().toBuilder().custom(rewrittenCustom).build();
    return TaskSpec.create(rewrittenTaskTemplate);
}
Also used : TaskTemplate(org.flyte.api.v1.TaskTemplate) Struct(org.flyte.api.v1.Struct)

Example 4 with TaskTemplate

use of org.flyte.api.v1.TaskTemplate in project flytekit-java by flyteorg.

the class FlyteAdminClientTest method shouldPropagateCreateTaskToStub.

@Test
public void shouldPropagateCreateTaskToStub() {
    TaskIdentifier identifier = TaskIdentifier.builder().domain(DOMAIN).project(PROJECT).name(TASK_NAME).version(TASK_VERSION).build();
    TypedInterface interface_ = TypedInterface.builder().inputs(ImmutableMap.of("x", createVar(SimpleType.STRING))).outputs(ImmutableMap.of("y", createVar(SimpleType.INTEGER))).build();
    Container container = Container.builder().command(ImmutableList.of(COMMAND)).args(ImmutableList.of()).image(IMAGE_NAME).env(ImmutableList.of(KeyValuePair.of("key", "value"))).build();
    RetryStrategy retries = RetryStrategy.builder().retries(4).build();
    TaskTemplate template = TaskTemplate.builder().container(container).type("custom-task").interface_(interface_).custom(Struct.of(emptyMap())).retries(retries).build();
    client.createTask(identifier, template);
    assertThat(stubService.createTaskRequest, equalTo(TaskOuterClass.TaskCreateRequest.newBuilder().setId(newIdentifier(ResourceType.TASK, TASK_NAME, TASK_VERSION)).setSpec(newTaskSpec()).build()));
}
Also used : TaskTemplate(org.flyte.api.v1.TaskTemplate) TypedInterface(org.flyte.api.v1.TypedInterface) Container(org.flyte.api.v1.Container) TaskIdentifier(org.flyte.api.v1.TaskIdentifier) PartialTaskIdentifier(org.flyte.api.v1.PartialTaskIdentifier) RetryStrategy(org.flyte.api.v1.RetryStrategy) Test(org.junit.Test)

Example 5 with TaskTemplate

use of org.flyte.api.v1.TaskTemplate in project flytekit-java by flyteorg.

the class Execute method execute.

private void execute() {
    Config config = Config.load();
    Collection<ClassLoader> modules = ClassLoaders.forModuleDir(config.moduleDir()).values();
    Map<String, FileSystem> fileSystems = FileSystemLoader.loadFileSystems(modules);
    FileSystem outputFs = FileSystemLoader.getFileSystem(fileSystems, outputPrefix);
    ProtoWriter protoWriter = new ProtoWriter(outputPrefix, outputFs);
    try {
        FileSystem inputFs = FileSystemLoader.getFileSystem(fileSystems, inputs);
        ProtoReader protoReader = new ProtoReader(inputFs);
        TaskTemplate taskTemplate = protoReader.getTaskTemplate(taskTemplatePath);
        ClassLoader packageClassLoader = PackageLoader.load(fileSystems, taskTemplate);
        // before we run anything, switch class loader, otherwise,
        // ServiceLoaders and other things wouldn't work, for instance,
        // FileSystemRegister in Apache Beam
        Map<String, Literal> outputs = withClassLoader(packageClassLoader, () -> {
            Map<String, Literal> input = protoReader.getInput(inputs);
            RunnableTask runnableTask = getTask(task);
            return runnableTask.run(input);
        });
        protoWriter.writeOutputs(outputs);
    } catch (ContainerError e) {
        LOG.error("failed to run task", e);
        protoWriter.writeError(ProtoUtil.serializeContainerError(e));
    } catch (Throwable e) {
        LOG.error("failed to run task", e);
        protoWriter.writeError(ProtoUtil.serializeThrowable(e));
    }
}
Also used : TaskTemplate(org.flyte.api.v1.TaskTemplate) FileSystem(org.flyte.jflyte.api.FileSystem) Literal(org.flyte.api.v1.Literal) ClassLoaders.withClassLoader(org.flyte.jflyte.ClassLoaders.withClassLoader) RunnableTask(org.flyte.api.v1.RunnableTask) ContainerError(org.flyte.api.v1.ContainerError)

Aggregations

TaskTemplate (org.flyte.api.v1.TaskTemplate)9 Container (org.flyte.api.v1.Container)5 RunnableTask (org.flyte.api.v1.RunnableTask)4 Test (org.junit.jupiter.api.Test)4 Tasks (flyteidl.core.Tasks)3 PartialTaskIdentifier (org.flyte.api.v1.PartialTaskIdentifier)3 TaskIdentifier (org.flyte.api.v1.TaskIdentifier)3 ByteString (com.google.protobuf.ByteString)2 HashMap (java.util.HashMap)2 ContainerError (org.flyte.api.v1.ContainerError)2 Literal (org.flyte.api.v1.Literal)2 Resources (org.flyte.api.v1.Resources)2 RetryStrategy (org.flyte.api.v1.RetryStrategy)2 Struct (org.flyte.api.v1.Struct)2 TypedInterface (org.flyte.api.v1.TypedInterface)2 ClassLoaders.withClassLoader (org.flyte.jflyte.ClassLoaders.withClassLoader)2 FileSystem (org.flyte.jflyte.api.FileSystem)2 Path (java.nio.file.Path)1 DynamicJobSpec (org.flyte.api.v1.DynamicJobSpec)1 DynamicWorkflowTask (org.flyte.api.v1.DynamicWorkflowTask)1