This is an automated email from the ASF dual-hosted git repository. sk0x50 pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/ignite-3.git
The following commit(s) were added to refs/heads/main by this push: new 885de79599f IGNITE-26169 Update code deployment example (#6398) 885de79599f is described below commit 885de79599fdc1f46329eeed3af6cefecbd0ccdc Author: jinxxxoid <jinxxxinthemir...@gmail.com> AuthorDate: Tue Aug 26 14:50:59 2025 +0400 IGNITE-26169 Update code deployment example (#6398) --- .../code/deployment/CodeDeploymentExample.java | 58 ++++++++++++++++++ .../ignite/example/code/deployment/MyJob.java | 71 ++++++++++++++++++++++ .../example/code/deployment/resources/script.sh | 3 + 3 files changed, 132 insertions(+) diff --git a/examples/src/main/java/org/apache/ignite/example/code/deployment/CodeDeploymentExample.java b/examples/src/main/java/org/apache/ignite/example/code/deployment/CodeDeploymentExample.java new file mode 100644 index 00000000000..1db20427358 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/example/code/deployment/CodeDeploymentExample.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.example.code.deployment; + +import org.apache.ignite.client.IgniteClient; +import org.apache.ignite.compute.JobDescriptor; +import org.apache.ignite.compute.JobTarget; +import org.apache.ignite.deployment.DeploymentUnit; + +public class CodeDeploymentExample { + + /** Deployment unit name. */ + private static final String DEPLOYMENT_UNIT_NAME = "codeDeploymentExampleUnit"; + + /** Deployment unit version. */ + private static final String DEPLOYMENT_UNIT_VERSION = "1.0.0"; + + /** + * Main method of the example. + * + * @param args The command line arguments. + */ + public static void main(String[] args) { + + System.out.println("\nConnecting to server..."); + + try (IgniteClient client = IgniteClient.builder().addresses("127.0.0.1:10800").build()) { + + System.out.println("\nConfiguring compute job..."); + + JobDescriptor<String, String> job = JobDescriptor.builder(MyJob.class) + .units(new DeploymentUnit(DEPLOYMENT_UNIT_NAME, DEPLOYMENT_UNIT_VERSION)).resultClass(String.class).build(); + + JobTarget target = JobTarget.anyNode(client.cluster().nodes()); + + System.out.println("\nExecuting compute job'" + "'..."); + + String result = client.compute().execute(target, job, "Hello from job"); + + System.out.println("\n=== Result ===\n" + result); + } + } +} diff --git a/examples/src/main/java/org/apache/ignite/example/code/deployment/MyJob.java b/examples/src/main/java/org/apache/ignite/example/code/deployment/MyJob.java new file mode 100644 index 00000000000..c11eebddf4e --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/example/code/deployment/MyJob.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.example.code.deployment; + +import java.io.OutputStream; +import org.apache.ignite.Ignite; +import org.apache.ignite.compute.ComputeJob; +import org.apache.ignite.compute.JobExecutionContext; + +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.concurrent.CompletableFuture; + +public class MyJob implements ComputeJob<String, String> { + @Override + public CompletableFuture<String> executeAsync(JobExecutionContext ctx, String arg) { + Ignite ignite = ctx.ignite(); + + /** Full path to the script we want to run */ + final String resPath = "/org/apache/ignite/example/code/deployment/resources/script.sh"; + + try (InputStream in = MyJob.class.getResourceAsStream(resPath)) { + if (in == null) { + throw new IllegalStateException("Resource not found: " + resPath); + } + + byte[] script = in.readAllBytes(); + + Process p = new ProcessBuilder("sh", "-s", "--", arg) + .redirectErrorStream(true) + .start(); + + try (OutputStream os = p.getOutputStream()) { + os.write(script); + } + + String out; + try (InputStream procOut = p.getInputStream()) { + out = new String(procOut.readAllBytes(), StandardCharsets.UTF_8).strip(); + } + + int exit = p.waitFor(); + if (exit != 0) { + throw new RuntimeException("Script exited with code " + exit + ":\n" + out); + } + + String result = "Node: " + ignite.name() + + "\nArg: " + arg + + "\nScript output:\n" + out; + + return CompletableFuture.completedFuture(result); + } catch (Exception e) { + throw new RuntimeException("Failed to run script", e); + } + } +} diff --git a/examples/src/main/java/org/apache/ignite/example/code/deployment/resources/script.sh b/examples/src/main/java/org/apache/ignite/example/code/deployment/resources/script.sh new file mode 100644 index 00000000000..4c0baef5110 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/example/code/deployment/resources/script.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env sh +printf "Script is running. Arg: %s\n" "$1" +date