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

Listeners for main server process #2018

Merged
merged 9 commits into from
Mar 1, 2021
Merged

Listeners for main server process #2018

merged 9 commits into from
Mar 1, 2021

Conversation

ahopkins
Copy link
Member

@ahopkins ahopkins commented Feb 1, 2021

Stems from discussion in #2015.

This is a POC for adding listeners that only are triggered in the main server process. There is no testing yet, and I have not done any edge cases (ASGI should throw error).

Eventually, this should be replaced by #1630.


@app.listener("main_process_start")
async def process_start(app, loop):
    print("process_start")

@app.listener("main_process_stop")
async def process_stop(app, loop):
    print("process_stop")

Single server

[2021-02-02 00:51:12 +0200] [566407] [INFO] Goin' Fast @ http://127.0.0.1:9999
process_start
[2021-02-02 00:51:12 +0200] [566407] [INFO] Starting worker [566407]
^C[2021-02-02 00:51:12 +0200] [566407] [INFO] Stopping worker [566407]
process_stop
[2021-02-02 00:51:12 +0200] [566407] [INFO] Server Stopped

Multiple servers

[2021-02-02 00:51:18 +0200] [566483] [INFO] Goin' Fast @ http://127.0.0.1:9999
process_start
[2021-02-02 00:51:18 +0200] [566484] [INFO] Starting worker [566484]
[2021-02-02 00:51:18 +0200] [566485] [INFO] Starting worker [566485]
^C^C[2021-02-02 00:51:22 +0200] [566483] [INFO] Received signal SIGTERM. Shutting down.
[2021-02-02 00:51:22 +0200] [566483] [INFO] Received signal SIGINT. Shutting down.
[2021-02-02 00:51:22 +0200] [566485] [INFO] Stopping worker [566485]
[2021-02-02 00:51:22 +0200] [566484] [INFO] Stopping worker [566484]
process_stop
[2021-02-02 00:51:22 +0200] [566483] [INFO] Server Stopped

@ahopkins ahopkins marked this pull request as draft February 1, 2021 22:59
@ahopkins ahopkins marked this pull request as ready for review February 21, 2021 14:49
@codecov
Copy link

codecov bot commented Feb 21, 2021

Codecov Report

Merging #2018 (a712586) into master (170177f) will increase coverage by 0.036%.
The diff coverage is 96.552%.

Impacted file tree graph

@@              Coverage Diff              @@
##            master     #2018       +/-   ##
=============================================
+ Coverage   91.719%   91.754%   +0.036%     
=============================================
  Files           35        35               
  Lines         3200      3226       +26     
  Branches       555       557        +2     
=============================================
+ Hits          2935      2960       +25     
  Misses         181       181               
- Partials        84        85        +1     
Impacted Files Coverage Δ
sanic/server.py 87.958% <94.737%> (+0.287%) ⬆️
sanic/app.py 91.793% <100.000%> (+0.072%) ⬆️
sanic/mixins/listeners.py 100.000% <100.000%> (ø)
sanic/worker.py 84.800% <100.000%> (+0.374%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 170177f...a712586. Read the comment docs.

Copy link
Contributor

@sjsadowski sjsadowski left a comment

Choose a reason for hiding this comment

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

Approved.

@@ -920,6 +921,13 @@ async def create_server(
server_settings.get("before_start", []),
server_settings.get("loop"),
)
main_start = server_settings.pop("main_start", None)
main_stop = server_settings.pop("main_stop", None)
if main_start or main_stop:
Copy link
Contributor

Choose a reason for hiding this comment

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

I appreciate this given the number of create_server issues that come up.

@ahopkins ahopkins merged commit 4b968dc into master Mar 1, 2021
@ahopkins ahopkins deleted the main-process-listeners branch March 1, 2021 13:03
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.

2 participants