fix: maintain event configurations only on event producer site

This commit is contained in:
Rucha Mahabal 2019-10-03 21:35:09 +05:30
parent 86b2b25915
commit 7013ffb59e
12 changed files with 80 additions and 44 deletions

View file

@ -4,8 +4,8 @@
"editable_grid": 1,
"engine": "InnoDB",
"field_order": [
"remote_fieldname",
"local_fieldname",
"remote_fieldname",
"is_child_table",
"child_table_mapping"
],
@ -38,7 +38,7 @@
}
],
"istable": 1,
"modified": "2019-09-27 12:46:50.165135",
"modified": "2019-10-03 21:18:32.068407",
"modified_by": "Administrator",
"module": "Events Streaming",
"name": "Document Type Field Mapping",

View file

@ -6,8 +6,8 @@
"field_order": [
"local_doctype",
"remote_doctype",
"mapping_section",
"field_maps"
"section_break_3",
"field_mapping"
],
"fields": [
{
@ -25,19 +25,18 @@
"reqd": 1
},
{
"fieldname": "mapping_section",
"fieldtype": "Section Break",
"label": "Mapping"
"fieldname": "section_break_3",
"fieldtype": "Section Break"
},
{
"fieldname": "field_maps",
"fieldname": "field_mapping",
"fieldtype": "Table",
"label": "Field Maps",
"label": "Field Mapping",
"options": "Document Type Field Mapping",
"reqd": 1
}
],
"modified": "2019-09-27 12:47:04.887985",
"modified": "2019-10-03 21:17:32.785912",
"modified_by": "Administrator",
"module": "Events Streaming",
"name": "Document Type Mapping",

View file

@ -1,5 +1,5 @@
{
"creation": "2019-08-29 11:35:00.625815",
"creation": "2019-10-03 21:08:25.890352",
"doctype": "DocType",
"editable_grid": 1,
"engine": "InnoDB",
@ -26,6 +26,7 @@
"label": "Has Mapping"
},
{
"depends_on": "eval: doc.has_mapping",
"fieldname": "mapping",
"fieldtype": "Link",
"label": "Mapping",
@ -41,10 +42,10 @@
}
],
"istable": 1,
"modified": "2019-09-27 13:53:09.687499",
"modified": "2019-10-03 21:27:31.483213",
"modified_by": "Administrator",
"module": "Events Streaming",
"name": "Event Subscribed DocType",
"name": "Event Configuration",
"owner": "Administrator",
"permissions": [],
"quick_entry": 1,

View file

@ -6,5 +6,5 @@ from __future__ import unicode_literals
# import frappe
from frappe.model.document import Document
class EventSubscribedDocType(Document):
class EventConfiguration(Document):
pass

View file

@ -24,7 +24,7 @@
"fieldname": "subscribed_doctypes",
"fieldtype": "Table",
"label": "Subscribed Document Types",
"options": "Event Subscribed DocType",
"options": "Event Subscribed Document Type",
"reqd": 1
},
{
@ -47,7 +47,7 @@
"reqd": 1
}
],
"modified": "2019-09-11 12:22:01.974457",
"modified": "2019-10-03 21:11:34.926285",
"modified_by": "Administrator",
"module": "Events Streaming",
"name": "Event Consumer",

View file

@ -6,7 +6,7 @@
"engine": "InnoDB",
"field_order": [
"producer_url",
"subscribed_doctypes",
"event_configuration",
"last_update",
"user",
"api_key",
@ -21,13 +21,6 @@
"reqd": 1,
"unique": 1
},
{
"fieldname": "subscribed_doctypes",
"fieldtype": "Table",
"label": "Subscribed Doctypes",
"options": "Event Subscribed DocType",
"reqd": 1
},
{
"fieldname": "last_update",
"fieldtype": "Data",
@ -52,9 +45,16 @@
"label": "Event Subscriber",
"options": "User",
"reqd": 1
},
{
"fieldname": "event_configuration",
"fieldtype": "Table",
"label": "Event Configuration",
"options": "Event Configuration",
"reqd": 1
}
],
"modified": "2019-09-25 10:17:32.591436",
"modified": "2019-10-03 21:26:32.739416",
"modified_by": "Administrator",
"module": "Events Streaming",
"name": "Event Producer",

View file

@ -18,10 +18,10 @@ class EventProducer(Document):
def validate(self):
'''create custom field to store remote docname and remote site url'''
for entry in self.subscribed_doctypes:
for entry in self.event_configuration:
if not entry.use_same_name:
if not frappe.db.exists('Custom Field', {'fieldname': 'remote_docname', 'dt': entry.ref_doctype}):
df = dict(fieldname='remote_docname', label='Remote Docname', fieldtype='Data', read_only=1, print_hide=1)
df = dict(fieldname='remote_docname', label='Remote Document Name', fieldtype='Data', read_only=1, print_hide=1)
create_custom_field(entry.ref_doctype, df)
if not frappe.db.exists('Custom Field', {'fieldname': 'remote_site_name', 'dt': entry.ref_doctype}):
df = dict(fieldname='remote_site_name', label='Remote Site', fieldtype='Data', read_only=1, print_hide=1)
@ -32,7 +32,7 @@ class EventProducer(Document):
event_consumer = producer_site.get_doc('Event Consumer', get_current_node())
if event_consumer:
event_consumer.subscribed_doctypes = []
for entry in self.subscribed_doctypes:
for entry in self.event_configuration:
event_consumer.subscribed_doctypes.append({
'ref_doctype': entry.ref_doctype
})
@ -43,8 +43,11 @@ class EventProducer(Document):
'''register event consumer on the producer site'''
producer_site = FrappeClient(self.producer_url)
subscribed_doctypes = []
for entry in self.subscribed_doctypes:
subscribed_doctypes.append(entry.ref_doctype)
for entry in self.event_configuration:
if entry.has_mapping:
subscribed_doctypes.append(frappe.db.get_value('Document Type Mapping', entry.mapping, 'remote_doctype'))
else:
subscribed_doctypes.append(entry.ref_doctype)
(api_key, api_secret, last_update) = producer_site.post_request({
'cmd': 'frappe.events_streaming.doctype.event_consumer.event_consumer.register_consumer',
'event_consumer': get_current_node(),
@ -89,7 +92,7 @@ def pull_from_node(event_producer):
last_update = event_producer.last_update
doctypes = []
for entry in event_producer.subscribed_doctypes:
for entry in event_producer.event_configuration:
doctypes.append(entry.ref_doctype)
updates = get_updates(producer_site, last_update, doctypes)
@ -98,27 +101,26 @@ def pull_from_node(event_producer):
sync(update, producer_site, event_producer)
def sync(update, producer_site, event_producer, in_retry=False):
use_same_name = check_use_same_name(update.ref_doctype, event_producer.subscribed_doctypes)
(use_same_name, has_mapping, mapping) = frappe.db.get_value(
doctype = 'Event Configuration',
filters = {'parent': event_producer.name, 'ref_doctype': update.ref_doctype},
fieldname = ['use_same_name', 'has_mapping', 'mapping'],
)
try:
if update.update_type == 'Create':
set_insert(update, producer_site, use_same_name, event_producer.name)
if update.update_type == 'Update':
set_update(update, producer_site, use_same_name)
if update.update_type == 'Delete':
set_delete(update, use_same_name)
if in_retry:
return 'Synced'
log_event_sync(update, event_producer.name, 'Synced')
except Exception:
if in_retry:
return 'Failed'
log_event_sync(update, event_producer.name, 'Failed', frappe.get_traceback())
frappe.db.set_value('Event Producer', event_producer.name, 'last_update', update.name)
@ -256,12 +258,6 @@ def resync(update):
event_producer = frappe.get_doc('Event Producer', update.event_producer)
return sync(update, producer_site, event_producer, in_retry=True)
def check_use_same_name(doctype, subscription_configs):
for entry in subscription_configs:
if entry.ref_doctype == doctype and entry.use_same_name != 1:
return False
return True
def set_custom_fields(local_doc, remote_docname, remote_site_name):
'''sets custom field in doc for storing remote docname'''
frappe.db.set_value(local_doc.doctype, local_doc.name, 'remote_docname', remote_docname)

View file

@ -0,0 +1,30 @@
{
"creation": "2019-10-03 21:10:54.754651",
"doctype": "DocType",
"editable_grid": 1,
"engine": "InnoDB",
"field_order": [
"ref_doctype"
],
"fields": [
{
"fieldname": "ref_doctype",
"fieldtype": "Link",
"in_list_view": 1,
"label": "Document Type",
"options": "DocType",
"reqd": 1
}
],
"istable": 1,
"modified": "2019-10-03 21:10:54.754651",
"modified_by": "Administrator",
"module": "Events Streaming",
"name": "Event Subscribed Document Type",
"owner": "Administrator",
"permissions": [],
"quick_entry": 1,
"sort_field": "modified",
"sort_order": "DESC",
"track_changes": 1
}

View file

@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2019, Frappe Technologies and contributors
# For license information, please see license.txt
from __future__ import unicode_literals
# import frappe
from frappe.model.document import Document
class EventSubscribedDocumentType(Document):
pass

View file

@ -84,7 +84,7 @@
}
],
"in_create": 1,
"modified": "2019-10-01 08:09:34.416375",
"modified": "2019-10-03 21:29:43.036851",
"modified_by": "Administrator",
"module": "Events Streaming",
"name": "Event Sync Log",