diff options
| author | stuppie | 2026-11-04 12:11:03 -0600 |
|---|---|---|
| committer | stuppie | 2026-11-04 12:11:03 -0600 |
| commit | 671a7b1b02503cfa5c9aecd40c27a0c2729a285b (patch) | |
| tree | 6cb1bd7c33e3e1d243ee5ae74decc57faaa5742d /jb/flow/tasks.py | |
| parent | e99ec6b7e38113a2237dc1ff402d8f4337c651d5 (diff) | |
| download | amt-jb-671a7b1b02503cfa5c9aecd40c27a0c2729a285b.tar.gz amt-jb-671a7b1b02503cfa5c9aecd40c27a0c2729a285b.zip | |
strip out AMT
Diffstat (limited to 'jb/flow/tasks.py')
| -rw-r--r-- | jb/flow/tasks.py | 109 |
1 files changed, 0 insertions, 109 deletions
diff --git a/jb/flow/tasks.py b/jb/flow/tasks.py deleted file mode 100644 index c555021..0000000 --- a/jb/flow/tasks.py +++ /dev/null @@ -1,109 +0,0 @@ -import time -from typing import TypedDict, cast - -from generalresearch.config import is_debug - -from jb.decorators import AMTM, HM, HQM, HTM, LOG, pg_config -from jb.flow.maintenance import check_hit_status -from jb.flow.monitoring import emit_hit_event, write_hit_gauge -from jb.models.definitions import HitStatus -from jb.models.hit import Hit, HitQuestion, HitType - - -class HitRow(TypedDict): - amt_hit_id: str - amt_hit_type_id: str - - -def check_stale_hits(): - # Check live hits that haven't been modified in a long time. They may not - # be expired yet, but maybe something is wrong? - res = pg_config.execute_sql_query( - """ - SELECT amt_hit_id, amt_hit_type_id - FROM mtwerk_hit mh - JOIN mtwerk_hittype mht ON mht.id = mh.hit_type_id - WHERE status = %(status)s - ORDER BY modified_at - LIMIT 100;""", - params={"status": HitStatus.Assignable.value}, - ) - for hit in cast(list[HitRow], res): - LOG.info(f"check_stale_hits: {hit["amt_hit_id"]}") - check_hit_status( - amtm=AMTM, - amt_hit_id=hit["amt_hit_id"], - amt_hit_type_id=hit["amt_hit_type_id"], - reason="cleanup", - ) - - -def check_expired_hits(): - # Check live/assignable hits that are expired (based on AMT's expiration time) - res = pg_config.execute_sql_query( - """ - SELECT amt_hit_id, amt_hit_type_id - FROM mtwerk_hit mh - JOIN mtwerk_hittype mht ON mht.id = mh.hit_type_id - WHERE status = %(status)s - AND expiration < now() - LIMIT 100;""", - params={"status": HitStatus.Assignable.value}, - ) - for hit in cast(list[HitRow], res): - LOG.info(f"check_expired_hits: {hit["amt_hit_id"]}") - check_hit_status( - amtm=AMTM, - amt_hit_id=hit["amt_hit_id"], - amt_hit_type_id=hit["amt_hit_type_id"], - reason="expired", - ) - - -def create_hit_from_hittype(hit_type: HitType) -> Hit: - if is_debug(): - raise Exception("Handle AMT Sandbox issues.") - - else: - question = HQM.get_or_create( - HitQuestion(height=800, url="https://jamesbillings67.com/work/") - ) - - hit = AMTM.create_hit_with_hit_type(hit_type=hit_type, question=question) - HM.create(hit) - emit_hit_event(status=hit.status, amt_hit_type_id=hit.amt_hit_type_id) - return hit - - -def refill_hits() -> None: - - for hit_type in HTM.filter_active(): - assert hit_type.id - assert hit_type.amt_hit_type_id - - active_count = HM.get_active_count(hit_type_id=hit_type.id) - LOG.info( - f"HitType: {hit_type.amt_hit_type_id}, {hit_type.min_active=}, active_count={active_count}" - ) - write_hit_gauge( - status=HitStatus.Assignable, - amt_hit_type_id=hit_type.amt_hit_type_id, - cnt=active_count, - ) - if active_count < hit_type.min_active: - cnt_todo = hit_type.min_active - active_count - LOG.info(f"Refilling {cnt_todo} hits") - for _ in range(cnt_todo): - create_hit_from_hittype(hit_type) - - -def refill_hits_task(): - while True: - try: - check_expired_hits() - check_stale_hits() - refill_hits() - except Exception as e: - LOG.exception(e) - finally: - time.sleep(5 * 60) |
