Features: 1) Add _in_celery_task utility function to detect task execution context; 2) Enhance vendor integration retrieval with context-specific logging (async/sync); 3) Improve error handling in vendor integration loading with exception logging;
Fixes: None; Extra: 1) Refactor vendor filtering logic for clarity; 2) Add imports for logging, contextlib, and Celery utilities;
This commit is contained in:
parent
45d20bc17e
commit
202964aea6
1 changed files with 27 additions and 15 deletions
|
|
@ -1,27 +1,39 @@
|
||||||
|
import logging
|
||||||
|
from contextlib import suppress
|
||||||
from typing import Type
|
from typing import Type
|
||||||
|
|
||||||
|
from celery import current_task
|
||||||
|
from celery.utils.log import get_task_logger
|
||||||
|
|
||||||
from core.models import Vendor
|
from core.models import Vendor
|
||||||
from core.vendors import AbstractVendor
|
from core.vendors import AbstractVendor
|
||||||
from evibes.utils.misc import create_object
|
from evibes.utils.misc import create_object
|
||||||
|
|
||||||
|
sync_logger = logging.getLogger("django")
|
||||||
|
async_logger = get_task_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def _in_celery_task() -> bool:
|
||||||
|
task = current_task
|
||||||
|
with suppress(Exception):
|
||||||
|
return bool(task and getattr(task, "request", None) and getattr(task.request, "id", None))
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def get_vendors_integrations(name: str | None = None) -> list[Type[AbstractVendor]]:
|
def get_vendors_integrations(name: str | None = None) -> list[Type[AbstractVendor]]:
|
||||||
vendors_integrations: list[Type[AbstractVendor]] = []
|
vendors_integrations: list[Type[AbstractVendor]] = []
|
||||||
vendors = (
|
|
||||||
Vendor.objects.filter(
|
vendors = Vendor.objects.filter(is_active=True, integration_path__isnull=False)
|
||||||
is_active=True,
|
if name:
|
||||||
integration_path__isnull=False,
|
vendors = vendors.filter(name=name)
|
||||||
name=name,
|
|
||||||
)
|
logger = async_logger if _in_celery_task() else sync_logger
|
||||||
if name
|
|
||||||
else Vendor.objects.filter(
|
|
||||||
is_active=True,
|
|
||||||
integration_path__isnull=False,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
for vendor in vendors:
|
for vendor in vendors:
|
||||||
if vendor.integration_path:
|
try:
|
||||||
module_name = ".".join(vendor.integration_path.split(".")[:-1])
|
module_name, class_name = vendor.integration_path.rsplit(".", 1)
|
||||||
class_name = vendor.integration_path.split(".")[-1]
|
|
||||||
vendors_integrations.append(create_object(module_name, class_name))
|
vendors_integrations.append(create_object(module_name, class_name))
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning("Couldn't load integration for vendor %s: %s", vendor.name, e)
|
||||||
|
|
||||||
return vendors_integrations
|
return vendors_integrations
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue