Refactor + fault-tolerance

This commit is contained in:
Oleg Silkin 2019-05-21 15:13:08 -04:00
parent 0da5f4e3e3
commit 3ab97b171a
11 changed files with 64 additions and 18 deletions

27
config/nginx.conf Normal file
View file

@ -0,0 +1,27 @@
worker_processes 1;
daemon off;
error_log /dev/stdout warn;
events {
worker_connections 1024;
accept_mutex off;
}
http {
default_type application/json;
access_log off;
server {
listen 5921;
location /api {
proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_pass http://localhost:2903;
}
}
}

View file

@ -1,7 +1,7 @@
import logging import logging
import sqlite3 import sqlite3
from lbry_comment_server.settings import config from src.settings import config
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

17
src/data.conf Normal file
View file

@ -0,0 +1,17 @@
location / {
limit_except GET HEAD {
deny all;
}
}
location /api/ {
limit_except GET HEAD {
deny all;
}
}
location /api {
limit_except GET HEAD POST {
}
}

View file

@ -7,7 +7,7 @@ import typing
import aiosqlite import aiosqlite
import nacl.hash import nacl.hash
from lbry_comment_server.settings import config from src.settings import config
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View file

@ -5,10 +5,10 @@ import asyncio
from aiohttp import web from aiohttp import web
from aiojobs.aiohttp import atomic from aiojobs.aiohttp import atomic
from lbry_comment_server.writes import write_comment from src.writes import write_comment
from lbry_comment_server.database import get_claim_comments from src.database import get_claim_comments
from lbry_comment_server.database import get_comments_by_id, get_comment_ids from src.database import get_comments_by_id, get_comment_ids
from lbry_comment_server.database import obtain_connection from src.database import obtain_connection
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View file

@ -5,13 +5,13 @@ import asyncio
from aiohttp import web from aiohttp import web
import schema.db_helpers import schema.db_helpers
from lbry_comment_server.database import obtain_connection from src.database import obtain_connection
from lbry_comment_server.handles import api_endpoint from src.handles import api_endpoint
from lbry_comment_server.settings import config from src.settings import config
from lbry_comment_server.writes import create_comment_scheduler, DatabaseWriter from src.writes import create_comment_scheduler, DatabaseWriter
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG) # logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(config['LOGGING_FORMAT']) formatter = logging.Formatter(config['LOGGING_FORMAT'])
debug_handler = logging.FileHandler(config['PATH']['LOG']) debug_handler = logging.FileHandler(config['PATH']['LOG'])
@ -86,14 +86,16 @@ async def stop_app(runner):
await runner.cleanup() await runner.cleanup()
async def run_app(app, duration=3600): async def run_app(app):
runner = None runner = None
try: try:
runner = web.AppRunner(app) runner = web.AppRunner(app)
await runner.setup() await runner.setup()
site = web.TCPSite(runner, config['HOST'], config['PORT']) site = web.TCPSite(runner, config['HOST'], config['PORT'])
await site.start() await site.start()
await asyncio.sleep(duration) while True:
await asyncio.sleep(2**63)
except asyncio.CancelledError as cerr: except asyncio.CancelledError as cerr:
pass pass
finally: finally:

View file

@ -4,7 +4,7 @@ import logging
import aiojobs import aiojobs
from asyncio import coroutine from asyncio import coroutine
from lbry_comment_server.database import obtain_connection, create_comment from src.database import obtain_connection, create_comment
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View file

@ -5,10 +5,10 @@ from faker.providers import internet
from faker.providers import lorem from faker.providers import lorem
from faker.providers import misc from faker.providers import misc
from lbry_comment_server.database import get_comments_by_id, create_comment, get_comment_ids, create_comment_async, \ from src.database import get_comments_by_id, create_comment, get_comment_ids, create_comment_async, \
get_claim_comments get_claim_comments
from schema.db_helpers import setup_database, teardown_database from schema.db_helpers import setup_database, teardown_database
from lbry_comment_server.settings import config from src.settings import config
from tests.testcase import DatabaseTestCase, AsyncioTestCase from tests.testcase import DatabaseTestCase, AsyncioTestCase
fake = faker.Faker() fake = faker.Faker()

View file

@ -4,9 +4,9 @@ from unittest.case import _Outcome
import asyncio import asyncio
from lbry_comment_server.database import obtain_connection from src.database import obtain_connection
from schema.db_helpers import setup_database, teardown_database from schema.db_helpers import setup_database, teardown_database
from lbry_comment_server.settings import config from src.settings import config
class AsyncioTestCase(unittest.TestCase): class AsyncioTestCase(unittest.TestCase):