-
Notifications
You must be signed in to change notification settings - Fork 5k
[Improvement-18034][alert] Add configurable timeout for script alert plugin #18050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,10 +21,12 @@ | |
| import org.apache.dolphinscheduler.alert.api.AlertChannelFactory; | ||
| import org.apache.dolphinscheduler.alert.api.AlertInputTips; | ||
| import org.apache.dolphinscheduler.common.utils.JSONUtils; | ||
| import org.apache.dolphinscheduler.spi.params.base.DataType; | ||
| import org.apache.dolphinscheduler.spi.params.base.ParamsOptions; | ||
| import org.apache.dolphinscheduler.spi.params.base.PluginParams; | ||
| import org.apache.dolphinscheduler.spi.params.base.Validate; | ||
| import org.apache.dolphinscheduler.spi.params.input.InputParam; | ||
| import org.apache.dolphinscheduler.spi.params.input.number.InputNumberParam; | ||
| import org.apache.dolphinscheduler.spi.params.radio.RadioParam; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
@@ -66,7 +68,18 @@ public List<PluginParams> params() { | |
| .addValidate(Validate.newBuilder().setRequired(true).build()) | ||
| .build(); | ||
|
|
||
| return Arrays.asList(scriptUserParam, scriptPathParam, scriptTypeParams); | ||
| InputNumberParam scriptTimeoutParam = | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is best to tell the user the meaning of the exit code in the document. |
||
| InputNumberParam.newBuilder(ScriptParamsConstants.NAME_SCRIPT_TIMEOUT, | ||
| ScriptParamsConstants.SCRIPT_TIMEOUT) | ||
| .setValue(ScriptParamsConstants.DEFAULT_SCRIPT_TIMEOUT) | ||
| .addValidate(Validate.newBuilder() | ||
| .setType(DataType.NUMBER.getDataType()) | ||
| .setRequired(false) | ||
| .setMin(0D) | ||
| .build()) | ||
| .build(); | ||
|
|
||
| return Arrays.asList(scriptUserParam, scriptPathParam, scriptTypeParams, scriptTimeoutParam); | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -36,6 +36,7 @@ public final class ScriptSender { | |||||
| private final String scriptPath; | ||||||
| private final String scriptType; | ||||||
| private final String userParams; | ||||||
| private final long timeout; | ||||||
|
|
||||||
| ScriptSender(Map<String, String> config) { | ||||||
| scriptPath = StringUtils.isNotBlank(config.get(ScriptParamsConstants.NAME_SCRIPT_PATH)) | ||||||
|
|
@@ -47,6 +48,19 @@ public final class ScriptSender { | |||||
| userParams = StringUtils.isNotBlank(config.get(ScriptParamsConstants.NAME_SCRIPT_USER_PARAMS)) | ||||||
| ? config.get(ScriptParamsConstants.NAME_SCRIPT_USER_PARAMS) | ||||||
| : ""; | ||||||
| String timeoutConfig = config.get(ScriptParamsConstants.NAME_SCRIPT_TIMEOUT); | ||||||
| if (StringUtils.isNotBlank(timeoutConfig)) { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| long parsedTimeout = ScriptParamsConstants.DEFAULT_SCRIPT_TIMEOUT; | ||||||
| try { | ||||||
| parsedTimeout = Long.parseLong(timeoutConfig); | ||||||
| } catch (NumberFormatException ex) { | ||||||
| log.warn("Invalid script timeout config value: '{}', using default: {}", | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should return |
||||||
| timeoutConfig, ScriptParamsConstants.DEFAULT_SCRIPT_TIMEOUT, ex); | ||||||
| } | ||||||
| timeout = parsedTimeout; | ||||||
| } else { | ||||||
| timeout = ScriptParamsConstants.DEFAULT_SCRIPT_TIMEOUT; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| AlertResult sendScriptAlert(String title, String content) { | ||||||
|
|
@@ -108,13 +122,19 @@ private AlertResult executeShellScript(String title, String content) { | |||||
|
|
||||||
| String[] cmd = {"/bin/sh", "-c", scriptPath + ALERT_TITLE_OPTION + "'" + title + "'" + ALERT_CONTENT_OPTION | ||||||
| + "'" + content + "'" + ALERT_USER_PARAMS_OPTION + "'" + userParams + "'"}; | ||||||
| int exitCode = ProcessUtils.executeScript(cmd); | ||||||
| int exitCode = ProcessUtils.executeScript(timeout, cmd); | ||||||
|
|
||||||
| if (exitCode == 0) { | ||||||
| alertResult.setSuccess(true); | ||||||
| alertResult.setMessage("send script alert msg success"); | ||||||
| return alertResult; | ||||||
| } | ||||||
| if (exitCode == -2) { | ||||||
| alertResult.setMessage("send script alert msg error, script execution timed out after " + timeout | ||||||
| + " seconds"); | ||||||
| log.error("send script alert msg error, script execution timed out after {} seconds", timeout); | ||||||
| return alertResult; | ||||||
| } | ||||||
| alertResult.setMessage("send script alert msg error,exitCode is " + exitCode); | ||||||
| log.info("send script alert msg error,exitCode is {}", exitCode); | ||||||
| return alertResult; | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,10 @@ | |
|
|
||
| package org.apache.dolphinscheduler.plugin.alert.script; | ||
|
|
||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.condition.DisabledOnOs; | ||
| import org.junit.jupiter.api.condition.OS; | ||
|
|
||
| /** | ||
| * ProcessUtilsTest | ||
|
|
@@ -32,8 +35,17 @@ | |
| private String[] cmd = {"/bin/sh", "-c", shellFilPath + " -t 1"}; | ||
|
|
||
| @Test | ||
| @DisabledOnOs(OS.WINDOWS) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need to do this since we've decleared in the document that windows is not supported. |
||
| public void testExecuteScript() { | ||
| int code = ProcessUtils.executeScript(cmd); | ||
| assert code != -1; | ||
| int code = ProcessUtils.executeScript(60, cmd); | ||
| Assertions.assertNotEquals(-1, code); | ||
| } | ||
|
|
||
| @Test | ||
| @DisabledOnOs(OS.WINDOWS) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
| public void testExecuteScriptTimeout() { | ||
|
Check warning on line 46 in dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtilsTest.java
|
||
| String[] sleepCmd = {"/bin/sh", "-c", "sleep 30"}; | ||
| int code = ProcessUtils.executeScript(2, sleepCmd); | ||
| Assertions.assertEquals(-2, code); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to use positive integer here.