Upgrades Server to Allow Production Deployment #2

Merged
osilkin98 merged 10 commits from server_upgrade into master 2019-07-24 08:24:25 +02:00
Showing only changes of commit 30812b2479 - Show all commits

View file

@ -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,15 +49,7 @@ async def start_background_tasks(app: web.Application):
app['writer'] = app['db_writer'].connection app['writer'] = app['db_writer'].connection
def insert_to_config(app, conf=None, db_file=None): async def stop_background_tasks(app):
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):
logger.info('Ending background backup loop') logger.info('Ending background backup loop')
app['waitful_backup'].cancel() app['waitful_backup'].cancel()
await app['waitful_backup'] await app['waitful_backup']
@ -65,13 +57,14 @@ async def cleanup_background_tasks(app):
app['writer'].close() 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 = web.Application()
app['START_TIME'] = int(time.time()) self.insert_to_config(app, config, db_file=db_path)
insert_to_config(app, conf, db_path)
app.on_startup.append(setup_db_schema) app.on_startup.append(setup_db_schema)
app.on_startup.append(start_background_tasks) 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) app.on_shutdown.append(close_comment_scheduler)
aiojobs.aiohttp.setup(app, **kwargs) aiojobs.aiohttp.setup(app, **kwargs)
app.add_routes([ app.add_routes([
@ -79,19 +72,51 @@ def create_app(conf, db_path='DEFAULT', **kwargs):
web.get('/', get_api_endpoint), web.get('/', get_api_endpoint),
web.get('/api', 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): 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())