Skip to content

Commit

Permalink
BackgroundProcess#awaitSuccess() now returns Bool
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Jan 20, 2023
1 parent 1f30f1a commit 9f2a9b8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- BackgroundProcess#awaitSuccess() now returns a boolean indicating if the process finished or is still running

### Fixed
- Fix "Uncaught exception Lock was aquired by another thread!"
- Prevent random premature killing of external processes run via BackgroundProcess


## [4.0.1] - 2023-01-17

Expand Down
14 changes: 10 additions & 4 deletions src/hx/concurrent/thread/BackgroundProcess.hx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ class BackgroundProcess {
* If <code>timeoutMS</code> is set to value > 0, waits up to the given timespan for the process exists and returns either null or the exit code.
* If <code>timeoutMS</code> is set to `-1`, waits indefinitely until the process exists.
* If <code>timeoutMS</code> is set to value lower than -1, results in an exception.
*
* @return the exit code or null
*/
public function awaitExit(timeoutMS:Int):Null<Int> {
Threads.await(() -> exitCode != null, timeoutMS);
Expand All @@ -152,17 +154,21 @@ class BackgroundProcess {
* If <code>timeoutMS</code> is set to `-1`, waits indefinitely until the process exists.
* If <code>timeoutMS</code> is set to value lower than -1, results in an exception.
*
* @return `true` if process exited successful, `false` if process is still running
* @throws if exitCode != 0
*/
public function awaitSuccess(timeoutMS:Int, includeStdErr = true):Void {
public function awaitSuccess(timeoutMS:Int, includeStdErr = true):Bool {
final exitCode = awaitExit(timeoutMS);
if (exitCode == 0)
return;
return true;

if (exitCode == null)
return false;

if (includeStdErr)
throw 'Process failed with exit code $exitCode and error message: ${stderr.readAll()}';
throw 'Process [cmd=$cmd,pid=$pid] failed with exit code $exitCode and error message: ${stderr.readAll()}';

throw 'Process failed with exit code $exitCode';
throw 'Process [cmd=$cmd,pid=$pid] failed with exit code $exitCode';
}

/**
Expand Down
8 changes: 5 additions & 3 deletions test/hx/concurrent/TestRunner.hx
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,8 @@ class TestRunner extends hx.doctest.DocTestRunner {
function testBackgroundProcess() {
// cannot use timout command on Windows because of "ERROR: Input redirection is not supported, exiting the process immediately."
final p = Sys.systemName() == "Windows" ?
new BackgroundProcess("ping", ["127.0.0.1", "-n", 2, "-w", 1000]) :
new BackgroundProcess("ping", ["127.0.0.1", "-c", 2, "-W", 1000]);
new BackgroundProcess("ping", ["127.0.0.1", "-n", 2, "-w", 3000]) :
new BackgroundProcess("ping", ["127.0.0.1", "-c", 2, "-W", 3000]);

assertEquals(p.exitCode, null);
assertTrue(p.isRunning);
Expand All @@ -427,7 +427,9 @@ class TestRunner extends hx.doctest.DocTestRunner {
#end

Logger.log(INFO, "Awaiting exit of ping process...");
p.awaitExit(5000);
assertFalse(p.awaitSuccess(100));
assertEquals(p.awaitExit(100), null);
p.awaitExit(3000);

if(p.exitCode != 0)
trace(p.stderr.readAll());
Expand Down

0 comments on commit 9f2a9b8

Please sign in to comment.