From 038fbc43f1a46533b598feb3b7315697034ab3e0 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 22 Oct 2018 02:01:06 +0300 Subject: [PATCH] Add automatic sqlalchemy commit --- maubot/__main__.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/maubot/__main__.py b/maubot/__main__.py index fce8080..fe9f168 100644 --- a/maubot/__main__.py +++ b/maubot/__main__.py @@ -68,14 +68,25 @@ for plugin in plugins: signal.signal(signal.SIGINT, signal.default_int_handler) signal.signal(signal.SIGTERM, signal.default_int_handler) +stop = False + + +async def periodic_commit(): + while not stop: + await asyncio.sleep(60) + db_session.commit() + + try: loop.run_until_complete(asyncio.gather( server.start(), *[plugin.start() for plugin in plugins])) log.debug("Startup actions complete, running forever") + loop.run_until_complete(periodic_commit()) loop.run_forever() except KeyboardInterrupt: log.debug("Interrupt received, stopping HTTP clients/servers and saving database") + stop = True for client in Client.cache.values(): client.stop() db_session.commit()