scheduler_tasks.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import os
  2. from loguru import logger
  3. from parsers.log import process_logs
  4. from services.notifications.notifications import (
  5. has_remote_commits_with_messages,
  6. set_commit_notifications,
  7. )
  8. from services.quota.quota_scheduler import register_quota_scheduler_tasks
  9. from services.system.metrics_service import MetricsService
  10. def register_scheduler_tasks(scheduler):
  11. """Registers global scheduler tasks for the app."""
  12. @scheduler.task(
  13. "interval", id="check_notifications", minutes=30, misfire_grace_time=1800
  14. )
  15. def check_notifications_task():
  16. repo_path = os.path.dirname(os.path.abspath(__file__))
  17. has_updates, messages = has_remote_commits_with_messages(repo_path)
  18. set_commit_notifications(has_updates, messages)
  19. @scheduler.task("interval", id="do_job_1", seconds=30, misfire_grace_time=900)
  20. def init_scheduler():
  21. log_file = os.getenv("SQUID_LOG", "/var/log/squid/access.log")
  22. logger.info(f"Scheduler for file log: {log_file}")
  23. if not os.path.exists(log_file):
  24. logger.error(f"Log file not found: {log_file}")
  25. return
  26. process_logs(log_file)
  27. @scheduler.task("interval", id="cleanup_metrics", hours=1, misfire_grace_time=3600)
  28. def cleanup_old_metrics():
  29. try:
  30. success = MetricsService.cleanup_old_metrics()
  31. if success:
  32. logger.info("Cleanup of old metrics completed successfully")
  33. else:
  34. logger.warning("Error during cleanup of old metrics")
  35. except Exception as e:
  36. logger.error(f"Error in metrics cleanup task: {e}")
  37. # Register quota worker tasks along with global tasks.
  38. register_quota_scheduler_tasks(scheduler)