Moves the server from using run_app api to using TCPSite

This commit is contained in:
Oleg Silkin 2019-07-22 13:16:54 -04:00
parent d89bc509e7
commit 30812b2479

View file

@ -2,6 +2,7 @@
import logging
import pathlib
import re
import signal
import time
import aiojobs
@ -40,8 +41,7 @@ async def database_backup_routine(app):
pass
# noinspection PyDeprecation
async def start_background_tasks(app: web.Application):
async def start_background_tasks(app):
app['reader'] = obtain_connection(app['db_path'], True)
app['waitful_backup'] = app.loop.create_task(database_backup_routine(app))
app['comment_scheduler'] = await aiojobs.create_scheduler(limit=1, pending_limit=0)
@ -49,15 +49,7 @@ async def start_background_tasks(app: web.Application):
app['writer'] = app['db_writer'].connection
def insert_to_config(app, conf=None, db_file=None):
db_file = db_file if db_file else 'DEFAULT'
app['config'] = conf
app['db_path'] = conf['PATH'][db_file]
app['backup'] = re.sub(r'\.db$', '.backup.db', app['db_path'])
assert app['db_path'] != app['backup']
async def cleanup_background_tasks(app):
async def stop_background_tasks(app):
logger.info('Ending background backup loop')
app['waitful_backup'].cancel()
await app['waitful_backup']
@ -65,13 +57,14 @@ async def cleanup_background_tasks(app):
app['writer'].close()
def create_app(conf, db_path='DEFAULT', **kwargs):
class CommentDaemon:
def __init__(self, config, db_path=None, **kwargs):
self.config = config
app = web.Application()
app['START_TIME'] = int(time.time())
insert_to_config(app, conf, db_path)
self.insert_to_config(app, config, db_file=db_path)
app.on_startup.append(setup_db_schema)
app.on_startup.append(start_background_tasks)
app.on_shutdown.append(cleanup_background_tasks)
app.on_shutdown.append(stop_background_tasks)
app.on_shutdown.append(close_comment_scheduler)
aiojobs.aiohttp.setup(app, **kwargs)
app.add_routes([
@ -79,19 +72,51 @@ def create_app(conf, db_path='DEFAULT', **kwargs):
web.get('/', get_api_endpoint),
web.get('/api', get_api_endpoint)
])
return app
self.app = app
self.app_runner = web.AppRunner(app)
self.app_site = None
async def start(self):
self.app['START_TIME'] = time.time()
await self.app_runner.setup()
self.app_site = web.TCPSite(
runner=self.app_runner,
host=self.config['HOST'],
port=self.config['PORT'],
)
await self.app_site.start()
logger.info(f'Comment Server is running on {self.config["HOST"]}:{self.config["PORT"]}')
async def stop(self):
await self.app.shutdown()
await self.app.cleanup()
await self.app_runner.cleanup()
@staticmethod
def insert_to_config(app, conf=None, db_file=None):
db_file = db_file if db_file else 'DEFAULT'
app['config'] = conf
app['db_path'] = conf['PATH'][db_file]
app['backup'] = re.sub(r'\.db$', '.backup.db', app['db_path'])
assert app['db_path'] != app['backup']
def run_app(config):
appl = create_app(conf=config, db_path='DEFAULT', close_timeout=5.0)
comment_app = CommentDaemon(config=config, db_path='DEFAULT', close_timeout=5.0)
loop = asyncio.get_event_loop()
loop.set_debug(True)
def __exit():
raise web.GracefulExit()
loop.add_signal_handler(signal.SIGINT, __exit)
loop.add_signal_handler(signal.SIGTERM, __exit)
try:
asyncio.run(web.run_app(
app=appl,
access_log=logging.getLogger('aiohttp.access'),
host=config['HOST'],
port=config['PORT']
))
except asyncio.CancelledError:
loop.run_until_complete(comment_app.start())
loop.run_forever()
except (web.GracefulExit, KeyboardInterrupt, asyncio.CancelledError, ValueError):
logging.warning('Server going down, asyncio loop raised cancelled error:')
except ValueError:
logging.exception('Server going down due to value error:')
finally:
loop.run_until_complete(comment_app.stop())