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

Add docs for execution order of listeners #1610

Closed
Ronserruya opened this issue Jun 23, 2019 · 4 comments
Closed

Add docs for execution order of listeners #1610

Ronserruya opened this issue Jun 23, 2019 · 4 comments

Comments

@Ronserruya
Copy link

I noticed an undocumented behavior with listeners, where some of them run in reverse order of registration

  1. What is the rationale for that?
  2. Can this be added to the docs?

Additional context
Relevant lines of code from https://github.com/huge-success/sanic/blob/master/sanic/app.py#L1356

 # -------------------------------------------- #
        # Register start/stop events
        # -------------------------------------------- #

        for event_name, settings_name, reverse in (
            ("before_server_start", "before_start", False),
            ("after_server_start", "after_start", False),
            ("before_server_stop", "before_stop", True),
            ("after_server_stop", "after_stop", True),
        ):
            listeners = self.listeners[event_name].copy()
            if reverse:
                listeners.reverse()
            # Prepend sanic to the arguments when listeners are triggered
            listeners = [partial(listener, self) for listener in listeners]
            server_settings[settings_name] = listeners
@stale
Copy link

stale bot commented Sep 21, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is incorrect, please respond with an update. Thank you for your contributions.

@stale stale bot added the stale label Sep 21, 2019
@Tronic
Copy link
Member

Tronic commented Sep 22, 2019

This convention comes from Python with statements, C++ RAII and similar constructions of scope bound resource management. The rule is that you should deconstruct in exact reverse order of construction.

For instance, if your first before_server_start handler setups a database connection, ones registered after it can rely on that connection being alive both when they are started and stopped, because stopping is done in reverse order, and the database connection is torn down last.

Perhaps it should be documented. Pull requests are welcome. Look at docs/sanic/

@stale stale bot removed the stale label Sep 22, 2019
@stale
Copy link

stale bot commented Dec 21, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is incorrect, please respond with an update. Thank you for your contributions.

@tomaszdrozdz
Copy link
Contributor

@ahopkins @Tronic Can we close this issue ?

@ahopkins ahopkins closed this as completed Oct 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants