Skip to content

Commit 9f2a9b8

Browse files
committed
BackgroundProcess#awaitSuccess() now returns Bool
1 parent 1f30f1a commit 9f2a9b8

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## [Unreleased]
1010

11+
### Added
12+
- BackgroundProcess#awaitSuccess() now returns a boolean indicating if the process finished or is still running
13+
14+
### Fixed
15+
- Fix "Uncaught exception Lock was aquired by another thread!"
16+
- Prevent random premature killing of external processes run via BackgroundProcess
17+
1118

1219
## [4.0.1] - 2023-01-17
1320

src/hx/concurrent/thread/BackgroundProcess.hx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ class BackgroundProcess {
138138
* 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.
139139
* If <code>timeoutMS</code> is set to `-1`, waits indefinitely until the process exists.
140140
* If <code>timeoutMS</code> is set to value lower than -1, results in an exception.
141+
*
142+
* @return the exit code or null
141143
*/
142144
public function awaitExit(timeoutMS:Int):Null<Int> {
143145
Threads.await(() -> exitCode != null, timeoutMS);
@@ -152,17 +154,21 @@ class BackgroundProcess {
152154
* If <code>timeoutMS</code> is set to `-1`, waits indefinitely until the process exists.
153155
* If <code>timeoutMS</code> is set to value lower than -1, results in an exception.
154156
*
157+
* @return `true` if process exited successful, `false` if process is still running
155158
* @throws if exitCode != 0
156159
*/
157-
public function awaitSuccess(timeoutMS:Int, includeStdErr = true):Void {
160+
public function awaitSuccess(timeoutMS:Int, includeStdErr = true):Bool {
158161
final exitCode = awaitExit(timeoutMS);
159162
if (exitCode == 0)
160-
return;
163+
return true;
164+
165+
if (exitCode == null)
166+
return false;
161167

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

165-
throw 'Process failed with exit code $exitCode';
171+
throw 'Process [cmd=$cmd,pid=$pid] failed with exit code $exitCode';
166172
}
167173

168174
/**

test/hx/concurrent/TestRunner.hx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,8 @@ class TestRunner extends hx.doctest.DocTestRunner {
417417
function testBackgroundProcess() {
418418
// cannot use timout command on Windows because of "ERROR: Input redirection is not supported, exiting the process immediately."
419419
final p = Sys.systemName() == "Windows" ?
420-
new BackgroundProcess("ping", ["127.0.0.1", "-n", 2, "-w", 1000]) :
421-
new BackgroundProcess("ping", ["127.0.0.1", "-c", 2, "-W", 1000]);
420+
new BackgroundProcess("ping", ["127.0.0.1", "-n", 2, "-w", 3000]) :
421+
new BackgroundProcess("ping", ["127.0.0.1", "-c", 2, "-W", 3000]);
422422

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

429429
Logger.log(INFO, "Awaiting exit of ping process...");
430-
p.awaitExit(5000);
430+
assertFalse(p.awaitSuccess(100));
431+
assertEquals(p.awaitExit(100), null);
432+
p.awaitExit(3000);
431433

432434
if(p.exitCode != 0)
433435
trace(p.stderr.readAll());

0 commit comments

Comments
 (0)