lizerk f15e8aca3f feat(ldap): add LDAP sync retry queue with Celery
- Add LdapSyncQueue model for deferred LDAP operations
- Add Celery tasks: process_ldap_queue_item, process_ldap_queue
- Refactor all LDAP signals (employee, department, group, position)
  to enqueue operations on DirectoryLdapError/DirectoryServiceError
- Add _queue.py helper for enqueuing with automatic Celery dispatch
- Add Celery Beat periodic task (every 60s) for queue processing
- Add management command: ldap_queue (status/retry/cleanup)
- Add migration 0043_ldap_sync_queue
- Add 18 tests covering model, tasks, signals, and enqueue logic
- Include LDAP services refactoring (base_service, constants,
  user_login_service, user_mapper_service, user_password_service,
  phone_utils)
2026-03-19 15:07:34 +03:00

34 lines
1.2 KiB
Python

"""Вспомогательный модуль для постановки LDAP-операций в очередь retry."""
import logging
logger = logging.getLogger(__name__)
def _enqueue(operation: str, model_name: str, object_pk, payload: dict):
"""Ставит неудавшуюся LDAP-операцию в очередь для отложенного retry.
Создаёт запись LdapSyncQueue и сразу пытается запустить Celery-задачу.
Если Celery недоступен — periodic task process_ldap_queue подберёт позже.
"""
from employees.models import LdapSyncQueue
item = LdapSyncQueue.objects.create(
operation=operation,
model_name=model_name,
object_pk=str(object_pk),
payload=payload,
)
try:
from employees.tasks import process_ldap_queue_item
process_ldap_queue_item.delay(item.pk)
except Exception:
pass # Celery недоступен — periodic task подберёт позже
logger.info(
"Enqueued LDAP operation %s for %s:%s (queue_id=%d)",
operation, model_name, object_pk, item.pk,
)
return item