2019-05-26 06:42:39 +02:00
|
|
|
# cython: language_level=3
|
2019-12-30 23:26:37 +01:00
|
|
|
import asyncio
|
2019-05-21 13:54:52 +02:00
|
|
|
import logging
|
2019-05-30 17:25:42 +02:00
|
|
|
import pathlib
|
2019-07-22 19:16:54 +02:00
|
|
|
import signal
|
2019-07-22 14:14:07 +02:00
|
|
|
import time
|
2019-05-21 13:54:52 +02:00
|
|
|
|
2019-07-19 06:32:14 +02:00
|
|
|
import aiojobs
|
2019-05-21 13:54:52 +02:00
|
|
|
import aiojobs.aiohttp
|
2019-05-16 01:17:06 +02:00
|
|
|
from aiohttp import web
|
|
|
|
|
2019-08-05 00:05:03 +02:00
|
|
|
from src.database.queries import obtain_connection, DatabaseWriter
|
2019-12-30 23:26:37 +01:00
|
|
|
from src.database.queries import setup_database, backup_database
|
2019-07-24 07:43:50 +02:00
|
|
|
from src.server.handles import api_endpoint, get_api_endpoint
|
2019-05-26 07:31:05 +02:00
|
|
|
|
2019-05-21 12:56:27 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
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-30 17:25:42 +02:00
|
|
|
if not pathlib.Path(app['db_path']).exists():
|
2019-07-31 07:53:39 +02:00
|
|
|
logger.info(f'Setting up schema in {app["db_path"]}')
|
|
|
|
setup_database(app['db_path'])
|
2019-05-30 17:25:42 +02:00
|
|
|
else:
|
2019-07-22 14:38:53 +02:00
|
|
|
logger.info(f'Database already exists in {app["db_path"]}, skipping setup')
|
2019-05-16 01:17:06 +02:00
|
|
|
|
2019-05-20 07:18:47 +02:00
|
|
|
|
2019-06-04 14:14:12 +02:00
|
|
|
async def database_backup_routine(app):
|
2019-05-21 12:56:27 +02:00
|
|
|
try:
|
|
|
|
while True:
|
2019-08-24 06:19:11 +02:00
|
|
|
await asyncio.sleep(app['config']['backup_int'])
|
2019-07-22 14:14:07 +02:00
|
|
|
with app['reader'] as conn:
|
2019-05-23 12:34:50 +02:00
|
|
|
logger.debug('backing up database')
|
2019-07-22 14:38:53 +02:00
|
|
|
backup_database(conn, app['backup'])
|
2019-06-04 14:14:12 +02:00
|
|
|
except asyncio.CancelledError:
|
2019-05-21 13:28:21 +02:00
|
|
|
pass
|
2019-05-21 12:56:27 +02:00
|
|
|
|
2019-05-23 12:34:50 +02:00
|
|
|
|
2019-07-22 19:16:54 +02:00
|
|
|
async def start_background_tasks(app):
|
2019-12-27 04:44:37 +01:00
|
|
|
# Reading the DB
|
2019-05-31 21:50:31 +02:00
|
|
|
app['reader'] = obtain_connection(app['db_path'], True)
|
2019-07-30 06:14:42 +02:00
|
|
|
app['waitful_backup'] = asyncio.create_task(database_backup_routine(app))
|
2019-12-27 04:44:37 +01:00
|
|
|
|
|
|
|
# Scheduler to prevent multiple threads from writing to DB simulataneously
|
2019-07-19 06:32:14 +02:00
|
|
|
app['comment_scheduler'] = await aiojobs.create_scheduler(limit=1, pending_limit=0)
|
|
|
|
app['db_writer'] = DatabaseWriter(app['db_path'])
|
|
|
|
app['writer'] = app['db_writer'].connection
|
2019-05-23 12:34:50 +02:00
|
|
|
|
2019-12-27 04:44:37 +01:00
|
|
|
# for requesting to external and internal APIs
|
|
|
|
app['webhooks'] = await aiojobs.create_scheduler(pending_limit=0)
|
2019-09-07 02:27:14 +02:00
|
|
|
|
2019-05-23 12:34:50 +02:00
|
|
|
|
2019-07-30 06:14:42 +02:00
|
|
|
async def close_database_connections(app):
|
2019-07-20 15:06:34 +02:00
|
|
|
logger.info('Ending background backup loop')
|
2019-05-21 12:56:27 +02:00
|
|
|
app['waitful_backup'].cancel()
|
|
|
|
await app['waitful_backup']
|
2019-05-21 13:28:21 +02:00
|
|
|
app['reader'].close()
|
|
|
|
app['writer'].close()
|
2019-07-30 06:14:42 +02:00
|
|
|
app['db_writer'].cleanup()
|
|
|
|
|
|
|
|
|
2019-12-27 04:44:37 +01:00
|
|
|
async def close_schedulers(app):
|
2019-07-30 06:14:42 +02:00
|
|
|
logger.info('Closing comment_scheduler')
|
|
|
|
await app['comment_scheduler'].close()
|
2019-05-21 12:56:27 +02:00
|
|
|
|
2019-12-27 04:44:37 +01:00
|
|
|
logger.info('Closing scheduler for webhook requests')
|
|
|
|
await app['webhooks'].close()
|
|
|
|
|
2019-05-21 12:56:27 +02:00
|
|
|
|
2019-07-22 19:16:54 +02:00
|
|
|
class CommentDaemon:
|
2019-07-30 06:14:42 +02:00
|
|
|
def __init__(self, config, db_file=None, backup=None, **kwargs):
|
2019-07-22 19:16:54 +02:00
|
|
|
app = web.Application()
|
2019-12-27 04:44:37 +01:00
|
|
|
|
|
|
|
# configure the config
|
2019-07-30 06:14:42 +02:00
|
|
|
app['config'] = config
|
2019-08-24 06:19:11 +02:00
|
|
|
self.config = app['config']
|
2019-12-27 04:44:37 +01:00
|
|
|
|
|
|
|
# configure the db file
|
2019-07-30 06:14:42 +02:00
|
|
|
if db_file:
|
|
|
|
app['db_path'] = db_file
|
|
|
|
app['backup'] = backup
|
|
|
|
else:
|
2019-08-24 06:19:11 +02:00
|
|
|
app['db_path'] = config['path']['database']
|
2019-07-30 06:14:42 +02:00
|
|
|
app['backup'] = backup or (app['db_path'] + '.backup')
|
2019-12-27 04:44:37 +01:00
|
|
|
|
|
|
|
# configure the order of tasks to run during app lifetime
|
2019-07-22 19:16:54 +02:00
|
|
|
app.on_startup.append(setup_db_schema)
|
|
|
|
app.on_startup.append(start_background_tasks)
|
2019-12-27 04:44:37 +01:00
|
|
|
app.on_shutdown.append(close_schedulers)
|
2019-07-30 06:14:42 +02:00
|
|
|
app.on_cleanup.append(close_database_connections)
|
2019-07-22 19:16:54 +02:00
|
|
|
aiojobs.aiohttp.setup(app, **kwargs)
|
2019-12-27 04:44:37 +01:00
|
|
|
|
|
|
|
# Configure the routes
|
2019-07-22 19:16:54 +02:00
|
|
|
app.add_routes([
|
|
|
|
web.post('/api', api_endpoint),
|
|
|
|
web.get('/', get_api_endpoint),
|
|
|
|
web.get('/api', get_api_endpoint)
|
|
|
|
])
|
|
|
|
self.app = app
|
2019-07-30 06:14:42 +02:00
|
|
|
self.app_runner = None
|
2019-07-22 19:16:54 +02:00
|
|
|
self.app_site = None
|
|
|
|
|
2019-07-30 06:14:42 +02:00
|
|
|
async def start(self, host=None, port=None):
|
2019-08-24 06:19:11 +02:00
|
|
|
self.app['start_time'] = time.time()
|
2019-07-30 06:14:42 +02:00
|
|
|
self.app_runner = web.AppRunner(self.app)
|
2019-07-22 19:16:54 +02:00
|
|
|
await self.app_runner.setup()
|
|
|
|
self.app_site = web.TCPSite(
|
|
|
|
runner=self.app_runner,
|
2019-08-24 06:19:11 +02:00
|
|
|
host=host or self.config['host'],
|
|
|
|
port=port or self.config['port'],
|
2019-07-22 19:16:54 +02:00
|
|
|
)
|
|
|
|
await self.app_site.start()
|
2019-08-24 06:19:11 +02:00
|
|
|
logger.info(f'Comment Server is running on {self.config["host"]}:{self.config["port"]}')
|
2019-07-22 19:16:54 +02:00
|
|
|
|
|
|
|
async def stop(self):
|
2019-07-30 06:14:42 +02:00
|
|
|
await self.app_runner.shutdown()
|
2019-07-22 19:16:54 +02:00
|
|
|
await self.app_runner.cleanup()
|
|
|
|
|
2019-05-16 01:17:06 +02:00
|
|
|
|
2019-07-30 06:14:42 +02:00
|
|
|
def run_app(config, db_file=None):
|
|
|
|
comment_app = CommentDaemon(config=config, db_file=db_file, close_timeout=5.0)
|
2019-07-22 19:16:54 +02:00
|
|
|
|
2019-07-30 06:14:42 +02:00
|
|
|
loop = asyncio.get_event_loop()
|
2019-07-22 19:16:54 +02:00
|
|
|
|
|
|
|
def __exit():
|
|
|
|
raise web.GracefulExit()
|
|
|
|
|
|
|
|
loop.add_signal_handler(signal.SIGINT, __exit)
|
|
|
|
loop.add_signal_handler(signal.SIGTERM, __exit)
|
|
|
|
|
2019-05-21 13:28:21 +02:00
|
|
|
try:
|
2019-07-22 19:16:54 +02:00
|
|
|
loop.run_until_complete(comment_app.start())
|
|
|
|
loop.run_forever()
|
|
|
|
except (web.GracefulExit, KeyboardInterrupt, asyncio.CancelledError, ValueError):
|
2019-07-22 14:14:07 +02:00
|
|
|
logging.warning('Server going down, asyncio loop raised cancelled error:')
|
2019-07-22 19:16:54 +02:00
|
|
|
finally:
|
2019-07-30 06:29:26 +02:00
|
|
|
loop.run_until_complete(comment_app.stop())
|