Acquire a lock to prevent concurrent task execution.
(task_name, id)
| 290 | return cls._pubsub_client |
| 291 | |
| 292 | def acquire_task_lock(task_name, id): |
| 293 | """Acquire a lock to prevent concurrent task execution.""" |
| 294 | redis_client = RedisClient.get_client() |
| 295 | lock_id = f"task_lock_{task_name}_{id}" |
| 296 | |
| 297 | # Use the Redis SET command with NX (only set if not exists) and EX (set expiration) |
| 298 | lock_acquired = redis_client.set(lock_id, "locked", ex=300, nx=True) |
| 299 | |
| 300 | if not lock_acquired: |
| 301 | logger.warning(f"Lock for {task_name} and id={id} already acquired. Task will not proceed.") |
| 302 | |
| 303 | return lock_acquired |
| 304 | |
| 305 | def release_task_lock(task_name, id): |
| 306 | """Release the lock after task execution.""" |