diff --git a/src/snek/app.py b/src/snek/app.py index e14aa5f..a551e41 100644 --- a/src/snek/app.py +++ b/src/snek/app.py @@ -3,13 +3,13 @@ import logging import pathlib import ssl import uuid +import signal from datetime import datetime from contextlib import asynccontextmanager from snek import snode from snek.view.threads import ThreadsView from snek.system.ads import AsyncDataSet - logging.basicConfig(level=logging.DEBUG) from concurrent.futures import ThreadPoolExecutor from ipaddress import ip_address @@ -31,11 +31,7 @@ from snek.sgit import GitApplication from snek.sssh import start_ssh_server from snek.system import http from snek.system.cache import Cache -from snek.system.stats import ( - middleware as stats_middleware, - create_stats_structure, - stats_handler, -) +from snek.system.stats import middleware as stats_middleware, create_stats_structure, stats_handler from snek.system.markdown import MarkdownExtension from snek.system.middleware import auth_middleware, cors_middleware, csp_middleware from snek.system.profiler import profiler_handler @@ -47,11 +43,7 @@ from snek.system.template import ( ) from snek.view.about import AboutHTMLView, AboutMDView from snek.view.avatar import AvatarView -from snek.view.channel import ( - ChannelAttachmentView, - ChannelAttachmentUploadView, - ChannelView, -) +from snek.view.channel import ChannelAttachmentView,ChannelAttachmentUploadView, ChannelView from snek.view.docs import DocsHTMLView, DocsMDView from snek.view.drive import DriveApiView, DriveView from snek.view.channel import ChannelDriveApiView @@ -183,12 +175,13 @@ class Application(BaseApplication): self.on_startup.append(self.prepare_asyncio) self.on_startup.append(self.start_user_availability_service) self.on_startup.append(self.start_ssh_server) - # self.on_startup.append(self.prepare_database) + #self.on_startup.append(self.prepare_database) async def prepare_stats(self, app): - app["stats"] = create_stats_structure() + app['stats'] = create_stats_structure() print("Stats prepared", flush=True) + @property def uptime_seconds(self): return (datetime.now() - self.time_start).total_seconds() @@ -232,10 +225,10 @@ class Application(BaseApplication): # app.loop = asyncio.get_running_loop() app.executor = ThreadPoolExecutor(max_workers=200) app.loop.set_default_executor(self.executor) - # for sig in (signal.SIGINT, signal.SIGTERM): - # app.loop.add_signal_handler( - # sig, lambda: asyncio.create_task(self.services.container.shutdown()) - # ) + #for sig in (signal.SIGINT, signal.SIGTERM): + #app.loop.add_signal_handler( + # sig, lambda: asyncio.create_task(self.services.container.shutdown()) + #) async def create_task(self, task): await self.tasks.put(task) @@ -251,6 +244,7 @@ class Application(BaseApplication): print(ex) self.db.commit() + async def prepare_database(self, app): await self.db.query_raw("PRAGMA journal_mode=WAL") await self.db.query_raw("PRAGMA syncnorm=off") @@ -297,7 +291,9 @@ class Application(BaseApplication): self.router.add_view( "/channel/{channel_uid}/attachment.bin", ChannelAttachmentView ) - self.router.add_view("/channel/{channel_uid}/drive.json", ChannelDriveApiView) + self.router.add_view( + "/channel/{channel_uid}/drive.json", ChannelDriveApiView + ) self.router.add_view( "/channel/{channel_uid}/attachment.sock", ChannelAttachmentUploadView ) @@ -418,9 +414,9 @@ class Application(BaseApplication): ) try: - context["nonce"] = request["csp_nonce"] + context["nonce"] = request['csp_nonce'] except: - context["nonce"] = "?" + context['nonce'] = '?' rendered = await super().render_template(template, request, context) @@ -470,14 +466,14 @@ class Application(BaseApplication): @asynccontextmanager async def no_save(self): - stats = {"count": 0} - + stats = { + 'count': 0 + } async def patched_save(*args, **kwargs): await self.cache.set(args[0]["uid"], args[0]) - stats["count"] = stats["count"] + 1 + stats['count'] = stats['count'] + 1 print(f"save is ignored {stats['count']} times") return args[0] - save_original = self.services.channel_message.mapper.save self.services.channel_message.mapper.save = patched_save raised_exception = None @@ -490,7 +486,6 @@ class Application(BaseApplication): if raised_exception: raise raised_exception - app = Application(db_path="sqlite:///snek.db") diff --git a/src/snek/service/channel_message.py b/src/snek/service/channel_message.py index bbb870c..642f4e0 100644 --- a/src/snek/service/channel_message.py +++ b/src/snek/service/channel_message.py @@ -2,7 +2,6 @@ from snek.system.service import BaseService from snek.system.template import sanitize_html import time - class ChannelMessageService(BaseService): mapper_name = "channel_message" @@ -35,6 +34,7 @@ class ChannelMessageService(BaseService): time.sleep(0.1) print(ex, flush=True) + while True: changed = 0 async for message in self.find(is_final=False): @@ -72,7 +72,7 @@ class ChannelMessageService(BaseService): try: template = self.app.jinja2_env.get_template("message.html") model["html"] = template.render(**context) - model["html"] = sanitize_html(model["html"]) + model['html'] = sanitize_html(model['html']) except Exception as ex: print(ex, flush=True) @@ -99,9 +99,9 @@ class ChannelMessageService(BaseService): if not user: return {} - # if not message["html"].startswith("