■ ■ ■ ■ ■ ■
commons-utils/src/main/java/io/onedev/commons/utils/command/Commandline.java
| skipped 13 lines |
14 | 14 | | import java.util.concurrent.Executors; |
15 | 15 | | import java.util.concurrent.TimeoutException; |
16 | 16 | | import java.util.concurrent.atomic.AtomicBoolean; |
| 17 | + | import java.util.concurrent.atomic.AtomicLong; |
17 | 18 | | |
18 | 19 | | import javax.annotation.Nullable; |
19 | 20 | | |
| skipped 216 lines |
236 | 237 | | throw new RuntimeException(e); |
237 | 238 | | } |
238 | 239 | | |
239 | | - | ProcessStreamPumper streamPumper = new ProcessStreamPumper(process, stdout, stderr, stdin); |
240 | | - | |
241 | 240 | | ExecutionResult result = new ExecutionResult(this); |
242 | 241 | | if (timeout != 0) { |
| 242 | + | AtomicLong lastActiveTime = new AtomicLong(System.currentTimeMillis()); |
| 243 | + | |
| 244 | + | class OutputStreamWrapper extends OutputStream { |
| 245 | + | |
| 246 | + | private final OutputStream delegate; |
| 247 | + | |
| 248 | + | public OutputStreamWrapper(OutputStream delegate) { |
| 249 | + | this.delegate = delegate; |
| 250 | + | } |
| 251 | + | |
| 252 | + | @Override |
| 253 | + | public void flush() throws IOException { |
| 254 | + | if (delegate != null) |
| 255 | + | delegate.flush(); |
| 256 | + | } |
| 257 | + | |
| 258 | + | @Override |
| 259 | + | public void close() throws IOException { |
| 260 | + | if (delegate != null) |
| 261 | + | delegate.close(); |
| 262 | + | } |
| 263 | + | |
| 264 | + | @Override |
| 265 | + | public void write(int b) throws IOException { |
| 266 | + | lastActiveTime.set(System.currentTimeMillis()); |
| 267 | + | if (delegate != null) |
| 268 | + | delegate.write(b); |
| 269 | + | } |
| 270 | + | |
| 271 | + | @Override |
| 272 | + | public void write(byte[] b) throws IOException { |
| 273 | + | lastActiveTime.set(System.currentTimeMillis()); |
| 274 | + | if (delegate != null) |
| 275 | + | delegate.write(b); |
| 276 | + | } |
| 277 | + | |
| 278 | + | @Override |
| 279 | + | public void write(byte[] b, int off, int len) throws IOException { |
| 280 | + | lastActiveTime.set(System.currentTimeMillis()); |
| 281 | + | if (delegate != null) |
| 282 | + | delegate.write(b, off, len); |
| 283 | + | } |
| 284 | + | |
| 285 | + | }; |
| 286 | + | |
| 287 | + | ProcessStreamPumper streamPumper = new ProcessStreamPumper(process, |
| 288 | + | new OutputStreamWrapper(stdout), new OutputStreamWrapper(stderr), stdin); |
| 289 | + | |
243 | 290 | | Thread thread = Thread.currentThread(); |
244 | 291 | | AtomicBoolean stoppedRef = new AtomicBoolean(false); |
245 | | - | long time = System.currentTimeMillis(); |
246 | 292 | | EXECUTOR_SERVICE.execute(new Runnable() { |
247 | 293 | | |
248 | 294 | | @Override |
249 | 295 | | public void run() { |
250 | 296 | | while (!stoppedRef.get()) { |
251 | | - | if (System.currentTimeMillis() - time > timeout*1000L) { |
| 297 | + | if (System.currentTimeMillis() - lastActiveTime.get() > timeout*1000L) { |
252 | 298 | | thread.interrupt(); |
253 | 299 | | break; |
254 | 300 | | } else { |
| skipped 10 lines |
265 | 311 | | result.setReturnCode(process.waitFor()); |
266 | 312 | | } catch (InterruptedException e) { |
267 | 313 | | processKiller.kill(process, executionId); |
268 | | - | if (System.currentTimeMillis() - time > timeout*1000L) |
| 314 | + | if (System.currentTimeMillis() - lastActiveTime.get() > timeout*1000L) |
269 | 315 | | throw new RuntimeException(new TimeoutException()); |
270 | 316 | | else |
271 | 317 | | throw new RuntimeException(e); |
| skipped 2 lines |
274 | 320 | | streamPumper.waitFor(); |
275 | 321 | | } |
276 | 322 | | } else { |
| 323 | + | ProcessStreamPumper streamPumper = new ProcessStreamPumper(process, stdout, stderr, stdin); |
277 | 324 | | try { |
278 | 325 | | result.setReturnCode(process.waitFor()); |
279 | 326 | | } catch (InterruptedException e) { |
| skipped 10 lines |