2019-05-21 13:54:52 +02:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import aiojobs.aiohttp
|
2019-05-16 01:17:06 +02:00
|
|
|
import asyncio
|
|
|
|
from aiohttp import web
|
|
|
|
|
2019-05-21 11:02:01 +02:00
|
|
|
import lbry_comment_server.writes as writes
|
2019-05-21 13:54:52 +02:00
|
|
|
import schema.db_helpers as helpers
|
2019-05-21 12:56:27 +02:00
|
|
|
from lbry_comment_server.database import obtain_connection
|
2019-05-21 13:54:52 +02:00
|
|
|
from lbry_comment_server.handles import api_endpoint
|
|
|
|
from lbry_comment_server.settings import config
|
2019-05-21 12:56:27 +02:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
formatter = logging.Formatter(config['LOGGING_FORMAT'])
|
|
|
|
debug_handler = logging.FileHandler(config['PATH']['LOG'])
|
|
|
|
error_handler = logging.FileHandler(config['PATH']['ERROR_LOG'])
|
|
|
|
stdout_handler = logging.StreamHandler()
|
|
|
|
|
|
|
|
debug_handler.setLevel(logging.DEBUG)
|
|
|
|
error_handler.setLevel(logging.ERROR)
|
|
|
|
stdout_handler.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
debug_handler.setFormatter(formatter)
|
|
|
|
error_handler.setFormatter(formatter)
|
|
|
|
stdout_handler.setFormatter(formatter)
|
|
|
|
|
|
|
|
logger.addHandler(debug_handler)
|
|
|
|
logger.addHandler(error_handler)
|
|
|
|
logger.addHandler(stdout_handler)
|
2019-05-16 05:32:29 +02:00
|
|
|
|
2019-05-20 07:18:47 +02:00
|
|
|
|
2019-05-21 11:02:01 +02:00
|
|
|
async def setup_db_schema(app):
|
2019-05-21 12:56:27 +02:00
|
|
|
logger.info('Setting up schema in %s', app['db_path'])
|
2019-05-21 11:02:01 +02:00
|
|
|
helpers.setup_database(app['db_path'])
|
2019-05-16 01:17:06 +02:00
|
|
|
|
2019-05-20 07:18:47 +02:00
|
|
|
|
2019-05-21 11:02:01 +02:00
|
|
|
async def close_comment_scheduler(app):
|
2019-05-21 12:56:27 +02:00
|
|
|
logger.debug('Closing comment_scheduler')
|
2019-05-21 11:02:01 +02:00
|
|
|
await app['comment_scheduler'].close()
|
2019-05-16 01:17:06 +02:00
|
|
|
|
|
|
|
|
2019-05-21 12:56:27 +02:00
|
|
|
async def create_database_backup(app):
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
await asyncio.sleep(app['config']['BACKUP_INT'])
|
|
|
|
with obtain_connection(app['db_path']) as conn:
|
|
|
|
logger.debug('%s backing up database')
|
|
|
|
helpers.backup_database(conn, app['backup'])
|
|
|
|
|
|
|
|
except asyncio.CancelledError as e:
|
2019-05-21 13:28:21 +02:00
|
|
|
pass
|
2019-05-21 12:56:27 +02:00
|
|
|
|
|
|
|
async def start_background_tasks(app: web.Application):
|
|
|
|
app['waitful_backup'] = app.loop.create_task(create_database_backup(app))
|
2019-05-21 13:28:21 +02:00
|
|
|
app['comment_scheduler'] = await writes.create_comment_scheduler()
|
|
|
|
app['writer'] = writes.DatabaseWriter(config['PATH']['DEFAULT'])
|
2019-05-21 12:56:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def cleanup_background_tasks(app):
|
|
|
|
logger.debug('Ending background backup loop')
|
|
|
|
app['waitful_backup'].cancel()
|
|
|
|
await app['waitful_backup']
|
2019-05-21 13:28:21 +02:00
|
|
|
app['reader'].close()
|
|
|
|
app['writer'].close()
|
2019-05-21 12:56:27 +02:00
|
|
|
|
|
|
|
|
2019-05-21 11:02:01 +02:00
|
|
|
def create_app(**kwargs):
|
|
|
|
app = web.Application()
|
|
|
|
app['config'] = config
|
|
|
|
app['db_path'] = config['PATH']['DEFAULT']
|
|
|
|
app['backup'] = config['PATH']['BACKUP']
|
|
|
|
app.on_startup.append(setup_db_schema)
|
2019-05-21 12:56:27 +02:00
|
|
|
app.on_startup.append(start_background_tasks)
|
|
|
|
app['reader'] = obtain_connection(app['db_path'], True)
|
2019-05-21 11:02:01 +02:00
|
|
|
app.on_shutdown.append(close_comment_scheduler)
|
2019-05-21 12:56:27 +02:00
|
|
|
app.on_shutdown.append(cleanup_background_tasks)
|
2019-05-21 11:02:01 +02:00
|
|
|
aiojobs.aiohttp.setup(app, **kwargs)
|
|
|
|
app.add_routes([web.post('/api', api_endpoint)])
|
|
|
|
return app
|
2019-05-16 01:17:06 +02:00
|
|
|
|
|
|
|
|
2019-05-21 11:02:01 +02:00
|
|
|
async def stop_app(runner):
|
2019-05-21 12:56:27 +02:00
|
|
|
logger.info('stopping app; running cleanup routine')
|
2019-05-21 11:02:01 +02:00
|
|
|
await runner.cleanup()
|
2019-05-16 01:17:06 +02:00
|
|
|
|
2019-05-20 08:11:02 +02:00
|
|
|
|
2019-05-21 11:02:01 +02:00
|
|
|
async def run_app(app, duration=3600):
|
|
|
|
runner = None
|
|
|
|
try:
|
|
|
|
runner = web.AppRunner(app)
|
|
|
|
await runner.setup()
|
|
|
|
site = web.TCPSite(runner, config['HOST'], config['PORT'])
|
|
|
|
await site.start()
|
|
|
|
await asyncio.sleep(duration)
|
2019-05-21 12:56:27 +02:00
|
|
|
except asyncio.CancelledError as cerr:
|
2019-05-21 13:28:21 +02:00
|
|
|
pass
|
2019-05-21 11:02:01 +02:00
|
|
|
finally:
|
|
|
|
await stop_app(runner)
|
2019-05-16 01:17:06 +02:00
|
|
|
|
2019-05-20 08:55:02 +02:00
|
|
|
|
2019-05-16 01:17:06 +02:00
|
|
|
if __name__ == '__main__':
|
2019-05-21 11:02:01 +02:00
|
|
|
appl = create_app(close_timeout=5.0)
|
2019-05-21 13:28:21 +02:00
|
|
|
try:
|
|
|
|
asyncio.run(web.run_app(appl, access_log=logger, host=config['HOST'], port=config['PORT']))
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
pass
|
|
|
|
except ValueError:
|
|
|
|
pass
|