diff --git a/src/snek/__main__.py b/src/snek/__main__.py index a2e433a..611e5b8 100644 --- a/src/snek/__main__.py +++ b/src/snek/__main__.py @@ -1,11 +1,11 @@ import pathlib import shutil import sqlite3 - +import asyncio import click from aiohttp import web from IPython import start_ipython - +from snek.shell import Shell from snek.app import Application @@ -14,6 +14,15 @@ def cli(): pass + +@cli.command() +def maintenance(): + async def run(): + app = Application(db_path="sqlite:///snek.db") + await app.services.container.maintenance() + await app.services.channel_message.maintenance() + asyncio.run(run()) + @cli.command() @click.option( "--db_path", default="snek.db", help="Database to initialize if not exists." @@ -68,9 +77,7 @@ def serve(port, host, db_path): help="Database path for the application", ) def shell(db_path): - app = Application(db_path=f"sqlite:///{db_path}") - start_ipython(argv=[], user_ns={"app": app}) - + Shell(db_path).run() def main(): cli() diff --git a/src/snek/service/__init__.py b/src/snek/service/__init__.py index 77702c2..2f6e85b 100644 --- a/src/snek/service/__init__.py +++ b/src/snek/service/__init__.py @@ -17,7 +17,7 @@ from snek.service.user import UserService from snek.service.user_property import UserPropertyService from snek.service.util import UtilService from snek.system.object import Object - +from snek.service.statistics import StatisticsService @functools.cache def get_services(app): @@ -39,6 +39,7 @@ def get_services(app): "channel_attachment": ChannelAttachmentService(app=app), "container": ContainerService(app=app), "push": PushService(app=app), + "statistics": StatisticsService(app=app), } ) diff --git a/src/snek/service/channel_message.py b/src/snek/service/channel_message.py index 6d70da7..bfc5954 100644 --- a/src/snek/service/channel_message.py +++ b/src/snek/service/channel_message.py @@ -5,6 +5,23 @@ from snek.system.template import whitelist_attributes class ChannelMessageService(BaseService): mapper_name = "channel_message" + async def maintenance(self): + async for message in self.find(): + updated_at = message["updated_at"] + html = message["html"] + await self.save(message) + + self.mapper.db['channel_message'].upsert( + { + "uid": message["uid"], + "updated_at": updated_at, + }, + ["uid"], + ) + if html != message["html"]: + print("Reredefined message", message["uid"]) + + async def create(self, channel_uid, user_uid, message, is_final=True): model = await self.new() diff --git a/src/snek/service/container.py b/src/snek/service/container.py index 2c989c8..0a00f27 100644 --- a/src/snek/service/container.py +++ b/src/snek/service/container.py @@ -43,6 +43,14 @@ class ContainerService(BaseService): async def start(self, channel_uid): return await self.compose.start(await self.get_container_name(channel_uid)) + async def maintenance(self): + async for channel in self.services.channel.find(): + if not await self.get(channel["uid"]): + print("Creating container for channel", channel["uid"]) + result = await self.create(channel_uid=channel["uid"]) + print(result) + + async def get_status(self, channel_uid): return await self.compose.get_instance_status(await self.get_container_name(channel_uid)) @@ -60,6 +68,11 @@ class ContainerService(BaseService): volumes=None, ): name = await self.get_container_name(channel_uid) + + test = await self.compose.get_instance(name) + if test: + return test + self.compose.create_instance( name, image, @@ -74,6 +87,7 @@ class ContainerService(BaseService): + "/home/ubuntu" ], ) + return await self.compose.get_instance(name) async def create2( self, id, name, status, resources=None, user_uid=None, path=None, readonly=False diff --git a/src/snek/system/docker.py b/src/snek/system/docker.py index 150c9f9..393c3d6 100644 --- a/src/snek/system/docker.py +++ b/src/snek/system/docker.py @@ -84,6 +84,8 @@ class ComposeFileManager: async def get_instance(self, name): instance = self.compose.get("services", {}).get(name) + if not instance: + return None instance = json.loads(json.dumps(instance,default=str)) instance['status'] = await self.get_instance_status(name) return instance