Skip to content

Python: add status updates during automatic provisioning - #5522

Merged
helderco merged 2 commits into
dagger:mainfrom
helderco:sdk/python/provisioning-status-updates
Jul 31, 2023
Merged

helderco merged 2 commits into
dagger:mainfrom
helderco:sdk/python/provisioning-status-updates

Conversation

@helderco

@helderco helderco commented Jul 28, 2023 •

Copy link
Copy Markdown
Contributor

This is the Python implementation for:

It takes a slightly different approach than Go and Node.js in:

Instead of Message... OK! lines written only to log_output, this change is less obtrusive, more animated and works even without log_output1:

  • Shows an animation which gives a better experience than a static "…", and replaces status updates in-line.
  • In the end this line disappears so there’s no lingering record in the logs. It's ok because the intent is to provide interactive feedback, without polluting logs.
  • Writes to log_output if it’s interactive (e.g., pseudo terminal, not a file).
  • Supports an escape hatch if you need to turn this off (independently of log_output).
  • Without log_output it defaults to stderr and makes a last update of “Running pipelines” when the provisioning has finished. This doesn’t show on log_output because the engine logs already provide progress feedback.
  • Only activates when automatic provisioning kicks in, so it’s disabled inside a running session like dagger run so that the TUI (progrock) can take over.

I've tested this under multiple conditions:

  • Empty dagger.Connection()
  • With log_output=sys.stderr
  • With log_output to write to a file (non-interactive)
  • All of the above, wrapped in dagger run
  • All of the above with escape hatch to disable

👇 I'm providing a sample app to easily test these multiple conditions.

Testing app Screenshot 2023-07-28 at 17 37 29

You'll need to pip install typer and save this code in a progress.py file:

# ruff: noqa: FBT002, UP007, T201
import contextlib
import pathlib
import sys
from typing import Annotated, Optional

import anyio
import typer

import dagger

app = typer.Typer()
q_opt = typer.Option("-q", help="Quiet console (i.e., no provisioning status updates)")
l_opt = typer.Option("-l", help="Add log_output")
f_opt = typer.Option(
    "-f",
    help="File for log_output (defaults to stderr)",
    file_okay=True,
    writable=True,
    resolve_path=True,
    show_default=False,
)


@app.command()
def entrypoint(
    quiet: Annotated[bool, q_opt] = False,
    log_output: Annotated[bool, l_opt] = False,
    path: Annotated[Optional[pathlib.Path], f_opt] = None,
):
    if path:
        if not log_output:
            msg = "Cannot use -f without -l"
            raise typer.BadParameter(msg)
        cm = contextlib.closing(path.open("w"))
    else:
        cm = contextlib.nullcontext(sys.stderr if log_output else None)

    with cm as file:
        cfg = dagger.Config(log_output=file)
        if quiet:
            cfg.console.quiet = True
        anyio.run(main, cfg)


async def main(cfg: dagger.Config):
    async with dagger.Connection(cfg) as client:
        print(
            await (
                client.container()
                .from_("python:3.11.1-alpine")
                .with_exec(["sleep", "3"])
                .with_exec(["python", "-V"])
                .stdout()
            )
        )


if __name__ == "__main__":
    app()

dagger.Connection()

default

dagger.Connection(log_output=sys.stderr)

Notice the status disappears and there's no "Running pipelines" when engine logs start.

stderr

dagger run

dagger-run

Footnotes

  1. Not that the same can’t be done in the other SDKs, but the library that does this was already a dependency, it was simple to implement and I already experimented with it very early on for this purpose (see https://github.com/dagger/dagger/issues/4186). ↩

@helderco
helderco force-pushed the sdk/python/provisioning-status-updates branch from f5d418b to 8237b17 Compare July 28, 2023 18:29
@helderco
helderco marked this pull request as ready for review July 28, 2023 18:29
@helderco helderco assigned grouville and jpadams and unassigned jpadams and grouville Jul 28, 2023
@helderco
helderco requested review from grouville and jpadams July 28, 2023 22:37
@helderco
helderco requested a review from gerhard July 31, 2023 12:02

@grouville grouville left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice ! ✅

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

Successfully merging this pull request may close these issues.

4 participants