feat: add pg-query example + upload.go supports requirements.txt
- examples/pg-query/handler.py: Python function querying PostgreSQL invocations table - examples/pg-query/requirements.txt: psycopg2-binary==2.9.9 - examples/pg-query/main.tf: terraform config for sless_function + sless_trigger - internal/api/handler/upload.go: generateDockerfile() now accepts hasRequirements bool - scans zip for requirements.txt at upload time - adds RUN pip install --no-cache-dir to Dockerfile when requirements.txt present - doc/progress.md: updated status for pg-query e2e task
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
# 2026-03-07
|
||||
# handler.py — пример serverless функции, работающей с PostgreSQL.
|
||||
# Подключается к postgres.sless.svc.cluster.local:5432 (внутри кластера).
|
||||
# DSN берётся из env переменной PG_DSN (задаётся через env_vars ресурса sless_function).
|
||||
# handle(event) — принимает JSON event, возвращает список записей из таблицы invocations.
|
||||
import os
|
||||
import json
|
||||
|
||||
def handle(event):
|
||||
# psycopg2 входит в python:3.11-slim через pip install ниже,
|
||||
# либо нужно добавить в Dockerfile — для этого примера устанавливаем через requirements.txt.
|
||||
try:
|
||||
import psycopg2
|
||||
import psycopg2.extras
|
||||
except ImportError:
|
||||
return {"error": "psycopg2 not installed — add requirements.txt with psycopg2-binary"}
|
||||
|
||||
dsn = os.environ.get("PG_DSN", "")
|
||||
if not dsn:
|
||||
return {"error": "PG_DSN env variable is not set"}
|
||||
|
||||
limit = event.get("limit", 5)
|
||||
|
||||
conn = psycopg2.connect(dsn)
|
||||
try:
|
||||
cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
|
||||
cur.execute("SELECT id, namespace, function_name, status, started_at FROM invocations ORDER BY started_at DESC LIMIT %s", (limit,))
|
||||
rows = cur.fetchall()
|
||||
# RealDictCursor возвращает объекты, сериализуем вручную
|
||||
result = []
|
||||
for row in rows:
|
||||
r = dict(row)
|
||||
# datetime → str
|
||||
if r.get("started_at"):
|
||||
r["started_at"] = str(r["started_at"])
|
||||
result.append(r)
|
||||
return {"invocations": result, "count": len(result)}
|
||||
finally:
|
||||
conn.close()
|
||||
Reference in New Issue
Block a user