[hub] Track Hub Sync on the Server

- Create a Hub Seller activity on start of Hub Sync
- Store its name as remote_id in the Run
- Update that activity remotely once the Run here is complete, status
This commit is contained in:
Prateeksha Singh
2018-07-29 14:40:52 +05:30
parent d0a1a4fe90
commit 6f05ea124e
6 changed files with 83 additions and 100 deletions

View File

@@ -2,7 +2,7 @@
# For license information, please see license.txt
from __future__ import unicode_literals
import frappe, requests, json, time
import frappe, requests, json
from frappe.utils import now, nowdate, cint
from frappe.utils.nestedset import get_root_of
from frappe.contacts.doctype.contact.contact import get_default_contact
@@ -18,7 +18,9 @@ def enable_hub():
def call_hub_method(method, params=None):
connection = get_client_connection()
params = json.loads(params)
if type(params) == unicode:
params = json.loads(params)
params.update({
'cmd': 'hub.hub.api.' + method
})
@@ -67,17 +69,46 @@ def get_valid_items(search_value=''):
@frappe.whitelist()
def publish_selected_items(items_to_publish):
for item_code in json.loads(items_to_publish):
items_to_publish = json.loads(items_to_publish)
if not len(items_to_publish):
return
for item_code in items_to_publish:
frappe.db.set_value('Item', item_code, 'publish_in_hub', 1)
# frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 1)
# time.sleep(10)
# frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 0)
hub_settings = frappe.get_doc('Hub Settings')
hub_settings.sync()
remote_id = item_sync_preprocess()
hub_settings.sync(remote_id)
return
return remote_id
def item_sync_preprocess():
# Call Hub to make a new activity
# and return an activity ID
# that will be used as the remote ID for the Migration Run
response = call_hub_method('init_new_activity_for_seller', {
'hub_seller': frappe.db.get_value("Hub Settings", "Hub Settings", "company_email"),
'activity_type': 'Items Publish'
})
if response:
# frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 1)
return response
else:
return ''
def item_sync_postprocess(obj):
response = call_hub_method('update_activity_for_seller', {
'hub_seller': frappe.db.get_value("Hub Settings", "Hub Settings", "company_email"),
'name': obj["remote_id"],
'status': obj["status"]
})
if response:
frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 0)
else:
frappe.throw("Unable to update remote activity")
@frappe.whitelist()
def get_item_favourites(start=0, limit=20, fields=["*"], order_by=None):

View File

@@ -35,7 +35,7 @@
"mapping_name": "Item to Hub Item",
"mapping_type": "Push",
"migration_id_field": "hub_sync_id",
"modified": "2018-07-27 21:52:52.383842",
"modified": "2018-07-28 22:25:35.289335",
"modified_by": "cave@aperture.com",
"name": "Item to Hub Item",
"owner": "Administrator",

View File

@@ -1,18 +1,19 @@
{
"creation": "2017-09-07 11:39:38.445902",
"docstatus": 0,
"doctype": "Data Migration Plan",
"idx": 1,
"creation": "2017-09-07 11:39:38.445902",
"docstatus": 0,
"doctype": "Data Migration Plan",
"idx": 1,
"mappings": [
{
"enabled": 1,
"enabled": 1,
"mapping": "Item to Hub Item"
}
],
"modified": "2018-07-27 21:52:52.324025",
"modified_by": "cave@aperture.com",
"module": "Hub Node",
"name": "Hub Sync",
"owner": "Administrator",
"plan_name": "Hub Sync"
}
],
"modified": "2018-07-28 22:25:35.216172",
"modified_by": "cave@aperture.com",
"module": "Hub Node",
"name": "Hub Sync",
"owner": "Administrator",
"plan_name": "Hub Sync",
"postprocess_method": "erpnext.hub_node.item_sync_postprocess"
}

View File

@@ -2,7 +2,7 @@
# For license information, please see license.txt
from __future__ import unicode_literals
import frappe, requests, json
import frappe, requests, json, time
from frappe.model.document import Document
from frappe.utils import add_years, now, get_datetime, get_datetime_str
@@ -28,19 +28,23 @@ class HubSettings(Document):
def get_hub_url(self):
return hub_url
def sync(self):
def sync(self, remote_id):
"""Create and execute Data Migration Run for Hub Sync plan"""
frappe.has_permission('Hub Settings', throw=True)
doc = frappe.get_doc({
'doctype': 'Data Migration Run',
'data_migration_plan': 'Hub Sync',
'data_migration_connector': 'Hub Connector',
}).insert()
if remote_id:
doc = frappe.get_doc({
'doctype': 'Data Migration Run',
'data_migration_plan': 'Hub Sync',
'data_migration_connector': 'Hub Connector',
'remote_id': remote_id
}).insert()
# self.sync_in_progress = 1
doc.run()
# self.sync_in_progress = 0
self.sync_in_progress = 1
# time.sleep(10)
doc.run()
else:
frappe.throw("No remote ID specified")
def register(self):
""" Create a User on hub.erpnext.org and return username/password """