Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /DeepselSystems_deepsel-cms /backend /execute_cron.py
| from contextlib import contextmanager | |
| from settings import DATABASE_URL | |
| import psycopg2 | |
| from apps.core.utils.models_pool import models_pool | |
| from db import get_db_context | |
| import logging | |
| import os | |
| from datetime import datetime, UTC | |
| import asyncio | |
| logging.basicConfig( | |
| level=os.environ.get("LOG_LEVEL", "INFO"), | |
| format="%(levelname)s: [%(asctime)s] %(name)s %(message)s", | |
| datefmt="%d-%m-%Y %H:%M:%S", | |
| ) | |
| logger = logging.getLogger("CRON") | |
| def get_connection(): | |
| conn = psycopg2.connect(DATABASE_URL) | |
| try: | |
| yield conn | |
| finally: | |
| conn.close() | |
| def get_cursor(commit=False): | |
| with get_connection() as connection: | |
| cursor = connection.cursor() | |
| try: | |
| yield cursor | |
| if commit: | |
| connection.commit() | |
| finally: | |
| cursor.close() | |
| def obtain_lock(lock_id: int): | |
| with get_cursor(commit=True) as cursor: | |
| cursor.execute("SELECT pg_try_advisory_lock(%s);", (lock_id,)) | |
| (obtained,) = cursor.fetchone() | |
| return obtained | |
| async def main(): | |
| with get_db_context() as db: | |
| CronModel = models_pool["cron"] | |
| crons = ( | |
| db.query(CronModel) | |
| .filter( | |
| CronModel.enabled == True, | |
| CronModel.active == True, | |
| CronModel.next_run <= datetime.now(UTC), | |
| ) | |
| .all() | |
| ) | |
| for cron in crons: | |
| if obtain_lock(cron.id): | |
| try: | |
| logger.info(f"Executing cron: {cron.name}") | |
| await cron.execute(db) | |
| finally: | |
| with get_cursor(commit=True) as cursor: | |
| cursor.execute("SELECT pg_advisory_unlock(%s);", (cron.id,)) | |
| else: | |
| logger.warning( | |
| f"Could not obtain lock for cron: {cron.name}, another instance maybe running" | |
| ) | |
| if __name__ == "__main__": | |
| asyncio.run(main()) | |
Xet Storage Details
- Size:
- 2.01 kB
- Xet hash:
- d9f66f2fd4bf7c0485beb7321a361ba369fc13a14960d86a17eaa42846f4f755
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.