-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Taskrunner intermediate progress #3376
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b5a7e0c
WIP
kbirk 99101fc
Add progress to taskrunner
kbirk 3f8b9fd
Update taskrunner interface
kbirk 96f43ae
Merge branch 'main' into kbirk/task-progress
kbirk a92d69d
Merge branch 'main' into kbirk/task-progress
kbirk b66d543
Merge branch 'main' into kbirk/task-progress
kbirk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import lombok.Data; | ||
|
@@ -37,6 +38,7 @@ public class Task { | |
private Process process; | ||
private CompletableFuture<Integer> processFuture; | ||
private String inputPipeName; | ||
private String progressPipeName; | ||
private String outputPipeName; | ||
private TaskStatus status = TaskStatus.QUEUED; | ||
private ScopedLock lock = new ScopedLock(); | ||
|
@@ -52,6 +54,7 @@ public Task(final TaskRequest req) throws IOException, InterruptedException { | |
|
||
this.req = req; | ||
inputPipeName = "/tmp/input-" + req.getId(); | ||
progressPipeName = "/tmp/progress-" + req.getId(); | ||
outputPipeName = "/tmp/output-" + req.getId(); | ||
|
||
try { | ||
|
@@ -88,16 +91,23 @@ private void setup() throws IOException, InterruptedException { | |
processBuilder = new ProcessBuilder("python", req.getScript(), "--id", req.getId().toString(), | ||
"--input_pipe", | ||
inputPipeName, | ||
"--output_pipe", outputPipeName); | ||
"--output_pipe", | ||
outputPipeName, | ||
"--progress_pipe", | ||
progressPipeName); | ||
} else { | ||
// executable command, execute it directly | ||
processBuilder = new ProcessBuilder(req.getScript(), "--id", req.getId().toString(), | ||
"--input_pipe", | ||
inputPipeName, | ||
"--output_pipe", outputPipeName); | ||
"--output_pipe", | ||
outputPipeName, | ||
"--progress_pipe", | ||
progressPipeName); | ||
} | ||
|
||
log.debug("Creating input and output pipes: {} {} for task {}", inputPipeName, outputPipeName, req.getId()); | ||
log.info("Creating input, output, and progress pipes: {}, {}, {} for task {}", inputPipeName, outputPipeName, | ||
progressPipeName, req.getId()); | ||
|
||
// Create the named pipes | ||
final Process inputPipe = new ProcessBuilder("mkfifo", inputPipeName).start(); | ||
|
@@ -111,19 +121,25 @@ private void setup() throws IOException, InterruptedException { | |
if (exitCode != 0) { | ||
throw new RuntimeException("Error creating input pipe"); | ||
} | ||
|
||
final Process progressPipe = new ProcessBuilder("mkfifo", progressPipeName).start(); | ||
exitCode = progressPipe.waitFor(); | ||
if (exitCode != 0) { | ||
throw new RuntimeException("Error creating input pipe"); | ||
} | ||
} | ||
|
||
public void writeInputWithTimeout(final byte[] bytes, final int timeoutMinutes) | ||
throws IOException, InterruptedException, TimeoutException { | ||
log.debug("Dispatching write thread for input pipe: {} for task: {}", inputPipeName, req.getId()); | ||
log.info("Dispatching write thread for input pipe: {} for task: {}", inputPipeName, req.getId()); | ||
|
||
final CompletableFuture<Void> future = new CompletableFuture<>(); | ||
new Thread(() -> { | ||
try { | ||
// Write to the named pipe in a separate thread | ||
log.debug("Opening input pipe: {} for task: {}", inputPipeName, req.getId()); | ||
log.info("Opening input pipe: {} for task: {}", inputPipeName, req.getId()); | ||
try (FileOutputStream fos = new FileOutputStream(inputPipeName)) { | ||
log.debug("Writing to input pipe: {} for task: {}", inputPipeName, req.getId()); | ||
log.info("Writing to input pipe: {} for task: {}", inputPipeName, req.getId()); | ||
fos.write(bytes); | ||
} | ||
future.complete(null); | ||
|
@@ -158,30 +174,72 @@ public void writeInputWithTimeout(final byte[] bytes, final int timeoutMinutes) | |
|
||
public byte[] readOutputWithTimeout(final int timeoutMinutes) | ||
throws IOException, InterruptedException, ExecutionException, TimeoutException { | ||
log.debug("Dispatching read thread for input pipe: {} for task: {}", outputPipeName, req.getId()); | ||
log.info("Dispatching read thread for output pipe: {} for task: {}", outputPipeName, req.getId()); | ||
|
||
final CompletableFuture<byte[]> future = new CompletableFuture<>(); | ||
new Thread(() -> { | ||
log.debug("Opening output pipe: {} for task: {}", outputPipeName, req.getId()); | ||
log.info("Opening output pipe: {} for task: {}", outputPipeName, req.getId()); | ||
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(outputPipeName))) { | ||
log.debug("Reading from output pipe: {} for task: {}", outputPipeName, req.getId()); | ||
log.info("Reading from output pipe: {} for task: {}", outputPipeName, req.getId()); | ||
final ByteArrayOutputStream bos = new ByteArrayOutputStream(); | ||
final byte[] buffer = new byte[BYTES_PER_READ]; // buffer size | ||
int bytesRead; | ||
while ((bytesRead = bis.read(buffer)) != -1) { | ||
log.debug("Read {} bytes from output pipe: {} for task: {}", bytesRead, outputPipeName, | ||
log.info("Read {} bytes from output pipe: {} for task: {}", bytesRead, outputPipeName, | ||
req.getId()); | ||
bos.write(buffer, 0, bytesRead); | ||
} | ||
future.complete(bos.toByteArray()); | ||
} catch (final IOException e) { | ||
future.completeExceptionally(e); | ||
} | ||
}).start(); | ||
|
||
Object result; | ||
try { | ||
result = CompletableFuture.anyOf(future, processFuture).get(timeoutMinutes, TimeUnit.MINUTES); | ||
} catch (final TimeoutException e) { | ||
future.cancel(true); | ||
throw new TimeoutException("Reading from pipe took too long for task " + req.getId()); | ||
} | ||
|
||
try (BufferedReader reader = new BufferedReader( | ||
new InputStreamReader(new FileInputStream(outputPipeName)))) { | ||
log.debug("Reading on output pipe: {} for task {}", outputPipeName, req.getId()); | ||
future.complete(reader.readLine().getBytes()); | ||
if (result == null) { | ||
throw new RuntimeException("Unexpected null result for task " + req.getId()); | ||
} | ||
|
||
if (result instanceof byte[]) { | ||
// we got our response | ||
return (byte[]) result; | ||
} | ||
if (result instanceof Integer) { | ||
// process has exited early | ||
if (getStatus() == TaskStatus.CANCELLED) { | ||
throw new InterruptedException("Process for task " + req.getId() + " has been cancelled"); | ||
} | ||
throw new InterruptedException("Process for task " + req.getId() + " exited early with code " + result); | ||
} | ||
|
||
throw new RuntimeException("Unexpected result type: " + result.getClass()); | ||
} | ||
|
||
public byte[] readProgressWithTimeout(final int timeoutMinutes) | ||
throws IOException, InterruptedException, ExecutionException, TimeoutException { | ||
log.info("Dispatching read thread for progress pipe: {} for task: {}", progressPipeName, req.getId()); | ||
|
||
final CompletableFuture<byte[]> future = new CompletableFuture<>(); | ||
new Thread(() -> { | ||
log.info("Opening progress pipe: {} for task: {}", progressPipeName, req.getId()); | ||
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(progressPipeName))) { | ||
log.info("Reading from progress pipe: {} for task: {}", progressPipeName, req.getId()); | ||
final ByteArrayOutputStream bos = new ByteArrayOutputStream(); | ||
final byte[] buffer = new byte[BYTES_PER_READ]; // buffer size | ||
int bytesRead; | ||
while ((bytesRead = bis.read(buffer)) != -1) { | ||
log.info("Read {} bytes from progress pipe: {} for task: {}", bytesRead, progressPipeName, | ||
req.getId()); | ||
bos.write(buffer, 0, bytesRead); | ||
} | ||
future.complete(bos.toByteArray()); | ||
} catch (final IOException e) { | ||
future.completeExceptionally(e); | ||
} | ||
|
@@ -201,6 +259,15 @@ public byte[] readOutputWithTimeout(final int timeoutMinutes) | |
|
||
if (result instanceof byte[]) { | ||
// we got our response | ||
try { | ||
final JsonNode progress = mapper.readTree((byte[]) result); | ||
if (progress.has("done")) { | ||
// finished reading progress | ||
return null; | ||
} | ||
} catch (final Exception e) { | ||
// do nothing | ||
} | ||
Comment on lines
+268
to
+270
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why doing a catch if we do nothing? To not stop the thread? |
||
return (byte[]) result; | ||
} | ||
if (result instanceof Integer) { | ||
|
@@ -227,6 +294,12 @@ public void cleanup() { | |
log.warn("Exception occurred while cleaning up the task output pipe:" + e); | ||
} | ||
|
||
try { | ||
Files.deleteIfExists(Paths.get(progressPipeName)); | ||
} catch (final Exception e) { | ||
log.warn("Exception occurred while cleaning up the task progress pipe:" + e); | ||
} | ||
|
||
try { | ||
cancel(); | ||
} catch (final Exception e) { | ||
|
@@ -263,7 +336,7 @@ public void start() throws IOException, InterruptedException { | |
processFuture = new CompletableFuture<>(); | ||
new Thread(() -> { | ||
try { | ||
log.debug("Begin waiting for process to exit for task {}"); | ||
log.info("Begin waiting for process to exit for task {}", req.getId()); | ||
final int exitCode = process.waitFor(); | ||
log.info("Process exited with code {} for task {}", exitCode, req.getId()); | ||
lock.lock(() -> { | ||
|
@@ -277,7 +350,7 @@ public void start() throws IOException, InterruptedException { | |
status = TaskStatus.SUCCESS; | ||
} | ||
}); | ||
log.debug("Finalized process status for task {}", exitCode, req.getId()); | ||
log.info("Finalized process status for task {}", exitCode, req.getId()); | ||
processFuture.complete(exitCode); | ||
} catch (final InterruptedException e) { | ||
log.warn("Process failed to exit cleanly for task {}: {}", req.getId(), e); | ||
|
@@ -349,7 +422,7 @@ public boolean flagAsCancelling() { | |
return lock.lock(() -> { | ||
if (status == TaskStatus.QUEUED) { | ||
// if we havaen't started yet, flag it as cancelled | ||
log.debug("Cancelled task {} before starting it", req.getId()); | ||
log.info("Cancelled task {} before starting it", req.getId()); | ||
status = TaskStatus.CANCELLED; | ||
return false; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be
Error creating progress pipe
here.