Intro

As we use microservices architecture, we need to have a way to communicate between services. One of the ways to do this is to use the event listener pattern.

Redis pub/sub as a message broker

When the action is performed, the service will publish an event to the Redis channel. The other services that are interested in this event will subscribe to the channel and listen for the event.

Channels

review_approved - Triggered when the review is approved by moderators. Depreciated, use moderate_action instead.

{
    "id": "f7b3b3b4-0b3b-4b3b-8b3b-0b3b3b3b3b3b"
}

review_created - Triggered when the review is created.

{
    "id": "f7b3b3b4-0b3b-4b3b-8b3b-0b3b3b3b3b3b"
}

record_change - Triggered when the record is changed.

{
    "id": "f7b3b3b4-0b3b-4b3b-8b3b-0b3b3b3b3b3b",
    "model_name": "Review",
}

record_delete - Triggered when the record is deleted.

{
    "id": "f7b3b3b4-0b3b-4b3b-8b3b-0b3b3b3b3b3b",
    "model_name": "Review",
}

record_create - Triggered when the record is created.

{
    "id": "f7b3b3b4-0b3b-4b3b-8b3b-0b3b3b3b3b3b",
    "model_name": "Review",
}

sso_user_changed - Triggered when the user is changed in the SSO. Depreciated. use id_user_changed instead.

{
    "data": {},
    "action": "update" # update, delete
}

id_user_changed - Triggered when the user is changed in the ID service.

{
    "data": {"id": "f7b3b3b4-0b3b-4b3b-8b3b-0b3b3b3b3b3b"},
    "action": "update" # update, delete
}

grant_published - Triggered when the grant is published.

{
    "id": "f7b3b3b4-0b3b-4b3b-8b3b-0b3b3b3b3b3b"
}

user_applied - Triggered when the user applied for the grant.

{
    "id": "f7b3b3b4-0b3b-4b3b-8b3b-0b3b3b3b3b3b"
}

winners_announcement - Triggered when the winners are announced for the grant.

{
    "id": "f7b3b3b4-0b3b-4b3b-8b3b-0b3b3b3b3b3b"
}

moderate_action - Triggered when the action is performed by the moderator.

{
    "action_id": "f7b3b3b4-0b3b-4b3b-8b3b-0b3b3b3b3b3b",
    "status": 1,  # 1 - approved, 2 - rejected ...
    "model_name": "Review",
    "id": "f7b3b3b4-0b3b-4b3b-8b3b-0b3b3b3b3b3b"
}

user_survey_finished - Triggered when the user finished the survey.

{
    "id": "f7b3b3b4-0b3b-4b3b-8b3b-0b3b3b3b3b3b"
}

Celery task

Send sms

  1. Create ShortMessage model instance
  2. trigger celery task with from django.db import transaction from core.celery import app transaction.on_commit( lambda: app.send_task( "send_short_message", args=(instance.id,), serializer="json", queue="sms_send_queue" ) )

Send email

  1. Create EmailMessage model instance
  2. trigger celery task with from django.db import transaction from core.celery import app transaction.on_commit( lambda: app.send_task( "send_email_task", args=(instance.id,), serializer="json", queue="sms_email_queue" ) )

Send notification

  1. Create Notification model instance
  2. trigger celery task with from django.db import transaction from core.celery import app transaction.on_commit( lambda: app.send_task( "send_notification", args=(instance.id,), serializer="json", queue="send_notification" ) )