l2_due.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """Return exit 0 when the full collection interval is due.
  4. The shell scheduler wakes every five minutes. This gate reads the same
  5. `full_interval_min` setting as the web config, so changing that single field
  6. changes both the full collection and the history-snapshot cadence. A stamp
  7. file is updated only after a complete run, so a failed run is retried on the
  8. next scheduler tick.
  9. """
  10. import os
  11. import sys
  12. import time
  13. HERE = os.path.dirname(os.path.abspath(__file__))
  14. sys.path.insert(0, HERE)
  15. import l2cfg # noqa: E402
  16. def main():
  17. try:
  18. cfg = l2cfg.config()
  19. interval = max(5, int(cfg.get('full_interval_min') or 5))
  20. except Exception as exc:
  21. # Never let a temporary config/DB read failure block collection.
  22. print('due check config fallback: %s' % exc)
  23. interval = 5
  24. stamp = os.environ.get('L2_FULL_STAMP') or '/tmp/l2_full_last'
  25. try:
  26. last = os.path.getmtime(stamp)
  27. except OSError:
  28. last = 0
  29. if time.time() - last >= interval * 60:
  30. return 0
  31. return 1
  32. if __name__ == '__main__':
  33. sys.exit(main())