Skip to content
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

Support capturing output for async commands #8

Open
joe-mcgovern opened this issue Apr 26, 2022 · 5 comments
Open

Support capturing output for async commands #8

joe-mcgovern opened this issue Apr 26, 2022 · 5 comments

Comments

@joe-mcgovern
Copy link

joe-mcgovern commented Apr 26, 2022

According to the docs, only work for non-captured command blocks. It would be really handy to support captured command blocks. Something like:

let handle = &${ echo Hello world! }

# Do some other work before calling join.
# This may be printed before or after "Hello world!".
std.print("Doing some work...")

# This will wait until the block runs to completion, and will return it's result.
let result = handle.join()

std.assert(std.type(result) != "error")
std.assert(result.stdout == "Hello world!")
@gahag
Copy link
Collaborator

gahag commented Apr 26, 2022

Fair enough. It is a very specific use case, but a valid one nonetheless. As I have some higher priority tasks for the near future, please feel free to submit a pull request :)

@bew
Copy link

bew commented Apr 26, 2022

note: I think you have a typo on the last assert:

-std.assert(std.stdout == "Hello world!")
+std.assert(result.stdout == "Hello world!")

@joe-mcgovern
Copy link
Author

^ good catch; I will update that.

@Jackman3005
Copy link

To be honest, I think it would be really valuable to be able to get the stdout of async commands before you join them... This would allow you to essentially stream shell commands through hush...

This would be particularly useful if you are starting a server asynchronously, you can never wait for the join, because it will be waiting forever. Instead, you can monitor the stdout stream and decide that it started successfully with a matching pattern you are looking for, then move on, etc.

You could stream logs from a server and filter or format messages in hush on the way out.

I recognize this is likely more challenging than just waiting for the async command to finish and then respond with the stdout string, but I think it's a much more powerful and re-usable approach.

@bsdelf
Copy link

bsdelf commented Sep 5, 2024

To be honest, I think it would be really valuable to be able to get the stdout of async commands before you join them...

this feature is well supported in nodejs, see code examples:

basically the nodejs approach relies on

  • data steaming
  • event emitter
  • single thread + event loop

suppose hush use similar approach, I can imagine the API usage would be

let output = ""

let onExit = function(code)
    std.print(code)
end

let onData = function(data)
    output = output ++ data
end

let flushData = function()
    std.print(output)
end

let handle = &{ echo Hello world! }
handle.pid # process id
handle.stdout # process standard output stream
handle.on("exit", onExit) # register process exit handler
handle.stdout.on("data", onData) # register data chunk handler
handle.stdout.on("end", flushData) # register end of data handler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants