sless-primer/pg-query/handler.py
“Naeel” f5d03afb52 fix: multiple fixes + pg-query e2e passed
upload.go:
- Update → Patch (MergeFrom) to avoid resourceVersion conflict when operator
  modifies Function between Get() and Update()

terraform provider v0.1.1:
- trigger_resource.go: trToModel returns StringNull() for empty schedule
  (fixes 'provider produced inconsistent result' for http triggers)
- main.go: bump version to 0.1.1

examples/pg-query:
- handler.py: fix column name started_at → created_at (matches migrations/001)
- main.tf: pin provider to ~> 0.1.1

.gitignore: add terraform state, lock, .terraform/, handler.zip

doc/errors/log.md: documented all 5 errors from this session:
- resourceVersion conflict → use Patch
- terraform inconsistent result for schedule → StringNull
- terraform import not implemented → delete+recreate workaround
- wrong column name → check migrations before writing handlers
- Deployment not restarting after image rebuild → rollout restart / TODO: restartedAt annotation

doc/progress.md: terraform apply e2e 
2026-03-07 10:52:29 +04:00

40 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 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, created_at FROM invocations ORDER BY created_at DESC LIMIT %s", (limit,))
rows = cur.fetchall()
# RealDictCursor возвращает объекты, сериализуем вручную
result = []
for row in rows:
r = dict(row)
# datetime → str
if r.get("created_at"):
r["created_at"] = str(r["created_at"])
result.append(r)
return {"invocations": result, "count": len(result)}
finally:
conn.close()