Transactions.

This commit is contained in:
retoor 2025-03-27 20:41:04 +01:00
parent 13ce09a5c5
commit 145373399d
2 changed files with 25 additions and 6 deletions
src/snek

View File

@ -61,6 +61,7 @@ class Application(BaseApplication):
middlewares=middlewares, template_path=self.template_path, *args, **kwargs middlewares=middlewares, template_path=self.template_path, *args, **kwargs
) )
session_setup(self, EncryptedCookieStorage(SESSION_KEY)) session_setup(self, EncryptedCookieStorage(SESSION_KEY))
self.tasks = []
self._middlewares.append(session_middleware) self._middlewares.append(session_middleware)
self.jinja2_env.add_extension(MarkdownExtension) self.jinja2_env.add_extension(MarkdownExtension)
self.jinja2_env.add_extension(LinkifyExtension) self.jinja2_env.add_extension(LinkifyExtension)
@ -68,12 +69,28 @@ class Application(BaseApplication):
self.jinja2_env.add_extension(EmojiExtension) self.jinja2_env.add_extension(EmojiExtension)
self.setup_router() self.setup_router()
self.cache = Cache(self) self.cache = Cache(self)
self.services = get_services(app=self) self.services = get_services(app=self)
self.mappers = get_mappers(app=self) self.mappers = get_mappers(app=self)
self.on_startup.append(self.prepare_database) self.on_startup.append(self.prepare_database)
async def create_task(self, task):
self.tasks.append(task)
async def task_runner(self):
while True:
await asyncio.sleep(0.1)
task = None
try:
task = self.tasks.pop(0)
except IndexError:
continue
try:
await task
except:
print(ex)
async def prepare_database(self,app): async def prepare_database(self,app):
self.db.query("PRAGMA journal_mode=WAL") self.db.query("PRAGMA journal_mode=WAL")
self.db.query("PRAGMA syncnorm=off") self.db.query("PRAGMA syncnorm=off")
@ -89,6 +106,7 @@ class Application(BaseApplication):
pass pass
await app.services.drive.prepare_all() await app.services.drive.prepare_all()
self.loop.create_task(self.task_runner())
def setup_router(self): def setup_router(self):
self.router.add_get("/", IndexView) self.router.add_get("/", IndexView)

View File

@ -21,7 +21,10 @@ class ChatService(BaseService):
user = await self.services.user.get(uid=user_uid) user = await self.services.user.get(uid=user_uid)
await self.services.notification.create_channel_message(channel_message_uid) await self.services.notification.create_channel_message(channel_message_uid)
sent_to_count = await self.services.socket.broadcast(channel_uid, dict( channel['last_message_on'] = now()
await self.services.channel.save(channel)
await self.app.create_task(self.services.socket.broadcast(channel_uid, dict(
message=channel_message["message"], message=channel_message["message"],
html=channel_message["html"], html=channel_message["html"],
user_uid=user_uid, user_uid=user_uid,
@ -32,7 +35,5 @@ class ChatService(BaseService):
username=user['username'], username=user['username'],
uid=channel_message['uid'], uid=channel_message['uid'],
user_nick=user['nick'] user_nick=user['nick']
)) )))
channel['last_message_on'] = now() return True
await self.services.channel.save(channel)
return sent_to_count