Example #1
0
class SalesforceService:

    name = 'salesforce'

    contacts_rpc = RpcProxy('contacts')

    salesforce = SalesforceAPI()

    source_tracker = SourceTracker()

    @event_handler('contacts', 'contact_created')
    def handle_platform_contact_created(self, payload):

        if self.source_tracker.is_sourced_from_salesforce():
            print("Ignoring event that was sourced from salesforce")
            return

        result = self.salesforce.Contact.create(
            {'LastName': payload['contact']['name']}
        )
        print('Created {} on salesforce'.format(result))

    @handle_sobject_notification(
        'Contact', exclude_current_user=True,
        notify_for_operation_update=False
    )
    def handle_sf_contact_created(self, sobject_type, record_type, notification):
        with self.source_tracker.sourced_from_salesforce():
            contact = self.contacts_rpc.create_contact(
                {'name': notification['sobject']['Name']}
            )
        print('Created {} on platform'.format(contact))
Example #2
0
class SalesforceService:

    name = 'salesforce'

    contacts_rpc = RpcProxy('contacts')

    salesforce = SalesforceAPI()

    source_tracker = SourceTracker()

    redis = Redis('lock')

    schedule_task = ScheduleTask()

    @event_handler('contacts', 'contact_created')
    def handle_platform_contact_created(self, payload):

        if self.source_tracker.is_sourced_from_salesforce():
            print("Ignoring event that was sourced from salesforce")
            return

        self.schedule_task(self.create_on_salesforce, payload)

    @handle_sobject_notification('Contact',
                                 exclude_current_user=True,
                                 notify_for_operation_update=False)
    @skip_duplicates(operator.attrgetter('redis'), key=skip_duplicate_key)
    def handle_sf_contact_created(self, sobject_type, record_type,
                                  notification):
        self.schedule_task(self.create_on_platform, notification)

    @task
    @debounce(operator.attrgetter('redis'), key=debounce_key_sf, repeat=True)
    @entrypoint_retry(
        retry_for=ValueError,
        limit=4,
        schedule=(1000, 1000, 2000),
    )
    def create_on_salesforce(self, payload):
        print('Trying to create on salesforce...')
        raise ValueError()

        result = self.salesforce.Contact.create(
            {'LastName': payload['contact']['name']})
        print('Created {} on salesforce'.format(result))

    @task
    @debounce(operator.attrgetter('redis'), key=debounce_key_plat, repeat=True)
    @entrypoint_retry(
        retry_for=ValueError,
        limit=4,
        schedule=(1000, 1000, 2000),
    )
    def create_on_platform(self, payload):
        with self.source_tracker.sourced_from_salesforce():
            contact = self.contacts_rpc.create_contact(
                {'name': payload['sobject']['Name']})
        print('Created {} on platform'.format(contact))