async def create_triggers(self): """ Drain the to_create queue and create all triggers that have been requested in the DB that we don't yet have. """ while self.to_create: trigger_id, trigger_instance = self.to_create.popleft() if trigger_id not in self.triggers: self.triggers[trigger_id] = { "task": create_task(self.run_trigger(trigger_id, trigger_instance)), "name": f"{trigger_instance!r} (ID {trigger_id})", "events": 0, } else: self.log.warning("Trigger %s had insertion attempted twice", trigger_id)
async def arun(self): """ Main (asynchronous) logic loop. The loop in here runs trigger addition/deletion/cleanup. Actual triggers run in their own separate coroutines. """ watchdog = create_task(self.block_watchdog()) last_status = time.time() while not self.stop: # Run core logic await self.create_triggers() await self.delete_triggers() await self.cleanup_finished_triggers() # Sleep for a bit await asyncio.sleep(1) # Every minute, log status if time.time() - last_status >= 60: self.log.info("%i triggers currently running", len(self.triggers)) last_status = time.time() # Wait for watchdog to complete await watchdog