[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):