mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
feat(generator): add poll cron that enqueues on payload-hash change
This commit is contained in:
@@ -0,0 +1,45 @@
|
|||||||
|
"""Generator cron: poll fetcher for payload-hash changes, enqueue rebuilds.
|
||||||
|
|
||||||
|
Run as its own container: CMD python -m src.cron
|
||||||
|
"""
|
||||||
|
import logging
|
||||||
|
import time
|
||||||
|
|
||||||
|
from . import config, db, fetcher_client
|
||||||
|
|
||||||
|
log = logging.getLogger("generator.cron")
|
||||||
|
|
||||||
|
|
||||||
|
def tick() -> dict:
|
||||||
|
enqueued = 0
|
||||||
|
failed = 0
|
||||||
|
for username in db.list_enrolled():
|
||||||
|
try:
|
||||||
|
info = db.get_settings(username)
|
||||||
|
current = info.get("last_fetcher_payload_hash")
|
||||||
|
r = fetcher_client.get_data(username)
|
||||||
|
latest = r.get("payload_hash")
|
||||||
|
if latest and latest != current:
|
||||||
|
db.enqueue_build(username)
|
||||||
|
db.set_last_fetcher_hash(username, latest)
|
||||||
|
enqueued += 1
|
||||||
|
except Exception as e:
|
||||||
|
log.warning("poll failed for %s: %s", username, e)
|
||||||
|
failed += 1
|
||||||
|
return {"enqueued": enqueued, "failed": failed}
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s")
|
||||||
|
db.init_dbs()
|
||||||
|
interval_s = config.POLL_INTERVAL_MINUTES * 60
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
log.info("tick: %s", tick())
|
||||||
|
except Exception:
|
||||||
|
log.exception("tick failed")
|
||||||
|
time.sleep(interval_s)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
import os, tempfile, pytest
|
||||||
|
from unittest.mock import patch
|
||||||
|
from src import db as dbmod
|
||||||
|
from src import cron as cronmod
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def tmp_dbs(monkeypatch):
|
||||||
|
with tempfile.TemporaryDirectory() as d:
|
||||||
|
monkeypatch.setattr(dbmod, "SETTINGS_DB_PATH", os.path.join(d, "s.db"))
|
||||||
|
monkeypatch.setattr(dbmod, "WIDGETS_DB_PATH", os.path.join(d, "w.db"))
|
||||||
|
dbmod.init_dbs()
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
def test_tick_enqueues_build_when_hash_changed(tmp_dbs):
|
||||||
|
dbmod.enroll("alice", {"theme": "dark"})
|
||||||
|
dbmod.set_last_fetcher_hash("alice", "old")
|
||||||
|
with dbmod._settings_conn() as c:
|
||||||
|
c.execute("DELETE FROM jobs")
|
||||||
|
c.commit()
|
||||||
|
with patch("src.cron.fetcher_client.get_data",
|
||||||
|
return_value={"data": {}, "payload_hash": "new"}):
|
||||||
|
stats = cronmod.tick()
|
||||||
|
assert stats["enqueued"] == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_tick_skips_when_hash_unchanged(tmp_dbs):
|
||||||
|
dbmod.enroll("alice", {"theme": "dark"})
|
||||||
|
dbmod.set_last_fetcher_hash("alice", "same")
|
||||||
|
with dbmod._settings_conn() as c:
|
||||||
|
c.execute("DELETE FROM jobs")
|
||||||
|
c.commit()
|
||||||
|
with patch("src.cron.fetcher_client.get_data",
|
||||||
|
return_value={"data": {}, "payload_hash": "same"}):
|
||||||
|
stats = cronmod.tick()
|
||||||
|
assert stats["enqueued"] == 0
|
||||||
Reference in New Issue
Block a user