Jelajahi Sumber

Merge pull request #4814 from CalvinKirs/script_thread

[Improvement][spi-alert]script plugin should contain alert content
gaojun2048 4 tahun lalu
induk
melakukan
0d1bbd8e4b

+ 1 - 1
dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannel.java

@@ -36,6 +36,6 @@ public class ScriptAlertChannel implements AlertChannel {
         if (null == paramsMap) {
             return new AlertResult("false", "ding talk params is null");
         }
-        return new ScriptSender(paramsMap).sendScriptAlert(alertData.getTitle());
+        return new ScriptSender(paramsMap).sendScriptAlert(alertData.getTitle(),alertData.getContent());
     }
 }

+ 10 - 4
dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSender.java

@@ -37,28 +37,34 @@ public class ScriptSender {
 
     private String userParams;
 
+    private static final String ALERT_TITLE_OPTION = " -t ";
+
+    private static final String ALERT_CONTENT_OPTION = " -c ";
+
+    private static final String ALERT_USER_PARAMS_OPTION = " -p ";
+
     ScriptSender(Map<String, String> config) {
         scriptPath = config.get(ScriptParamsConstants.NAME_SCRIPT_PATH);
         scriptType = config.get(ScriptParamsConstants.NAME_SCRIPT_TYPE);
         userParams = config.get(ScriptParamsConstants.NAME_SCRIPT_USER_PARAMS);
     }
 
-    AlertResult sendScriptAlert(String msg) {
+    AlertResult sendScriptAlert(String title, String content) {
         AlertResult alertResult = new AlertResult();
         if (ScriptType.SHELL.getDescp().equals(scriptType)) {
-            return executeShellScript(msg);
+            return executeShellScript(title, content);
         }
         return alertResult;
     }
 
-    private AlertResult executeShellScript(String msg) {
+    private AlertResult executeShellScript(String title, String content) {
         AlertResult alertResult = new AlertResult();
         alertResult.setStatus("false");
         if (Boolean.TRUE.equals(OSUtils.isWindows())) {
             alertResult.setMessage("shell script not support windows os");
             return alertResult;
         }
-        String[] cmd = {"/bin/sh", "-c", scriptPath + " " + msg + " " + userParams};
+        String[] cmd = {"/bin/sh", "-c", scriptPath + ALERT_TITLE_OPTION + "'" + title + "'" + ALERT_CONTENT_OPTION + "'" + content + "'" + ALERT_USER_PARAMS_OPTION + "'" + userParams + "'"};
         int exitCode = ProcessUtils.executeScript(cmd);
 
         if (exitCode == 0) {

+ 7 - 0
dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/StreamGobbler.java

@@ -55,6 +55,13 @@ public class StreamGobbler extends Thread {
             }
         } catch (IOException e) {
             logger.error("I/O error occurs {}", e.getMessage());
+        } finally {
+            try {
+                inputBufferReader.close();
+                inputStreamReader.close();
+            } catch (IOException e) {
+                logger.error("I/O error occurs {}", e.getMessage());
+            }
         }
     }
 

+ 2 - 2
dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtilsTest.java

@@ -26,9 +26,9 @@ public class ProcessUtilsTest {
 
     private static final String rootPath = System.getProperty("user.dir");
 
-    private static final String shellFilPath = rootPath + "/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/example.sh";
+    private static final String shellFilPath = rootPath + "/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/test.sh";
 
-    private String[] cmd = {"/bin/sh", "-c", shellFilPath + " " + "testMsg" + " " + "userParams"};
+    private String[] cmd = {"/bin/sh", "-c", shellFilPath + " -t 1"};
 
     @Test
     public void testExecuteScript() {

+ 3 - 3
dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSenderTest.java

@@ -35,7 +35,7 @@ public class ScriptSenderTest {
 
     private static final String rootPath = System.getProperty("user.dir");
 
-    private static final String shellFilPath = rootPath + "/src/test/script/shell/scriptTest.sh";
+    private static final String shellFilPath = rootPath + "/src/test/script/shell/scriptExample.sh";
 
     @Before
     public void initScriptConfig() {
@@ -49,9 +49,9 @@ public class ScriptSenderTest {
     public void testScriptSenderTest() {
         ScriptSender scriptSender = new ScriptSender(scriptConfig);
         AlertResult alertResult;
-        alertResult = scriptSender.sendScriptAlert("success");
+        alertResult = scriptSender.sendScriptAlert("test title Kris", "test content");
         Assert.assertEquals("true", alertResult.getStatus());
-        alertResult = scriptSender.sendScriptAlert("errorMsg");
+        alertResult = scriptSender.sendScriptAlert("error msg title", "test content");
         Assert.assertEquals("false", alertResult.getStatus());
     }
 

+ 16 - 2
dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/example.sh

@@ -1,3 +1,4 @@
+#!/bin/bash
 #
 # Licensed to the Apache Software Foundation (ASF) under one or more
 # contributor license agreements.  See the NOTICE file distributed with
@@ -15,11 +16,24 @@
 # limitations under the License.
 #
 
+while getopts t:c:p: opts; do
+    case $opts in
+        t) t=$OPTARG ;;
+        c) c=$OPTARG ;;
+        p) p=$OPTARG ;;
+        ?) ;;
+    esac
+done
 
-msg=$1
-content=$2
 
 # Write your specific logic here
 
 # Set the exit code according to your execution result, and alert needs to use it to judge the status of this alarm result
+
+
+if  [ "$t" = "error msg title" ]
+   then
+     exit 12
+fi
 exit 0
+exit 0

+ 9 - 7
dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/scriptTest.sh

@@ -15,11 +15,13 @@
 # limitations under the License.
 #
 
-msg=$1
-content=$2
+while getopts t: opts; do
+    case $opts in
+        t) t=$OPTARG ;;
+        ?) ;;
+    esac
+done
 
-if  [ $msg = errorMsg ]
-   then
-     exit 12
-fi
-exit 0
+echo "$t"
+
+exit 0