Commit 37c8f923 authored by Fredrik Wendt's avatar Fredrik Wendt

Use count down latch to decide whether to snatch work

parent 7b14ae10
......@@ -14,8 +14,9 @@ load_dotenv()
login_user = os.environ['LOGIN_USER']
login_pass = os.environ['LOGIN_PASS']
def snatch_grading_work(headless=None):
def snatch_grading_work(headless=None) -> bool:
global log
work_snatched = False
if log is None:
log = logging.getLogger('snatcher')
......@@ -74,6 +75,7 @@ def snatch_grading_work(headless=None):
while True:
current_buttons = page.query_selector_all('span:text-is("Claim")')
if len(current_buttons) == claim_count - 1:
work_snatched = True
break
if time.time() - start_time > 30: # Timeout after 30 seconds
break
......@@ -91,6 +93,8 @@ def snatch_grading_work(headless=None):
playwright.stop()
log.info("Done snatching grading work")
return work_snatched
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
......
......@@ -13,7 +13,7 @@ development_mode = os.getenv('PYCHARM_HOSTED')
def main():
log = logging.getLogger('wrapper')
messages_seen = []
wait_n_messages_before_snatching = 0
log.info("Running main")
host = 'localhost' if development_mode else 'redis.wendt.vpn'
......@@ -25,7 +25,7 @@ def main():
while True:
now = datetime.datetime.now(datetime.timezone.utc).isoformat(sep=' ', timespec='seconds')
log.info(f"Starting to wait for message at {now}")
log.info(f"Waiting for messages ({wait_n_messages_before_snatching}) at {now}")
try:
message = pubsub.get_message(ignore_subscribe_messages=True, timeout=60.0)
if message:
......@@ -42,15 +42,15 @@ def main():
subject = data["headers"]["subject"]
log.info(f"Subject: {subject}")
if "III Grading Required" in subject:
if len(messages_seen) == 0:
log.info("Subject found - let's go snatching!")
snatch_grading_work()
log.info("Subject found - let's consider snatching!")
if wait_n_messages_before_snatching <= 0:
log.info("Yes, it's our time to shine - go go go!")
if snatch_grading_work():
log.info("Got new work, resetting wait to 4")
wait_n_messages_before_snatching = 4
else:
log.info("Not snatching this one, only every 4th")
messages_seen.append(message)
if len(messages_seen) == 4:
log.info("Clearing message log, snatching the next one")
messages_seen = []
wait_n_messages_before_snatching =- 1
log.info(f"Not snatching this one, awaiting another {wait_n_messages_before_snatching} messages before snatching")
else:
log.info("Ignoring this email")
except Exception as e:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment