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

Provide a way to stream output of services #185

Open
mboes opened this issue Jan 28, 2024 · 5 comments
Open

Provide a way to stream output of services #185

mboes opened this issue Jan 28, 2024 · 5 comments

Comments

@mboes
Copy link

mboes commented Jan 28, 2024

Say I run a service called tickle that immediately prints an error but does not terminate. If I do so using nix .#start-services start, then all I see is:

$ nix run '.#start-services' start
21:03:16 system   | tickle.1 started (pid=415992)

I don't see the error message until the service stops. If it doesn't stop unless I SIGTERM on the terminal, then the output is lost.

@mboes
Copy link
Author

mboes commented Jan 28, 2024

I'm not sure what I did different, but I now see the error log. Closing for now. Will reopen if I can reproduce.

@mboes mboes closed this as completed Jan 28, 2024
@mboes mboes changed the title Provide a way to see output of services Provide a way to stream output of services Jan 28, 2024
@mboes
Copy link
Author

mboes commented Jan 28, 2024

Reopening. It's possible to see the output of a service, but only of the service exits. So long as that hasn't happened, then the output is not available.

@mboes mboes reopened this Jan 28, 2024
@thufschmitt
Copy link
Member

I can't reproduce. I've tried with the project file below, but the output comes properly streamed (both for stdin and stderr):

let inputs = import "./nickel.lock.ncl" in
let organist = inputs.organist in

{
  shells = organist.shells.Bash,

  services.tickle =
    let start_script =
        {
          name = "start-tickle",
          content.text = nix-s%"
            echo "Starting tickle"
            echo "Starting tickle -- stderr" >&2

            cat
        "%
        } | organist.nix.builders.ShellApplication
      in
      nix-s%"%{start_script}/bin/start-tickle"%,

}
  | organist.OrganistExpression
  & organist.services.Schema

Can you share the service that you were running?

@mboes
Copy link
Author

mboes commented Feb 16, 2024

Simple python code that retries connecting to a (non-existent) server:

import requests
import time

def send_request(url):
    url = f"{url}/tickle"
    try:
        response = requests.post(url)
        print("Response Status Code:", response.status_code)
    except requests.exceptions.ConnectionError as e:
        print(e)

def main():
    while True:
        send_request("https://localhost:5000")
        time.sleep(60)

if __name__ == "__main__":
    session = requests.Session()
    session.verify = False
    main()

When run on the command line using poetry run python src/tickle.py, the exception is printed on the terminal every minute. When run through nix run .#start-services start, I see a message saying the service started (as in the description of this issue) and that's it.

@jmou
Copy link

jmou commented May 1, 2024

Hi I just saw this while driving by. Do you think the issue might be output buffering? I think Python line buffers when attached to a terminal but otherwise aggressively buffers stdout and stderr. Could be worth trying python -u or setting PYTHONUNBUFFERED.

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

3 participants