Skip to content

Commit

Permalink
Adds try-except, messages log and remove retry from flows.update_vtex…
Browse files Browse the repository at this point in the history
…_products
  • Loading branch information
elitonzky committed Feb 29, 2024
1 parent fea9f0c commit eb25d47
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
2 changes: 0 additions & 2 deletions marketplace/clients/flows/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from django.conf import settings

from marketplace.clients.base import RequestClient
from marketplace.clients.decorators import retry_on_exception


class InternalAuthentication(RequestClient):
Expand Down Expand Up @@ -91,7 +90,6 @@ def update_status_catalog(self, flow_object_uuid, fba_catalog_id, is_active: boo
)
return response

@retry_on_exception()
def update_vtex_products(self, products, flow_object_uuid, dict_catalog):
data = {
"catalog": dict_catalog,
Expand Down
17 changes: 17 additions & 0 deletions marketplace/services/vtex/generic_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ def first_product_insert_with_catalog(cls, vtex_app: App, catalog_id: str):
wpp_cloud = cls._get_wpp_cloud(wpp_cloud_uuid)

catalog = cls._get_or_sync_catalog(wpp_cloud, catalog_id)
cls._delete_existing_feeds_ifexists(catalog)
cls._link_catalog_to_vtex_app_if_needed(catalog, vtex_app)

cls._send_insert_task(credentials, catalog)
Expand Down Expand Up @@ -338,6 +339,22 @@ def _link_catalog_to_vtex_app_if_needed(catalog, vtex_app):
f"Catalog {catalog.name} successfully linked to VTEX app: {vtex_app.uuid}."
)

@staticmethod
def _delete_existing_feeds_ifexists(catalog):
"""Deletes existing feeds linked to the catalog and logs their IDs."""
feeds = catalog.feeds.all()
total = feeds.count()
if total > 0:
print(f"Deleting {total} feed(s) linked to catalog {catalog.name}.")
for feed in feeds:
print(f"Deleting feed with ID {feed.facebook_feed_id}.")
feed.delete()
print(
f"All feeds linked to catalog {catalog.name} have been successfully deleted."
)
else:
print(f"No feeds linked to catalog {catalog.name} to delete.")

@staticmethod
def _send_insert_task(credentials, catalog):
from marketplace.celery import app as celery_app
Expand Down
31 changes: 24 additions & 7 deletions marketplace/wpp_products/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,26 +114,39 @@ def _sync_local_catalogs(self, all_catalogs_id, local_catalog_ids):

@celery_app.task(name="task_insert_vtex_products")
def task_insert_vtex_products(**kwargs):
print("Starting first product insert")
print("Starting task: 'task_insert_vtex_products'")
vtex_service = ProductInsertionService()
flows_service = FlowsService(FlowsClient())

credentials = kwargs.get("credentials")
catalog_uuid = kwargs.get("catalog_uuid")

catalog = Catalog.objects.get(uuid=catalog_uuid)
if not all([credentials, catalog_uuid]):
logger.error(
"Missing required parameters [credentials, catalog_uuid] for task_insert_vtex_products"
)
return

try:
catalog = Catalog.objects.get(uuid=catalog_uuid)
api_credentials = APICredentials(
app_key=credentials.get("app_key"),
app_token=credentials.get("app_token"),
domain=credentials.get("domain"),
app_key=credentials["app_key"],
app_token=credentials["app_token"],
domain=credentials["domain"],
)
print(f"Starting first product insert for catalog: {str(catalog.name)}")
products = vtex_service.first_product_insert(api_credentials, catalog)
if products is None:
print("There are no products to be shipped after processing the rules")
return

except Exception as e:
logger.exception(
f"An error occurred during the first insertion of vtex products for catalog {catalog_uuid}, {e}"
)
return

try:
dict_catalog = {
"name": catalog.name,
"facebook_catalog_id": catalog.facebook_catalog_id,
Expand All @@ -144,13 +157,17 @@ def task_insert_vtex_products(**kwargs):
print("Products created and sent to flows successfully")
except Exception as e:
logger.error(
f"Error on insert vtex products for catalog {str(catalog.uuid)}, {e}"
f"Error on send vtex products to flows for catalog {catalog_uuid}, {e}"
)

print(
f"First insertion into the catalog {catalog.name}-{catalog.facebook_catalog_id} was completed"
)


@celery_app.task(name="task_update_vtex_products")
def task_update_vtex_products(**kwargs):
print("Starting product update")
print("Starting task: 'task_update_vtex_products'")
vtex_base_service = VtexServiceBase()
flows_service = FlowsService(FlowsClient())

Expand Down

0 comments on commit eb25d47

Please sign in to comment.