Skip to content

Conversation

@z0u
Copy link

@z0u z0u commented Jun 24, 2025

Describe your changes

This PR refactors the monolithic _run_app function into reusable components. The motivation is to decouple output management from core app lifecycle operations, to allow custom output implementations.

  • Modularized _run_app into three parts:
    • _run_app_init, which creates the RunningApp
    • _run_app_create, which calls _create_all_objects and _publish_app
    • _disconnect_on_error, which encapsulates error handling
  • Add lifecycle hooks (callbacks) to allow complete separation of OutputManager from _run_app
  • Preserved behavior of OutputManager

It's hard to tell from the patch, but the core of the function is now this:

@asynccontextmanager
async def _run_app(
    app: _App,
    *,
    client: Optional[_Client] = None,
    detach: bool = False,
    environment_name: Optional[str] = None,
) -> AsyncGenerator[_App, None]:
    grace = config['logs_timeout']

    async with _run_app_init(app, client, detach, environment_name, grace) as ctx:
        async with _disconnect_on_error(ctx.client, ctx.app_id):
            await _run_app_create(app, ctx)
            yield app

Next steps, to allow custom output management:

  1. Make _App._logs() public, and have it include metadata (such as the file descriptor and task state).
  2. Extract OutputManager from _run_app, by either:
    • Make _run_app_init, _disconnect_on_error, and _run_app_create public, so users can compose their own run_app variant, or
    • Add lifecycle hooks to the simplified function (shown above), and make an adaptor for OutputManager to use those

Unlike the current App.run() context manager, which doesn't yield until the containers have been build, this would allow the user to start tailing logs as soon as the RunningApp has been created.

Checklists

Compatibility checklist

No protobuf changes.


Release checklist

If you intend for this commit to trigger a full release to PyPI, please ensure that the following steps have been taken:

  • Version file (modal_version/__init__.py) has been updated with the next logical version
  • Changelog has been cleaned up and given an appropriate subhead

Changelog

* Three main parts: _run_app -> _run_app_init -> _run_app_create

* Errors handling encapsulated in _disconnect_on_error
@z0u
Copy link
Author

z0u commented Jun 24, 2025

Relates to this feature request in Slack and this PR against mi-ni

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.

1 participant