Upgrades Server to Allow Production Deployment #2
1 changed files with 61 additions and 36 deletions
97
src/app.py
97
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,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,33 +57,66 @@ async def cleanup_background_tasks(app):
|
||||||
app['writer'].close()
|
app['writer'].close()
|
||||||
|
|
||||||
|
|
||||||
def create_app(conf, db_path='DEFAULT', **kwargs):
|
class CommentDaemon:
|
||||||
app = web.Application()
|
def __init__(self, config, db_path=None, **kwargs):
|
||||||
app['START_TIME'] = int(time.time())
|
self.config = config
|
||||||
insert_to_config(app, conf, db_path)
|
app = web.Application()
|
||||||
app.on_startup.append(setup_db_schema)
|
self.insert_to_config(app, config, db_file=db_path)
|
||||||
app.on_startup.append(start_background_tasks)
|
app.on_startup.append(setup_db_schema)
|
||||||
app.on_shutdown.append(cleanup_background_tasks)
|
app.on_startup.append(start_background_tasks)
|
||||||
app.on_shutdown.append(close_comment_scheduler)
|
app.on_shutdown.append(stop_background_tasks)
|
||||||
aiojobs.aiohttp.setup(app, **kwargs)
|
app.on_shutdown.append(close_comment_scheduler)
|
||||||
app.add_routes([
|
aiojobs.aiohttp.setup(app, **kwargs)
|
||||||
web.post('/api', api_endpoint),
|
app.add_routes([
|
||||||
web.get('/', get_api_endpoint),
|
web.post('/api', api_endpoint),
|
||||||
web.get('/api', get_api_endpoint)
|
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):
|
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