From 4c9a6471321a5050ef7b330d063a79a221e91ebb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E5=85=AB?= Date: Thu, 23 Jul 2020 20:09:10 +0800 Subject: [PATCH] [dev] use script's exitValue to deterime whether the task is success (issue#28) --- .../core/processor/built/ScriptProcessor.java | 4 ++- .../kfcfans/powerjob/ScriptProcessorTest.java | 25 ++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/powerjob-worker/src/main/java/com/github/kfcfans/powerjob/worker/core/processor/built/ScriptProcessor.java b/powerjob-worker/src/main/java/com/github/kfcfans/powerjob/worker/core/processor/built/ScriptProcessor.java index 402b39ec..2780f86c 100644 --- a/powerjob-worker/src/main/java/com/github/kfcfans/powerjob/worker/core/processor/built/ScriptProcessor.java +++ b/powerjob-worker/src/main/java/com/github/kfcfans/powerjob/worker/core/processor/built/ScriptProcessor.java @@ -96,7 +96,9 @@ public abstract class ScriptProcessor implements BasicProcessor { } String result = String.format("[INPUT]: %s;[ERROR]: %s", inputSB.toString(), errorSB.toString()); - return new ProcessResult(true, result); + // 0 代表正常退出 + int exitValue = process.exitValue(); + return new ProcessResult(exitValue == 0, result); }catch (InterruptedException ie) { omsLogger.info("SYSTEM===> ScriptProcessor has been interrupted"); return new ProcessResult(false, "Interrupted"); diff --git a/powerjob-worker/src/test/java/com/github/kfcfans/powerjob/ScriptProcessorTest.java b/powerjob-worker/src/test/java/com/github/kfcfans/powerjob/ScriptProcessorTest.java index e4fbdba8..e7217255 100644 --- a/powerjob-worker/src/test/java/com/github/kfcfans/powerjob/ScriptProcessorTest.java +++ b/powerjob-worker/src/test/java/com/github/kfcfans/powerjob/ScriptProcessorTest.java @@ -1,9 +1,14 @@ package com.github.kfcfans.powerjob; +import com.github.kfcfans.powerjob.worker.core.processor.TaskContext; import com.github.kfcfans.powerjob.worker.core.processor.built.PythonProcessor; import com.github.kfcfans.powerjob.worker.core.processor.built.ShellProcessor; +import com.github.kfcfans.powerjob.worker.log.impl.OmsServerLogger; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import java.util.concurrent.ThreadLocalRandom; + /** * 测试脚本处理器 * @@ -14,25 +19,37 @@ public class ScriptProcessorTest { private static final long timeout = 10000; + private static final TaskContext context = new TaskContext(); + + @BeforeAll + public static void initContext() { + context.setOmsLogger(new OmsServerLogger(1L)); + } + @Test public void testLocalShellProcessor() throws Exception { ShellProcessor sp = new ShellProcessor(1L, "ls -a", timeout); - sp.process(null); + System.out.println(sp.process(context)); ShellProcessor sp2 = new ShellProcessor(2777L, "pwd", timeout); - sp2.process(null); + System.out.println(sp2.process(context)); } @Test public void testLocalPythonProcessor() throws Exception { PythonProcessor pp = new PythonProcessor(2L, "print 'Hello World!'", timeout); - pp.process(null); + System.out.println(pp.process(context)); } @Test public void testNetShellProcessor() throws Exception { ShellProcessor sp = new ShellProcessor(18L, "http://localhost:8080/test/test.sh", timeout); - sp.process(null); + System.out.println(sp.process(context)); } + @Test + public void testFailedScript() throws Exception { + ShellProcessor sp3 = new ShellProcessor(ThreadLocalRandom.current().nextLong(), "mvn tjq", timeout); + System.out.println(sp3.process(context)); + } }