Skip to content

Commit

Permalink
Merge pull request #7 from Half-Shot/hs/switch-metric-check-to-periodic
Browse files Browse the repository at this point in the history
Don't block startup on endless user check
  • Loading branch information
Half-Shot authored Nov 1, 2021
2 parents b2880a5 + 8d98a0f commit 5beffbf
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions mautrix_telegram/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class TelegramBridge(Bridge):
periodic_active_metrics_task: asyncio.Task
is_blocked: bool = False

periodic_sync_task: asyncio.Task

def prepare_db(self) -> None:
super().prepare_db()
init_db(self.db)
Expand Down Expand Up @@ -101,7 +103,10 @@ def prepare_bridge(self) -> None:
self.add_startup_actions(self.bot.start())
if self.config["bridge.resend_bridge_info"]:
self.add_startup_actions(self.resend_bridge_info())
self.add_startup_actions(self._loop_active_puppet_metric())

# Explicitly not a startup_action, as startup_actions block startup
self.periodic_sync_task = asyncio.create_task(self._loop_active_puppet_metric())


async def resend_bridge_info(self) -> None:
self.config["bridge.resend_bridge_info"] = False
Expand All @@ -112,6 +117,7 @@ async def resend_bridge_info(self) -> None:
self.log.info("Finished re-sending bridge info state events")

def prepare_stop(self) -> None:
self.periodic_sync_task.cancel()
for puppet in Puppet.by_custom_mxid.values():
puppet.stop()
self.shutdown_actions = (user.stop() for user in User.by_tgid.values())
Expand Down Expand Up @@ -157,15 +163,14 @@ async def _loop_active_puppet_metric(self) -> None:
await asyncio.sleep(ACTIVE_USER_METRICS_INTERVAL_S)
except asyncio.CancelledError:
return
self.log.info("Executing periodic active puppet metric check")
self.log.debug("Executing periodic active puppet metric check")
try:
await self._update_active_puppet_metric()
except asyncio.CancelledError:
return
except Exception as e:
self.log.exception(f"Error while checking: {e}")



async def manhole_global_namespace(self, user_id: UserID) -> Dict[str, Any]:
return {
**await super().manhole_global_namespace(user_id),
Expand Down

0 comments on commit 5beffbf

Please sign in to comment.