Skip to content

Manage single mongodb connection for bot - #828

Merged
amirbiron merged 2 commits into
mainfrom
cursor/manage-single-mongodb-connection-for-bot-3053
Oct 17, 2025
Merged

amirbiron merged 2 commits into
mainfrom
cursor/manage-single-mongodb-connection-for-bot-3053

Conversation

@amirbiron

@amirbiron amirbiron commented Oct 17, 2025 •

Copy link
Copy Markdown
Owner

✨ תיאור קצר

תיקון קריסות הבוט הנגרמות מריבוי חיבורי MongoDB שלא נסגרים. השינוי מאחד את כל הגישה ל-MongoClient לחיבור יחיד גלובלי עם סגירה אוטומטית, ומסיר יצירת חיבורים בזמן import.

📦 שינויים עיקריים

  • קוד (Backend)
  • בוט טלגרם
  • מסד נתונים/מיגרציות
  • תיעוד (docs/)
  • DevOps/CI/CD

פירוט נקודות (רשימת תבליטים):

  • activity_reporter.py: הוספת get_mongo_client (singleton), close_mongo_client עם atexit.register, ושימוש בחיבור יחיד עבור SimpleActivityReporter.
  • bot_handlers.py, conversation_handlers.py, refactor_handlers.py: הוסרו קריאות ל-create_reporter בזמן import. הוגדר _NoopReporter ופונקציית set_activity_reporter להזרקה בזמן ריצה.
  • main.py: יצירת ה-SimpleActivityReporter והזרקתו למודולים התלויים בו בזמן ריצה (בתוך CodeKeeperBot.__init__), עם תמיכה ב-DISABLE_ACTIVITY_REPORTER. נוספו בדיקות if reporter is not None לפני קריאות ל-report_activity.

🧪 בדיקות

  • הקוד נבדק מקומית לוודא שאין ייבוא מעגלי ושהלוגיקה של הזרקת ה-reporter עובדת כמצופה.
  • נבדק שהקוד שומר על תאימות מלאה לסביבת Render.
  • Unit
  • Integration
  • Manual

🧪 בדיקות נדרשות ב‑PR

  • 🔍 Code Quality & Security
  • 🧪 Unit Tests (3.11)
  • 🧪 Unit Tests (3.12)

📝 סוג שינוי

  • feat: פיצ'ר חדש
  • fix: תיקון באג
  • docs: שינוי תיעוד בלבד
  • refactor: שינוי קוד ללא שינוי התנהגות
  • perf: שיפור ביצועים
  • chore/ci: תשתית/CI
  • breaking change: שינוי שובר תאימות

✅ צ'קליסט

  • הקוד עוקב אחרי הסגנון (Black/isort/flake8/mypy)
  • בדיקות רצות ועוברות (נדרש CI)
  • תיעוד עודכן (README/Docs)
  • אין סודות/מפתחות בקוד
  • אין מחיקות מסוכנות/פעולות על root (ראו .cursorrules)
  • הודעת הקומיט תואמת Conventional Commits (ע"פ הטבלה)
  • CHANGELOG עודכן אם נדרש
  • כל ה‑Required Checks לעיל ירוקים
  • צילום/וידאו UI מצורף אם רלוונטי

🧩 השפעות/סיכונים

  • השפעה חיובית: שיפור משמעותי ביציבות הבוט, מניעת קריסות ספונטניות, והפחתת צריכת משאבים עקב ניהול חיבורי DB יעיל יותר.
  • סיכון נמוך: שינוי מהותי בדרך ניהול חיבורי DB, אך נבדק שאין ייבוא מעגלי והלוגיקה מופרדת היטב.

🔗 קישורים

  • Issues קשורים: # (הבעיה דווחה בעל פה)
  • Docs Preview:
  • מסמכים/מפרטים רלוונטיים:

🧯 סיכון / החזרה לאחור (Rollback)

  • במקרה של תקלה, ניתן לחזור לגרסה הקודמת של הקוד.

Open in Cursor Open in Web


Note

Consolidates MongoDB to a single shared client and switches all bot modules to runtime-injected activity reporter with safe no-op fallback.

  • Activity Reporting / MongoDB:
    • Add get_mongo_client singleton and close_mongo_client with atexit in activity_reporter.py; use shared client in SimpleActivityReporter.
  • Runtime Reporter Injection:
    • Replace import-time create_reporter in bot_handlers.py, conversation_handlers.py, refactor_handlers.py with _NoopReporter + set_activity_reporter injection points.
    • Wrap report_activity calls with safe guards/try-except.
  • App Initialization (main.py):
    • Create reporter at runtime (env-driven, supports DISABLE_ACTIVITY_REPORTER) and inject via set_*_activity_reporter into dependent modules.
    • Add None checks before calling report_activity across commands/handlers.

Written by Cursor Bugbot for commit cd824f3. This will update automatically on new commits. Configure here.

This change defers reporter initialization until runtime, preventing multiple MongoDB connections and ensuring proper handling in different environments.

Co-authored-by: amirbiron <amirbiron@gmail.com>
@cursor

cursor Bot commented Oct 17, 2025

Copy link
Copy Markdown
Contributor

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@github-actions

github-actions Bot commented Oct 17, 2025 •

Copy link
Copy Markdown
Contributor

🧯 Dangerous deletes guard report

Policy: see .cursorrules — dangerous deletions are blocked unless wrapped safely.

Summary:

  • Flagged findings (blocking): 0
    0
  • Excluded matches (not blocking): 5
  • Total matches (all files): 14

Flagged findings (file:line:snippet):

Excluded matches (by path pattern):
./Dockerfile:32: rm -rf /var/lib/apt/lists/*
./Dockerfile:73: rm -rf /var/lib/apt/lists/*
./README.md:737:find . -name "pycache" -exec rm -rf {} +
./docs/DOCUMENTATION_GUIDE.md:453:rm -rf _build
./docs/Makefile:24: rm -rf $(BUILDDIR)

Co-authored-by: amirbiron <amirbiron@gmail.com>
@amirbiron
amirbiron marked this pull request as ready for review October 17, 2025 01:51
@codecov

codecov Bot commented Oct 17, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 60.63830% with 37 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
main.py 57.14% 13 Missing and 5 partials ⚠️
activity_reporter.py 33.33% 10 Missing ⚠️
conversation_handlers.py 63.63% 8 Missing ⚠️
refactor_handlers.py 88.88% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@amirbiron
amirbiron merged commit cd32bc9 into main Oct 17, 2025
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants