Skip to content

Commit

Permalink
Add some fallback handling for issues (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntindle authored May 10, 2024
1 parent bcd70a0 commit da8cb90
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 33 deletions.
21 changes: 13 additions & 8 deletions codex/deploy/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
import logging
import os

from langsmith import traceable
from prisma.models import CompletedApp, Deployment, Specification
from prisma.types import DeploymentCreateInput

from codex.api_model import Identifiers
from codex.deploy.packager import create_remote_repo, create_zip_file
from codex.develop.compile import create_bundle_code
from codex.deploy.infrastructure import create_cloud_db
from codex.deploy.model import Settings
from langsmith import traceable
from codex.deploy.packager import create_remote_repo, create_zip_file
from codex.develop.compile import create_bundle_code

logger = logging.getLogger(__name__)

Expand All @@ -22,17 +22,22 @@ async def create_deployment(
spec: Specification,
settings: Settings,
) -> Deployment:
if os.getenv("RUN_ENV", default="local") == "local" or settings.zipfile:
deployment = await create_local_deployment(ids, completedApp, spec)
return deployment
deployment_type = (
create_local_deployment
if (os.getenv("RUN_ENV", default="local") == "local" or settings.zipfile)
else create_cloud_deployment
)
deployment = await deployment_type(ids, completedApp, spec, settings)

deployment = await create_cloud_deployment(ids, completedApp, spec, settings)
return deployment


@traceable
async def create_local_deployment(
ids: Identifiers, completedApp: CompletedApp, spec: Specification
ids: Identifiers,
completedApp: CompletedApp,
spec: Specification,
settings: Settings,
) -> Deployment:
if not ids.user_id:
raise ValueError("User ID is required to create a deployment")
Expand Down
18 changes: 9 additions & 9 deletions codex/deploy/infrastructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@
def create_postgres_database(db_name):
logger.info(f"Attempting to create database {db_name}")

DB_USER: str | None = os.getenv("USER_DB_ADMIN")
if not DB_USER:
USER_DB_ADMIN: str | None = os.getenv("USER_DB_ADMIN")
if not USER_DB_ADMIN:
raise EnvironmentError("USER_DB_ADMIN not found in environment variables.")

DB_PASS = os.getenv("USER_DB_PASS")
if not DB_PASS:
USER_DB_PASS = os.getenv("USER_DB_PASS")
if not USER_DB_PASS:
raise EnvironmentError("USER_DB_PASS not found in environment variables.")

DB_CONN = os.getenv("USER_CONN_NAME")
if not DB_CONN:
USER_CONN_NAME = os.getenv("USER_CONN_NAME")
if not USER_CONN_NAME:
raise EnvironmentError("USER_CONN_NAME not found in environment variables.")

conn = psycopg2.connect(
dbname="postgres",
user=DB_USER,
password=DB_PASS,
host=DB_CONN,
user=USER_DB_ADMIN,
password=USER_DB_PASS,
host=USER_CONN_NAME,
)
conn.autocommit = True # Necessary to execute a create database command
cur = conn.cursor()
Expand Down
41 changes: 28 additions & 13 deletions codex/deploy/packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,19 +657,34 @@ async def create_pyproject(application: Application, package_dir: Path) -> None:
f"--dependency={p.packageName}{f':^{p.version}' if p.version else '=*'}"
for p in sorted(application.packages, key=lambda p: p.packageName)
]
await execute_command(
command=[
"poetry",
"init",
f"--name={app_name_slug}",
f"--description={app_description}",
f"--author={PROJECT_AUTHOR}",
"--python=>=3.11,<4.0",
*dependency_args,
"--no-interaction",
],
cwd=package_dir,
)
try:
await execute_command(
command=[
"poetry",
"init",
f"--name={app_name_slug}",
f"--description={app_description}",
f"--author={PROJECT_AUTHOR}",
"--python=>=3.11,<4.0",
*dependency_args,
"--no-interaction",
],
cwd=package_dir,
)
except Exception as e:
logger.exception(f"Error creating pyproject.toml: {e}")
await execute_command(
command=[
"poetry",
"init",
f"--name={app_name_slug}",
f"--description={app_description}",
f"--author={PROJECT_AUTHOR}",
"--python=>=3.11,<4.0",
"--no-interaction",
],
cwd=package_dir,
)


async def poetry_lock(package_dir: Path) -> None:
Expand Down
13 changes: 11 additions & 2 deletions codex/deploy/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import io
import json
import logging

from typing import Optional

from fastapi import APIRouter, HTTPException, Query
from fastapi.responses import JSONResponse, StreamingResponse
from prisma.models import CompletedApp
Expand All @@ -18,10 +18,10 @@
import codex.deploy.database
import codex.requirements.database
from codex.api_model import (
DeploymentRequest,
DeploymentResponse,
DeploymentsListResponse,
Identifiers,
DeploymentRequest,
)
from codex.common.database import INCLUDE_API_ROUTE, INCLUDE_FUNC
from codex.deploy.model import Settings
Expand Down Expand Up @@ -79,6 +79,8 @@ async def create_deployment(
user_id=user_id, app_id=app_id, spec_id=spec_id
)

deployment_settings: Settings | None = None

if deployment_details is None:
logger.info(f"No deployment settings provided for app {app_id}")
deployment_settings = Settings(
Expand All @@ -96,6 +98,13 @@ async def create_deployment(
except Exception as e:
logger.error("error in deployment settings: %s", e)

if deployment_settings is None:
deployment_settings = Settings(
zipfile=False,
githubRepo=True,
hosted=False,
)

deployment = await deploy_agent.create_deployment(
ids, completedApp, spec, deployment_settings
)
Expand Down
2 changes: 1 addition & 1 deletion codex/interview/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import prisma
import prisma.enums
import prisma.models
from langsmith import traceable

import codex.interview.model
from codex.common.ai_block import Identifiers
from codex.interview.ai_interview import InterviewBlock
from codex.interview.ai_interview_update import InterviewUpdateBlock
from codex.interview.ai_module import ModuleGenerationBlock
from codex.interview.model import Action, InterviewResponse, UpdateUnderstanding
from langsmith import traceable

logger = logging.getLogger(__name__)

Expand Down

0 comments on commit da8cb90

Please sign in to comment.