Add automatic sqlalchemy commit

This commit is contained in:
Tulir Asokan 2018-10-22 02:01:06 +03:00
parent 894c5df07b
commit 038fbc43f1

View File

@ -68,14 +68,25 @@ for plugin in plugins:
signal.signal(signal.SIGINT, signal.default_int_handler) signal.signal(signal.SIGINT, signal.default_int_handler)
signal.signal(signal.SIGTERM, 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: try:
loop.run_until_complete(asyncio.gather( loop.run_until_complete(asyncio.gather(
server.start(), server.start(),
*[plugin.start() for plugin in plugins])) *[plugin.start() for plugin in plugins]))
log.debug("Startup actions complete, running forever") log.debug("Startup actions complete, running forever")
loop.run_until_complete(periodic_commit())
loop.run_forever() loop.run_forever()
except KeyboardInterrupt: except KeyboardInterrupt:
log.debug("Interrupt received, stopping HTTP clients/servers and saving database") log.debug("Interrupt received, stopping HTTP clients/servers and saving database")
stop = True
for client in Client.cache.values(): for client in Client.cache.values():
client.stop() client.stop()
db_session.commit() db_session.commit()