Skip to content

Commit

Permalink
Allow to configure task handler (#28)
Browse files Browse the repository at this point in the history
Previously, every task was sent as AWS event, which caused error for
local environment setup.

Now, there is possibility to set different handler, e.g. one that prints
every task to stdout and nothing more.
  • Loading branch information
Toreno96 authored Jul 17, 2020
1 parent 9d53baa commit a3c9485
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
1 change: 0 additions & 1 deletion services/backend/common/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
from .emails import SendEmail, EmailParams # noqa
from .tasks import Task # noqa
6 changes: 5 additions & 1 deletion services/backend/common/emails.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import importlib
from dataclasses import dataclass, asdict

from .tasks import Task
from django.conf import settings

module_name, package = settings.TASKS_BASE_HANDLER.rsplit(".", maxsplit=1)
Task = getattr(importlib.import_module(module_name), package)


@dataclass
Expand Down
29 changes: 18 additions & 11 deletions services/backend/common/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@ def __init__(self, name: str, source: str, event_bus_name=settings.WORKERS_EVENT
self.source = source
self.event_bus_name = event_bus_name

def get_entries(self, data: dict):
return [
{
'Time': datetime.now(),
'Source': self.source,
'DetailType': self.name,
'Detail': json.dumps({"type": self.name, **data}),
'EventBusName': self.event_bus_name,
},
]

def apply(self, data: dict):
client = boto3.client('events', endpoint_url=settings.AWS_EVENTS_URL)
client.put_events(
Entries=[
{
'Time': datetime.now(),
'Source': self.source,
'DetailType': self.name,
'Detail': json.dumps({"type": self.name, **data}),
'EventBusName': self.event_bus_name,
},
]
)
client.put_events(Entries=self.get_entries(data))


class TaskPrinter(Task):
def apply(self, data: dict):
entries = self.get_entries(data)
print(f"Put events: {entries=}")
2 changes: 2 additions & 0 deletions services/backend/restauth/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,5 @@
WORKERS_EVENT_BUS_NAME = env("WORKERS_EVENT_BUS_NAME", default=None)

AWS_EVENTS_URL = env("AWS_EVENTS_URL", default=None)

TASKS_BASE_HANDLER = env("TASKS_BASE_HANDLER", default="common.tasks.Task")

0 comments on commit a3c9485

Please sign in to comment.