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:
Raphael Deem 2017-01-21 23:09:12 -08:00 committed by GitHub
commit afe390d407
3 changed files with 48 additions and 5 deletions

View File

@ -276,8 +276,7 @@ class Sanic:
def run(self, host="127.0.0.1", port=8000, debug=False, before_start=None, 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, after_start=None, before_stop=None, after_stop=None, ssl=None,
sock=None, workers=1, loop=None, protocol=HttpProtocol, 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 Runs the HTTP Server and listens until keyboard interrupt or term
signal. On termination, drains connections before closing. signal. On termination, drains connections before closing.
@ -317,6 +316,7 @@ class Sanic:
'request_timeout': self.config.REQUEST_TIMEOUT, 'request_timeout': self.config.REQUEST_TIMEOUT,
'request_max_size': self.config.REQUEST_MAX_SIZE, 'request_max_size': self.config.REQUEST_MAX_SIZE,
'loop': loop, 'loop': loop,
'register_sys_signals': register_sys_signals,
'backlog': backlog 'backlog': backlog
} }

View File

@ -261,7 +261,8 @@ def trigger_events(events, loop):
def serve(host, port, request_handler, error_handler, before_start=None, def serve(host, port, request_handler, error_handler, before_start=None,
after_start=None, before_stop=None, after_stop=None, debug=False, after_start=None, before_stop=None, after_stop=None, debug=False,
request_timeout=60, ssl=None, sock=None, request_max_size=None, 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. 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) trigger_events(after_start, loop)
# Register signals for graceful termination # Register signals for graceful termination
for _signal in (SIGINT, SIGTERM): if register_sys_signals:
loop.add_signal_handler(_signal, loop.stop) for _signal in (SIGINT, SIGTERM):
loop.add_signal_handler(_signal, loop.stop)
try: try:
loop.run_forever() loop.run_forever()

41
tests/test_sanic.py Normal file
View 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