Skip to content

Commit e902f7e

Browse files
committed
Address reviewer feedback
1 parent f3ed01d commit e902f7e

File tree

11 files changed

+818
-758
lines changed

11 files changed

+818
-758
lines changed
Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,69 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<!--
3-
~ Licensed to the Apache Software Foundation (ASF) under one or more
4-
~ contributor license agreements. See the NOTICE file distributed with
5-
~ this work for additional information regarding copyright ownership.
6-
~ The ASF licenses this file to You under the Apache License, Version 2.0
7-
~ (the "License"); you may not use this file except in compliance with
8-
~ the License. You may obtain a copy of the License at
9-
~
10-
~ http://www.apache.org/licenses/LICENSE-2.0
11-
~
12-
~ Unless required by applicable law or agreed to in writing, software
13-
~ distributed under the License is distributed on an "AS IS" BASIS,
14-
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15-
~ See the License for the specific language governing permissions and
16-
~ limitations under the License.
17-
-->
18-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20-
<modelVersion>4.0.0</modelVersion>
21-
<parent>
22-
<groupId>org.apache.dolphinscheduler</groupId>
23-
<artifactId>dolphinscheduler-alert-plugins</artifactId>
24-
<version>dev-SNAPSHOT</version>
25-
</parent>
26-
<artifactId>dolphinscheduler-alert-script</artifactId>
27-
<packaging>jar</packaging>
28-
29-
<properties>
30-
<plugin.name>alert.script</plugin.name>
31-
</properties>
32-
33-
<dependencies>
34-
<dependency>
35-
<groupId>org.apache.dolphinscheduler</groupId>
36-
<artifactId>dolphinscheduler-spi</artifactId>
37-
<scope>provided</scope>
38-
</dependency>
39-
40-
<dependency>
41-
<groupId>org.apache.dolphinscheduler</groupId>
42-
<artifactId>dolphinscheduler-alert-api</artifactId>
43-
<scope>provided</scope>
44-
</dependency>
45-
46-
<dependency>
47-
<groupId>org.apache.dolphinscheduler</groupId>
48-
<artifactId>dolphinscheduler-common</artifactId>
49-
<scope>provided</scope>
50-
</dependency>
51-
</dependencies>
52-
53-
<build>
54-
<plugins>
55-
<plugin>
56-
<groupId>org.apache.maven.plugins</groupId>
57-
<artifactId>maven-shade-plugin</artifactId>
58-
<executions>
59-
<execution>
60-
<goals>
61-
<goal>shade</goal>
62-
</goals>
63-
<phase>package</phase>
64-
</execution>
65-
</executions>
66-
</plugin>
67-
</plugins>
68-
</build>
69-
</project>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to You under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
<parent>
22+
<groupId>org.apache.dolphinscheduler</groupId>
23+
<artifactId>dolphinscheduler-alert-plugins</artifactId>
24+
<version>dev-SNAPSHOT</version>
25+
</parent>
26+
<artifactId>dolphinscheduler-alert-script</artifactId>
27+
<packaging>jar</packaging>
28+
29+
<properties>
30+
<plugin.name>alert.script</plugin.name>
31+
</properties>
32+
33+
<dependencies>
34+
<dependency>
35+
<groupId>org.apache.dolphinscheduler</groupId>
36+
<artifactId>dolphinscheduler-spi</artifactId>
37+
<scope>provided</scope>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>org.apache.dolphinscheduler</groupId>
42+
<artifactId>dolphinscheduler-alert-api</artifactId>
43+
<scope>provided</scope>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.apache.dolphinscheduler</groupId>
48+
<artifactId>dolphinscheduler-common</artifactId>
49+
<scope>provided</scope>
50+
</dependency>
51+
</dependencies>
52+
53+
<build>
54+
<plugins>
55+
<plugin>
56+
<groupId>org.apache.maven.plugins</groupId>
57+
<artifactId>maven-shade-plugin</artifactId>
58+
<executions>
59+
<execution>
60+
<goals>
61+
<goal>shade</goal>
62+
</goals>
63+
<phase>package</phase>
64+
</execution>
65+
</executions>
66+
</plugin>
67+
</plugins>
68+
</build>
69+
</project>
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,100 @@
1-
/*
2-
* Licensed to the Apache Software Foundation (ASF) under one or more
3-
* contributor license agreements. See the NOTICE file distributed with
4-
* this work for additional information regarding copyright ownership.
5-
* The ASF licenses this file to You under the Apache License, Version 2.0
6-
* (the "License"); you may not use this file except in compliance with
7-
* the License. You may obtain a copy of the License at
8-
*
9-
* http://www.apache.org/licenses/LICENSE-2.0
10-
*
11-
* Unless required by applicable law or agreed to in writing, software
12-
* distributed under the License is distributed on an "AS IS" BASIS,
13-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
* See the License for the specific language governing permissions and
15-
* limitations under the License.
16-
*/
17-
18-
package org.apache.dolphinscheduler.plugin.alert.script;
19-
20-
import java.io.IOException;
21-
import java.util.concurrent.TimeUnit;
22-
23-
import lombok.extern.slf4j.Slf4j;
24-
25-
@Slf4j
26-
public final class ProcessUtils {
27-
28-
private ProcessUtils() {
29-
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
30-
}
31-
32-
/**
33-
* executeScript with timeout
34-
*
35-
* @param timeoutSeconds timeout in seconds, if <= 0 waits indefinitely
36-
* @param cmd cmd params
37-
* @return exit code, -1 if error, -2 if timeout
38-
*/
39-
static Integer executeScript(long timeoutSeconds, String... cmd) {
40-
41-
int exitCode = -1;
42-
43-
ProcessBuilder processBuilder = new ProcessBuilder(cmd);
44-
try {
45-
Process process = processBuilder.start();
46-
StreamGobbler inputStreamGobbler = new StreamGobbler(process.getInputStream());
47-
StreamGobbler errorStreamGobbler = new StreamGobbler(process.getErrorStream());
48-
49-
inputStreamGobbler.start();
50-
errorStreamGobbler.start();
51-
52-
boolean finished = process.waitFor(timeoutSeconds, TimeUnit.SECONDS);
53-
if (!finished) {
54-
log.error("script execution timed out after {} seconds, destroying process", timeoutSeconds);
55-
process.destroyForcibly();
56-
inputStreamGobbler.interrupt();
57-
errorStreamGobbler.interrupt();
58-
return -2;
59-
}
60-
return process.exitValue();
61-
} catch (IOException | InterruptedException e) {
62-
log.error("execute alert script error {}", e.getMessage());
63-
Thread.currentThread().interrupt();
64-
}
65-
66-
return exitCode;
67-
}
68-
}
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.dolphinscheduler.plugin.alert.script;
19+
20+
import java.io.IOException;
21+
import java.util.concurrent.TimeUnit;
22+
23+
import lombok.extern.slf4j.Slf4j;
24+
25+
@Slf4j
26+
public final class ProcessUtils {
27+
28+
private ProcessUtils() {
29+
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
30+
}
31+
32+
/**
33+
* executeScript with timeout
34+
*
35+
* @param timeoutSeconds timeout in seconds, if <= 0 waits indefinitely
36+
* @param cmd cmd params
37+
* @return exit code, -1 if error, -2 if timeout
38+
*/
39+
static Integer executeScript(long timeoutSeconds, String... cmd) {
40+
41+
int exitCode = -1;
42+
43+
ProcessBuilder processBuilder = new ProcessBuilder(cmd);
44+
try {
45+
Process process = processBuilder.start();
46+
StreamGobbler inputStreamGobbler = new StreamGobbler(process.getInputStream());
47+
StreamGobbler errorStreamGobbler = new StreamGobbler(process.getErrorStream());
48+
49+
inputStreamGobbler.start();
50+
errorStreamGobbler.start();
51+
52+
if (timeoutSeconds > 0) {
53+
boolean finished = process.waitFor(timeoutSeconds, TimeUnit.SECONDS);
54+
if (!finished) {
55+
log.error("script execution timed out after {} seconds, destroying process", timeoutSeconds);
56+
process.destroyForcibly();
57+
closeProcessStreams(process);
58+
joinGobbler(inputStreamGobbler);
59+
joinGobbler(errorStreamGobbler);
60+
return -2;
61+
}
62+
} else {
63+
process.waitFor();
64+
}
65+
int processExitCode = process.exitValue();
66+
joinGobbler(inputStreamGobbler);
67+
joinGobbler(errorStreamGobbler);
68+
return processExitCode;
69+
} catch (InterruptedException e) {
70+
log.error("execute alert script interrupted {}", e.getMessage());
71+
Thread.currentThread().interrupt();
72+
} catch (IOException e) {
73+
log.error("execute alert script error {}", e.getMessage());
74+
}
75+
76+
return exitCode;
77+
}
78+
79+
private static void closeProcessStreams(Process process) {
80+
try {
81+
process.getInputStream().close();
82+
} catch (IOException e) {
83+
log.warn("Failed to close process input stream after timeout", e);
84+
}
85+
try {
86+
process.getErrorStream().close();
87+
} catch (IOException e) {
88+
log.warn("Failed to close process error stream after timeout", e);
89+
}
90+
}
91+
92+
private static void joinGobbler(StreamGobbler gobbler) {
93+
try {
94+
gobbler.interrupt();
95+
gobbler.join(TimeUnit.SECONDS.toMillis(1));
96+
} catch (InterruptedException e) {
97+
Thread.currentThread().interrupt();
98+
}
99+
}
100+
}
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
/*
2-
* Licensed to the Apache Software Foundation (ASF) under one or more
3-
* contributor license agreements. See the NOTICE file distributed with
4-
* this work for additional information regarding copyright ownership.
5-
* The ASF licenses this file to You under the Apache License, Version 2.0
6-
* (the "License"); you may not use this file except in compliance with
7-
* the License. You may obtain a copy of the License at
8-
*
9-
* http://www.apache.org/licenses/LICENSE-2.0
10-
*
11-
* Unless required by applicable law or agreed to in writing, software
12-
* distributed under the License is distributed on an "AS IS" BASIS,
13-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
* See the License for the specific language governing permissions and
15-
* limitations under the License.
16-
*/
17-
18-
package org.apache.dolphinscheduler.plugin.alert.script;
19-
20-
import org.apache.dolphinscheduler.alert.api.AlertChannel;
21-
import org.apache.dolphinscheduler.alert.api.AlertData;
22-
import org.apache.dolphinscheduler.alert.api.AlertInfo;
23-
import org.apache.dolphinscheduler.alert.api.AlertResult;
24-
25-
import org.apache.commons.collections4.MapUtils;
26-
27-
import java.util.Map;
28-
29-
public final class ScriptAlertChannel implements AlertChannel {
30-
31-
@Override
32-
public AlertResult process(AlertInfo alertinfo) {
33-
AlertData alertData = alertinfo.getAlertData();
34-
Map<String, String> paramsMap = alertinfo.getAlertParams();
35-
if (MapUtils.isEmpty(paramsMap)) {
36-
return new AlertResult(false, "script params is empty");
37-
}
38-
return new ScriptSender(paramsMap).sendScriptAlert(alertData.getTitle(), alertData.getContent());
39-
}
40-
}
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.dolphinscheduler.plugin.alert.script;
19+
20+
import org.apache.dolphinscheduler.alert.api.AlertChannel;
21+
import org.apache.dolphinscheduler.alert.api.AlertData;
22+
import org.apache.dolphinscheduler.alert.api.AlertInfo;
23+
import org.apache.dolphinscheduler.alert.api.AlertResult;
24+
25+
import org.apache.commons.collections4.MapUtils;
26+
27+
import java.util.Map;
28+
29+
public final class ScriptAlertChannel implements AlertChannel {
30+
31+
@Override
32+
public AlertResult process(AlertInfo alertinfo) {
33+
AlertData alertData = alertinfo.getAlertData();
34+
Map<String, String> paramsMap = alertinfo.getAlertParams();
35+
if (MapUtils.isEmpty(paramsMap)) {
36+
return new AlertResult(false, "script params is empty");
37+
}
38+
return new ScriptSender(paramsMap).sendScriptAlert(alertData.getTitle(), alertData.getContent());
39+
}
40+
}

0 commit comments

Comments
 (0)