|
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 | +} |
0 commit comments