Skip to content

Commit

Permalink
Use asynchronous logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Hellikrox authored and polina-popova committed Apr 16, 2019
1 parent 0fa393b commit d99cf24
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
2 changes: 2 additions & 0 deletions server/bin/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import redis
import tornado.ioloop
from aiologger import Logger as async_logger
from tornado.options import define, options

import huntflow_reloaded.scheduler
Expand Down Expand Up @@ -60,6 +61,7 @@ def main():
sys.exit(1)

args = {
'logger' : async_logger.with_default_handlers(name='tornado.application'),
'postgres': {
'dbname': options.postgres_dbname,
'hostname': options.postgres_host,
Expand Down
22 changes: 11 additions & 11 deletions server/huntflow_reloaded/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@


import json
import logging
import re
from datetime import datetime

Expand Down Expand Up @@ -63,7 +62,6 @@ def __init__(self, application, request, **kwargs):
self._channel_name = self._channel_name or None
self._decoded_body = {}
self._handlers = {}
self._logger = logging.getLogger('tornado.application')
self._redis_conn = self._redis_conn or None
self._req_type = None

Expand All @@ -73,10 +71,11 @@ def __init__(self, application, request, **kwargs):
val = self._get_attr_or_stub('{}_handler'.format(i.lower()))
self._handlers[key] = val

def initialize(self, redis_conn, channel_name, postgres): # pylint: disable=arguments-differ
def initialize(self, redis_conn, channel_name, postgres, logger): # pylint: disable=arguments-differ
self._channel_name = channel_name
self._redis_conn = redis_conn
self._postgres_data = postgres
self._logger = logger

def _classify_request(self):
try:
Expand Down Expand Up @@ -124,44 +123,45 @@ async def post(self): # pylint: disable=arguments-differ
try:
self._classify_request()
except UndefinedType:
self._logger.debug(body)
await self._logger.debug(body)
self.write('Undefined type')
self.set_status(500)
return
except UnknownType:
self._logger.debug(body)
await self._logger.debug(body)
self.write('Unknown type')
self.set_status(500)
return

self._logger.debug(self._decoded_body)
await self._logger.debug(self._decoded_body)

try:
await self._handlers[self._req_type]()
except IncompleteRequest:
self._logger.debug(body)
await self._logger.debug(body)
self.write('Incomplete request')
self.set_status(500)
return

self._logger.debug(body)
await self._logger.debug(body)


#
# Handlers
#

async def add_type_handler(self):
"""Invokes when a request of the 'ADD' type is received. """
self._logger.info("Handling 'add' request")
await self._logger.info("Handling 'add' request")

async def removed_type_handler(self):
"""Invokes when a request of the 'REMOVED' type is received. """
self._logger.info("Handling 'removed' request")
await self._logger.info("Handling 'removed' request")

async def status_type_handler(self): # pylint: disable=too-many-locals
"""Invokes when a request of the 'STATUS' type is received. """

self._logger.info("Handling 'status' request")
await self._logger.info("Handling 'status' request")

event = self._decoded_body['event']
if not event.get('calendar_event'):
Expand Down
1 change: 1 addition & 0 deletions server/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
aiologger==0.3.0
alembic==1.0.7
APScheduler==3.5.3
fakeredis==0.16.*
Expand Down
3 changes: 3 additions & 0 deletions server/test/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import subprocess
import sqlalchemy as sa # Tests are running synchronously so we have to use sqlalchemy instead of gino.
import testing.postgresql
from aiologger import Logger
from tornado.ioloop import IOLoop
from tornado.testing import AsyncHTTPTestCase
from tornado.web import Application
Expand Down Expand Up @@ -86,7 +87,9 @@ def tearDown(self):
class HuntflowWebhookHandlerTest(WebTestCase):
def get_handlers(self):
conn = fakeredis.FakeStrictRedis()
logger = Logger.with_default_handlers()
args = {
'logger': logger,
'postgres': {
'dbname': 'test',
'hostname': 'localhost',
Expand Down

0 comments on commit d99cf24

Please sign in to comment.