Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.5 kB
3
Indexable
Never
from haystack.signals import RealtimeSignalProcessor
import boto3

class DistributedRealtimeSignalProcessor(RealtimeSignalProcessor):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.sns_client = boto3.client('sns')
        self.topic_arn = 'arn:aws:sns:region:account-id:topicname'

    def handle_save(self, sender, instance, **kwargs):
        super().handle_save(sender, instance, **kwargs)
        # Publicar evento
        self.sns_client.publish(
            TopicArn=self.topic_arn,
            Message='IndexUpdate',
            MessageAttributes={
                'Model': {
                    'DataType': 'String',
                    'StringValue': sender.__name__
                },
                'ID': {
                    'DataType': 'String',
                    'StringValue': str(instance.pk)
                }
            }
        )

    def handle_delete(self, sender, instance, **kwargs):
        super().handle_delete(sender, instance, **kwargs)
        # Publicar evento
        self.sns_client.publish(
            TopicArn=self.topic_arn,
            Message='IndexDelete',
            MessageAttributes={
                'Model': {
                    'DataType': 'String',
                    'StringValue': sender.__name__
                },
                'ID': {
                    'DataType': 'String',
                    'StringValue': str(instance.pk)
                }
            }
        )
Leave a Comment