Moves the server from using run_app api to using TCPSite
This commit is contained in:
parent
d89bc509e7
commit
30812b2479
1 changed files with 61 additions and 36 deletions
99
src/app.py
99
src/app.py
|
@ -2,6 +2,7 @@
|
||||||
import logging
|
import logging
|
||||||
import pathlib
|
import pathlib
|
||||||
import re
|
import re
|
||||||
|
import signal
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import aiojobs
|
import aiojobs
|
||||||
|
@ -40,8 +41,7 @@ async def database_backup_routine(app):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
# noinspection PyDeprecation
|
async def start_background_tasks(app):
|
||||||
async def start_background_tasks(app: web.Application):
|
|
||||||
app['reader'] = obtain_connection(app['db_path'], True)
|
app['reader'] = obtain_connection(app['db_path'], True)
|
||||||
app['waitful_backup'] = app.loop.create_task(database_backup_routine(app))
|
app['waitful_backup'] = app.loop.create_task(database_backup_routine(app))
|
||||||
app['comment_scheduler'] = await aiojobs.create_scheduler(limit=1, pending_limit=0)
|
app['comment_scheduler'] = await aiojobs.create_scheduler(limit=1, pending_limit=0)
|
||||||
|
@ -49,6 +49,50 @@ async def start_background_tasks(app: web.Application):
|
||||||
app['writer'] = app['db_writer'].connection
|
app['writer'] = app['db_writer'].connection
|
||||||
|
|
||||||
|
|
||||||
|
async def stop_background_tasks(app):
|
||||||
|
logger.info('Ending background backup loop')
|
||||||
|
app['waitful_backup'].cancel()
|
||||||
|
await app['waitful_backup']
|
||||||
|
app['reader'].close()
|
||||||
|
app['writer'].close()
|
||||||
|
|
||||||
|
|
||||||
|
class CommentDaemon:
|
||||||
|
def __init__(self, config, db_path=None, **kwargs):
|
||||||
|
self.config = config
|
||||||
|
app = web.Application()
|
||||||
|
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(stop_background_tasks)
|
||||||
|
app.on_shutdown.append(close_comment_scheduler)
|
||||||
|
aiojobs.aiohttp.setup(app, **kwargs)
|
||||||
|
app.add_routes([
|
||||||
|
web.post('/api', api_endpoint),
|
||||||
|
web.get('/', get_api_endpoint),
|
||||||
|
web.get('/api', get_api_endpoint)
|
||||||
|
])
|
||||||
|
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):
|
def insert_to_config(app, conf=None, db_file=None):
|
||||||
db_file = db_file if db_file else 'DEFAULT'
|
db_file = db_file if db_file else 'DEFAULT'
|
||||||
app['config'] = conf
|
app['config'] = conf
|
||||||
|
@ -57,41 +101,22 @@ def insert_to_config(app, conf=None, db_file=None):
|
||||||
assert app['db_path'] != app['backup']
|
assert app['db_path'] != app['backup']
|
||||||
|
|
||||||
|
|
||||||
async def cleanup_background_tasks(app):
|
|
||||||
logger.info('Ending background backup loop')
|
|
||||||
app['waitful_backup'].cancel()
|
|
||||||
await app['waitful_backup']
|
|
||||||
app['reader'].close()
|
|
||||||
app['writer'].close()
|
|
||||||
|
|
||||||
|
|
||||||
def create_app(conf, db_path='DEFAULT', **kwargs):
|
|
||||||
app = web.Application()
|
|
||||||
app['START_TIME'] = int(time.time())
|
|
||||||
insert_to_config(app, conf, 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(close_comment_scheduler)
|
|
||||||
aiojobs.aiohttp.setup(app, **kwargs)
|
|
||||||
app.add_routes([
|
|
||||||
web.post('/api', api_endpoint),
|
|
||||||
web.get('/', get_api_endpoint),
|
|
||||||
web.get('/api', get_api_endpoint)
|
|
||||||
])
|
|
||||||
return app
|
|
||||||
|
|
||||||
|
|
||||||
def run_app(config):
|
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:
|
try:
|
||||||
asyncio.run(web.run_app(
|
loop.run_until_complete(comment_app.start())
|
||||||
app=appl,
|
loop.run_forever()
|
||||||
access_log=logging.getLogger('aiohttp.access'),
|
except (web.GracefulExit, KeyboardInterrupt, asyncio.CancelledError, ValueError):
|
||||||
host=config['HOST'],
|
|
||||||
port=config['PORT']
|
|
||||||
))
|
|
||||||
except asyncio.CancelledError:
|
|
||||||
logging.warning('Server going down, asyncio loop raised cancelled error:')
|
logging.warning('Server going down, asyncio loop raised cancelled error:')
|
||||||
except ValueError:
|
finally:
|
||||||
logging.exception('Server going down due to value error:')
|
loop.run_until_complete(comment_app.stop())
|
Loading…
Reference in a new issue