Skip to content

Commit

Permalink
Refactor: write log events asynchronously via celery queue (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
jayaddison authored May 27, 2020
1 parent e0b8530 commit 9ac8412
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 deletions.
28 changes: 15 additions & 13 deletions reciperadar/api/recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from user_agents import parse as ua_parser

from reciperadar import app
from reciperadar.models.events.search import SearchEvent
from reciperadar.models.recipes import Recipe
from reciperadar.search.recipes import RecipeSearch
from reciperadar.utils.decorators import internal
Expand Down Expand Up @@ -60,18 +59,21 @@ def recipe_search():
# Perform a recrawl for the search to find any new/missing recipes
recrawl_search.delay(include, exclude, equipment, offset)

# TODO: Once 'event' is json serializable: switch to store_event.delay
store_event(SearchEvent(
suspected_bot=suspected_bot,
include=include,
exclude=exclude,
equipment=equipment,
offset=offset,
limit=limit,
sort=sort,
results_ids=[result['id'] for result in results['results']],
results_total=results['total']
))
# Log a search event
store_event.delay(
event_table='searches',
event_data={
'suspected_bot': suspected_bot,
'include': include,
'exclude': exclude,
'equipment': equipment,
'offset': offset,
'limit': limit,
'sort': sort,
'results_ids': [result['id'] for result in results['results']],
'results_total': results['total']
}
)

return jsonify(results)

Expand Down
14 changes: 8 additions & 6 deletions reciperadar/api/redirect.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from flask import abort, redirect

from reciperadar import app
from reciperadar.models.events.redirect import RedirectEvent
from reciperadar.models.recipes import Recipe
from reciperadar.workers.events import store_event

Expand All @@ -12,10 +11,13 @@ def recipe_redirect(recipe_id):
if not recipe:
return abort(404)

store_event(RedirectEvent(
recipe_id=recipe.id,
domain=recipe.domain,
src=recipe.src
))
store_event.delay(
event_table='redirects',
event_data={
'recipe_id': recipe.id,
'domain': recipe.domain,
'src': recipe.src,
}
)

return redirect(recipe.src, code=301)
14 changes: 13 additions & 1 deletion reciperadar/workers/events.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
from reciperadar.models.events.redirect import RedirectEvent
from reciperadar.models.events.search import SearchEvent
from reciperadar.services.database import Database
from reciperadar.workers.broker import celery


event_table_classes = {
'redirects': RedirectEvent,
'searches': SearchEvent,
}


@celery.task
def store_event(event):
def store_event(event_table, event_data):
if event_table not in event_table_classes:
return
event = event_table_classes[event_table](**event_data)

session = Database().get_session()
session.add(event)
session.commit()
Expand Down
4 changes: 2 additions & 2 deletions tests/api/test_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_search_recrawling(query, store, recrawl, client):


@patch('reciperadar.api.recipes.recrawl_search.delay')
@patch('reciperadar.api.recipes.store_event')
@patch('reciperadar.api.recipes.store_event.delay')
@patch.object(RecipeSearch, 'query')
def test_bot_search(query, store, recrawl, client):
query.return_value = {'results': [], 'total': 0}
Expand All @@ -65,4 +65,4 @@ def test_bot_search(query, store, recrawl, client):
client.get('/api/recipes/search', headers={'user-agent': user_agent})

assert store.called
assert store.call_args[0][0].suspected_bot
assert store.call_args[1]['event_data']['suspected_bot'] is True

0 comments on commit 9ac8412

Please sign in to comment.