Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: batch and commit inventory updates to shopify #233

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 28 additions & 26 deletions ecommerce_integrations/shopify/inventory.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from collections import Counter
from typing import Dict

import frappe
from frappe.utils import cint, now
from frappe.utils import cint, create_batch, now
from pyactiveresource.connection import ResourceNotFound
from shopify.resources import InventoryLevel, Variant

Expand Down Expand Up @@ -40,30 +39,33 @@ def update_inventory_on_shopify() -> None:
def upload_inventory_data_to_shopify(inventory_levels, warehous_map) -> None:
synced_on = now()

for d in inventory_levels:
d.shopify_location_id = warehous_map[d.warehouse]

try:
variant = Variant.find(d.variant_id)
inventory_id = variant.inventory_item_id

InventoryLevel.set(
location_id=d.shopify_location_id,
inventory_item_id=inventory_id,
# shopify doesn't support fractional quantity
available=cint(d.actual_qty) - cint(d.reserved_qty),
)
update_inventory_sync_status(d.ecom_item, time=synced_on)
d.status = "Success"
except ResourceNotFound:
# Variant or location is deleted, mark as last synced and ignore.
update_inventory_sync_status(d.ecom_item, time=synced_on)
d.status = "Not Found"
except Exception as e:
d.status = "Failed"
d.failure_reason = str(e)

_log_inventory_update_status(inventory_levels)
for inventory_sync_batch in create_batch(inventory_levels, 50):
for d in inventory_sync_batch:
d.shopify_location_id = warehous_map[d.warehouse]

try:
variant = Variant.find(d.variant_id)
inventory_id = variant.inventory_item_id

InventoryLevel.set(
location_id=d.shopify_location_id,
inventory_item_id=inventory_id,
# shopify doesn't support fractional quantity
available=cint(d.actual_qty) - cint(d.reserved_qty),
)
update_inventory_sync_status(d.ecom_item, time=synced_on)
d.status = "Success"
except ResourceNotFound:
# Variant or location is deleted, mark as last synced and ignore.
update_inventory_sync_status(d.ecom_item, time=synced_on)
d.status = "Not Found"
except Exception as e:
d.status = "Failed"
d.failure_reason = str(e)

frappe.db.commit()

_log_inventory_update_status(inventory_sync_batch)


def _log_inventory_update_status(inventory_levels) -> None:
Expand Down