Files
Zamil Majdyandmajdyz fa72f11b22 [AGPT-580] Support async subprocess execution (#145)
Subprocess execution implementation (for Ruff & PyRight) is currently a blocking call. This doesn't work well with the event-loop used by the fastapi.

Scope:

Make AiBlock.validate async
Make subprocess execution async.

---------

Co-authored-by: majdyz <[email protected]>
2024-03-25 18:22:18 +04:00

41 lines
1.4 KiB
Python

import base64
import logging
from prisma.models import CompletedApp, Deployment
from prisma.types import DeploymentCreateInput
from codex.api_model import Identifiers
from codex.deploy.packager import create_zip_file
from codex.develop.compile import create_server_code
logger = logging.getLogger(__name__)
async def create_deployment(ids: Identifiers, completedApp: CompletedApp) -> Deployment:
if not ids.user_id:
raise ValueError("User ID is required to create a deployment")
app = await create_server_code(completedApp)
zip_file = await create_zip_file(app)
file_name = completedApp.name.replace(" ", "_")
try:
base64.b64encode(zip_file)
logger.info(f"Creating deployment for {completedApp.name}")
encoded_file_bytes = base64.b64encode(zip_file).decode("utf-8")
deployment = await Deployment.prisma().create(
data=DeploymentCreateInput(
CompletedApp={"connect": {"id": completedApp.id}},
User={"connect": {"id": ids.user_id}},
fileName=f"{file_name}.zip",
fileSize=len(zip_file),
# I need to do this as the Base64 type in prisma is not working
fileBytes=encoded_file_bytes, # type: ignore
)
)
except Exception as e:
logger.exception("Error creating deployment in database")
raise ValueError(f"Error creating deployment in database: {e}")
return deployment