Merge pull request #253 from dutradda/add_register_sys_signals_flag
add a flag to skip SIGINT and SIGTERM signals registration
This commit is contained in:
commit
afe390d407
|
@ -276,8 +276,7 @@ class Sanic:
|
|||
def run(self, host="127.0.0.1", port=8000, debug=False, before_start=None,
|
||||
after_start=None, before_stop=None, after_stop=None, ssl=None,
|
||||
sock=None, workers=1, loop=None, protocol=HttpProtocol,
|
||||
backlog=100, stop_event=None):
|
||||
|
||||
backlog=100, stop_event=None, register_sys_signals=True):
|
||||
"""
|
||||
Runs the HTTP Server and listens until keyboard interrupt or term
|
||||
signal. On termination, drains connections before closing.
|
||||
|
@ -317,6 +316,7 @@ class Sanic:
|
|||
'request_timeout': self.config.REQUEST_TIMEOUT,
|
||||
'request_max_size': self.config.REQUEST_MAX_SIZE,
|
||||
'loop': loop,
|
||||
'register_sys_signals': register_sys_signals,
|
||||
'backlog': backlog
|
||||
}
|
||||
|
||||
|
|
|
@ -261,7 +261,8 @@ def trigger_events(events, loop):
|
|||
def serve(host, port, request_handler, error_handler, before_start=None,
|
||||
after_start=None, before_stop=None, after_stop=None, debug=False,
|
||||
request_timeout=60, ssl=None, sock=None, request_max_size=None,
|
||||
reuse_port=False, loop=None, protocol=HttpProtocol, backlog=100):
|
||||
reuse_port=False, loop=None, protocol=HttpProtocol, backlog=100,
|
||||
register_sys_signals=True):
|
||||
"""
|
||||
Starts asynchronous HTTP Server on an individual process.
|
||||
|
||||
|
@ -333,8 +334,9 @@ def serve(host, port, request_handler, error_handler, before_start=None,
|
|||
trigger_events(after_start, loop)
|
||||
|
||||
# Register signals for graceful termination
|
||||
for _signal in (SIGINT, SIGTERM):
|
||||
loop.add_signal_handler(_signal, loop.stop)
|
||||
if register_sys_signals:
|
||||
for _signal in (SIGINT, SIGTERM):
|
||||
loop.add_signal_handler(_signal, loop.stop)
|
||||
|
||||
try:
|
||||
loop.run_forever()
|
||||
|
|
41
tests/test_sanic.py
Normal file
41
tests/test_sanic.py
Normal file
|
@ -0,0 +1,41 @@
|
|||
from sanic import Sanic
|
||||
from sanic.response import HTTPResponse
|
||||
from sanic.utils import HOST, PORT
|
||||
from unittest.mock import MagicMock
|
||||
import pytest
|
||||
import asyncio
|
||||
|
||||
|
||||
async def stop(app):
|
||||
await asyncio.sleep(0.2)
|
||||
app.stop()
|
||||
|
||||
|
||||
def test_register_system_signals():
|
||||
"""Test if sanic register system signals"""
|
||||
app = Sanic('test_register_system_signals')
|
||||
|
||||
@app.route('/hello')
|
||||
async def hello_route(request):
|
||||
return HTTPResponse()
|
||||
|
||||
loop = asyncio.new_event_loop()
|
||||
loop.add_signal_handler = MagicMock()
|
||||
asyncio.ensure_future(stop(app), loop=loop)
|
||||
app.run(HOST, PORT, loop=loop)
|
||||
assert loop.add_signal_handler.called == True
|
||||
|
||||
|
||||
def test_dont_register_system_signals():
|
||||
"""Test if sanic don't register system signals"""
|
||||
app = Sanic('test_register_system_signals')
|
||||
|
||||
@app.route('/hello')
|
||||
async def hello_route(request):
|
||||
return HTTPResponse()
|
||||
|
||||
loop = asyncio.new_event_loop()
|
||||
loop.add_signal_handler = MagicMock()
|
||||
asyncio.ensure_future(stop(app), loop=loop)
|
||||
app.run(HOST, PORT, loop=loop, register_sys_signals=False)
|
||||
assert loop.add_signal_handler.called == False
|
Loading…
Reference in New Issue
Block a user