keelflow 0.1.0a1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- keelflow-0.1.0a1/LICENSE +21 -0
- keelflow-0.1.0a1/PKG-INFO +91 -0
- keelflow-0.1.0a1/README.md +65 -0
- keelflow-0.1.0a1/pyproject.toml +72 -0
- keelflow-0.1.0a1/pyproject.toml.orig +66 -0
- keelflow-0.1.0a1/src/keelflow/__init__.py +2 -0
- keelflow-0.1.0a1/src/keelflow/admin/__init__.py +9 -0
- keelflow-0.1.0a1/src/keelflow/admin/app.py +190 -0
- keelflow-0.1.0a1/src/keelflow/compiler/__init__.py +4 -0
- keelflow-0.1.0a1/src/keelflow/compiler/loader.py +242 -0
- keelflow-0.1.0a1/src/keelflow/engine/__init__.py +1 -0
- keelflow-0.1.0a1/src/keelflow/engine/audit.py +65 -0
- keelflow-0.1.0a1/src/keelflow/engine/bulk.py +420 -0
- keelflow-0.1.0a1/src/keelflow/engine/context.py +45 -0
- keelflow-0.1.0a1/src/keelflow/engine/enqueue.py +237 -0
- keelflow-0.1.0a1/src/keelflow/engine/events.py +152 -0
- keelflow-0.1.0a1/src/keelflow/engine/human.py +188 -0
- keelflow-0.1.0a1/src/keelflow/engine/monitoring.py +112 -0
- keelflow-0.1.0a1/src/keelflow/engine/permissions.py +65 -0
- keelflow-0.1.0a1/src/keelflow/engine/registry.py +38 -0
- keelflow-0.1.0a1/src/keelflow/engine/scheduler.py +174 -0
- keelflow-0.1.0a1/src/keelflow/engine/statemachine.py +273 -0
- keelflow-0.1.0a1/src/keelflow/engine/worker.py +373 -0
- keelflow-0.1.0a1/src/keelflow/engine/workflow.py +736 -0
- keelflow-0.1.0a1/src/keelflow/mcp/__init__.py +11 -0
- keelflow-0.1.0a1/src/keelflow/mcp/__main__.py +3 -0
- keelflow-0.1.0a1/src/keelflow/mcp/server.py +77 -0
- keelflow-0.1.0a1/src/keelflow/postgres/__init__.py +0 -0
- keelflow-0.1.0a1/src/keelflow/postgres/schema.py +28 -0
- keelflow-0.1.0a1/src/keelflow/postgres/schema.sql +164 -0
- keelflow-0.1.0a1/src/keelflow/postgres/storage.py +54 -0
- keelflow-0.1.0a1/src/keelflow/py.typed +0 -0
- keelflow-0.1.0a1/src/keelflow/sdk/__init__.py +88 -0
- keelflow-0.1.0a1/src/keelflow/sdk/actor.py +44 -0
- keelflow-0.1.0a1/src/keelflow/sdk/context.py +34 -0
- keelflow-0.1.0a1/src/keelflow/sdk/handler.py +30 -0
- keelflow-0.1.0a1/src/keelflow/sdk/registry.py +22 -0
- keelflow-0.1.0a1/src/keelflow/sdk/services.py +222 -0
- keelflow-0.1.0a1/src/keelflow/sdk/specs.py +134 -0
keelflow-0.1.0a1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Dat Giang
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: keelflow
|
|
3
|
+
Version: 0.1.0a1
|
|
4
|
+
Summary: Reusable Postgres-backed durable workflow engine
|
|
5
|
+
Keywords: workflow,durable,postgres,jobs,state-machine
|
|
6
|
+
Author: Dat Giang
|
|
7
|
+
Author-email: Dat Giang <datgs@users.noreply.github.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Topic :: Database
|
|
16
|
+
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
|
17
|
+
Requires-Dist: alembic>=1.20.1
|
|
18
|
+
Requires-Dist: chancy>=0.26.0
|
|
19
|
+
Requires-Dist: croniter>=7.0.0
|
|
20
|
+
Requires-Dist: mcp>=2.3.0
|
|
21
|
+
Requires-Dist: psycopg[binary]>=3.3.9
|
|
22
|
+
Requires-Dist: pydantic>=2.14.3
|
|
23
|
+
Requires-Dist: pyyaml>=6.0.3
|
|
24
|
+
Requires-Python: >=3.11
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# keelflow
|
|
28
|
+
|
|
29
|
+
The keel of your business flows — a reusable Postgres-native durable
|
|
30
|
+
workflow engine. Reference Python implementation of the **keelflow spec**
|
|
31
|
+
(`spec/`).
|
|
32
|
+
|
|
33
|
+
`pip install keelflow` (PyPI name: `keelflow`)
|
|
34
|
+
|
|
35
|
+
## What it gives you
|
|
36
|
+
|
|
37
|
+
- **Durable jobs** — transactional enqueue: job + outbox row commit with
|
|
38
|
+
your data change, or nothing commits.
|
|
39
|
+
- **DAG workflows** — `needs`/`if:` guards/`${{ outputs }}` refs, YAML or
|
|
40
|
+
code definitions, human steps park a run until a person completes them.
|
|
41
|
+
- **Bulk imports** — chunk + per-row status, `atomic='chunk'|'row'`,
|
|
42
|
+
kill-resume of pending rows only, error queue.
|
|
43
|
+
- **Human tasks** — inbox (`user:`/`role:`), first-claim-wins, escalation.
|
|
44
|
+
- **Entity state machines** — guarded transitions, `creates_job` /
|
|
45
|
+
`triggers_workflow` hooks, `allowed_actions`.
|
|
46
|
+
- **Outbox events, audit, RBAC hooks, cron scheduling,
|
|
47
|
+
key-serialized execution** (`concurrency_key`), monitoring data APIs,
|
|
48
|
+
minimal SuperAdmin UI, read-only MCP tools.
|
|
49
|
+
|
|
50
|
+
Kernel lifecycle (claim/retry/schedule) rides on **Chancy**; everything
|
|
51
|
+
differentiating is built in-house.
|
|
52
|
+
|
|
53
|
+
## Quickstart
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
docker run -d --name kf-pg -e POSTGRES_PASSWORD=postgres \
|
|
57
|
+
-p 5544:5432 postgres:16-alpine
|
|
58
|
+
docker exec kf-pg createdb -U postgres keelflow
|
|
59
|
+
|
|
60
|
+
export KEELFLOW_DSN=postgresql://postgres:postgres@localhost:5544/keelflow
|
|
61
|
+
uv run python examples/order_flow.py # applies schema + runs a demo flow
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Full walkthrough of every feature: **[docs/guide.md](docs/guide.md)**.
|
|
65
|
+
Complete demo app (SM + human tasks + DAG + failure paths):
|
|
66
|
+
**[examples/invoice_flow](examples/invoice_flow/)**.
|
|
67
|
+
|
|
68
|
+
## Layout
|
|
69
|
+
|
|
70
|
+
- `src/keelflow/sdk/` — contracts apps implement (JobHandler, JobContext, specs)
|
|
71
|
+
- `src/keelflow/engine/` — dispatch, DAG resolver, registry, scheduler,
|
|
72
|
+
state machine, bulk, events, audit, monitoring
|
|
73
|
+
- `src/keelflow/postgres/` — schema + storage
|
|
74
|
+
- `src/keelflow/compiler/` — YAML → DAG/state-machine compiler
|
|
75
|
+
- `src/keelflow/admin/` — minimal SuperAdmin (dev/ops, no auth)
|
|
76
|
+
- `src/keelflow/mcp/` — read-only agent tools
|
|
77
|
+
- `spec/` — language-agnostic keelflow spec (port to .NET/Java/PHP)
|
|
78
|
+
- `docs/architecture-design-brief.md` — design source of truth
|
|
79
|
+
- `docs/plans/active/` — execution plan + acceptance evidence
|
|
80
|
+
|
|
81
|
+
## Dev
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
uv sync --dev
|
|
85
|
+
uv run pytest # needs Postgres at $KEELFLOW_TEST_DSN (default :5544/keelflow)
|
|
86
|
+
uv run ruff check src tests
|
|
87
|
+
uv run mypy src/keelflow
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Admin UI: `KEELFLOW_DSN=… uv run python -m keelflow.admin.app` →
|
|
91
|
+
http://127.0.0.1:8410 (read-only + retry button; no auth — bind locally).
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# keelflow
|
|
2
|
+
|
|
3
|
+
The keel of your business flows — a reusable Postgres-native durable
|
|
4
|
+
workflow engine. Reference Python implementation of the **keelflow spec**
|
|
5
|
+
(`spec/`).
|
|
6
|
+
|
|
7
|
+
`pip install keelflow` (PyPI name: `keelflow`)
|
|
8
|
+
|
|
9
|
+
## What it gives you
|
|
10
|
+
|
|
11
|
+
- **Durable jobs** — transactional enqueue: job + outbox row commit with
|
|
12
|
+
your data change, or nothing commits.
|
|
13
|
+
- **DAG workflows** — `needs`/`if:` guards/`${{ outputs }}` refs, YAML or
|
|
14
|
+
code definitions, human steps park a run until a person completes them.
|
|
15
|
+
- **Bulk imports** — chunk + per-row status, `atomic='chunk'|'row'`,
|
|
16
|
+
kill-resume of pending rows only, error queue.
|
|
17
|
+
- **Human tasks** — inbox (`user:`/`role:`), first-claim-wins, escalation.
|
|
18
|
+
- **Entity state machines** — guarded transitions, `creates_job` /
|
|
19
|
+
`triggers_workflow` hooks, `allowed_actions`.
|
|
20
|
+
- **Outbox events, audit, RBAC hooks, cron scheduling,
|
|
21
|
+
key-serialized execution** (`concurrency_key`), monitoring data APIs,
|
|
22
|
+
minimal SuperAdmin UI, read-only MCP tools.
|
|
23
|
+
|
|
24
|
+
Kernel lifecycle (claim/retry/schedule) rides on **Chancy**; everything
|
|
25
|
+
differentiating is built in-house.
|
|
26
|
+
|
|
27
|
+
## Quickstart
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
docker run -d --name kf-pg -e POSTGRES_PASSWORD=postgres \
|
|
31
|
+
-p 5544:5432 postgres:16-alpine
|
|
32
|
+
docker exec kf-pg createdb -U postgres keelflow
|
|
33
|
+
|
|
34
|
+
export KEELFLOW_DSN=postgresql://postgres:postgres@localhost:5544/keelflow
|
|
35
|
+
uv run python examples/order_flow.py # applies schema + runs a demo flow
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Full walkthrough of every feature: **[docs/guide.md](docs/guide.md)**.
|
|
39
|
+
Complete demo app (SM + human tasks + DAG + failure paths):
|
|
40
|
+
**[examples/invoice_flow](examples/invoice_flow/)**.
|
|
41
|
+
|
|
42
|
+
## Layout
|
|
43
|
+
|
|
44
|
+
- `src/keelflow/sdk/` — contracts apps implement (JobHandler, JobContext, specs)
|
|
45
|
+
- `src/keelflow/engine/` — dispatch, DAG resolver, registry, scheduler,
|
|
46
|
+
state machine, bulk, events, audit, monitoring
|
|
47
|
+
- `src/keelflow/postgres/` — schema + storage
|
|
48
|
+
- `src/keelflow/compiler/` — YAML → DAG/state-machine compiler
|
|
49
|
+
- `src/keelflow/admin/` — minimal SuperAdmin (dev/ops, no auth)
|
|
50
|
+
- `src/keelflow/mcp/` — read-only agent tools
|
|
51
|
+
- `spec/` — language-agnostic keelflow spec (port to .NET/Java/PHP)
|
|
52
|
+
- `docs/architecture-design-brief.md` — design source of truth
|
|
53
|
+
- `docs/plans/active/` — execution plan + acceptance evidence
|
|
54
|
+
|
|
55
|
+
## Dev
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
uv sync --dev
|
|
59
|
+
uv run pytest # needs Postgres at $KEELFLOW_TEST_DSN (default :5544/keelflow)
|
|
60
|
+
uv run ruff check src tests
|
|
61
|
+
uv run mypy src/keelflow
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Admin UI: `KEELFLOW_DSN=… uv run python -m keelflow.admin.app` →
|
|
65
|
+
http://127.0.0.1:8410 (read-only + retry button; no auth — bind locally).
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "keelflow"
|
|
3
|
+
version = "0.1.0a1"
|
|
4
|
+
description = "Reusable Postgres-backed durable workflow engine"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
keywords = [
|
|
9
|
+
"workflow",
|
|
10
|
+
"durable",
|
|
11
|
+
"postgres",
|
|
12
|
+
"jobs",
|
|
13
|
+
"state-machine",
|
|
14
|
+
]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3.11",
|
|
19
|
+
"Programming Language :: Python :: 3.12",
|
|
20
|
+
"Programming Language :: Python :: 3.13",
|
|
21
|
+
"Topic :: Database",
|
|
22
|
+
"Topic :: Software Development :: Libraries :: Application Frameworks",
|
|
23
|
+
]
|
|
24
|
+
requires-python = ">=3.11"
|
|
25
|
+
dependencies = [
|
|
26
|
+
"alembic>=1.20.1",
|
|
27
|
+
"chancy>=0.26.0",
|
|
28
|
+
"croniter>=7.0.0",
|
|
29
|
+
"mcp>=2.3.0",
|
|
30
|
+
"psycopg[binary]>=3.3.9",
|
|
31
|
+
"pydantic>=2.14.3",
|
|
32
|
+
"pyyaml>=6.0.3",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[[project.authors]]
|
|
36
|
+
name = "Dat Giang"
|
|
37
|
+
email = "datgs@users.noreply.github.com"
|
|
38
|
+
|
|
39
|
+
[build-system]
|
|
40
|
+
requires = ["uv_build>=0.11.19,<0.12.0"]
|
|
41
|
+
build-backend = "uv_build"
|
|
42
|
+
|
|
43
|
+
[dependency-groups]
|
|
44
|
+
dev = [
|
|
45
|
+
"mypy>=2.4.1",
|
|
46
|
+
"pytest>=9.2.0",
|
|
47
|
+
"pytest-asyncio>=1.5.0",
|
|
48
|
+
"ruff>=0.17.5",
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
[tool.pytest.ini_options]
|
|
52
|
+
asyncio_mode = "auto"
|
|
53
|
+
pythonpath = [
|
|
54
|
+
"src",
|
|
55
|
+
"examples/invoice_flow/src",
|
|
56
|
+
"examples/supplier_onboarding/src",
|
|
57
|
+
"examples/bulk_import/src",
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
[tool.mypy]
|
|
61
|
+
python_version = "3.11"
|
|
62
|
+
ignore_missing_imports = true
|
|
63
|
+
|
|
64
|
+
[[tool.mypy.overrides]]
|
|
65
|
+
module = "chancy.*"
|
|
66
|
+
ignore_missing_imports = true
|
|
67
|
+
|
|
68
|
+
[tool.uv.workspace]
|
|
69
|
+
members = [
|
|
70
|
+
".spikes/kernel",
|
|
71
|
+
"examples/*",
|
|
72
|
+
]
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "keelflow"
|
|
3
|
+
version = "0.1.0a1"
|
|
4
|
+
description = "Reusable Postgres-backed durable workflow engine"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
authors = [
|
|
9
|
+
{ name = "Dat Giang", email = "datgs@users.noreply.github.com" }
|
|
10
|
+
]
|
|
11
|
+
keywords = ["workflow", "durable", "postgres", "jobs", "state-machine"]
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Development Status :: 3 - Alpha",
|
|
14
|
+
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Programming Language :: Python :: 3.11",
|
|
17
|
+
"Programming Language :: Python :: 3.12",
|
|
18
|
+
"Programming Language :: Python :: 3.13",
|
|
19
|
+
"Topic :: Database",
|
|
20
|
+
"Topic :: Software Development :: Libraries :: Application Frameworks",
|
|
21
|
+
]
|
|
22
|
+
requires-python = ">=3.11"
|
|
23
|
+
dependencies = [
|
|
24
|
+
"alembic>=1.20.1",
|
|
25
|
+
"chancy>=0.26.0",
|
|
26
|
+
"croniter>=7.0.0",
|
|
27
|
+
"mcp>=2.3.0",
|
|
28
|
+
"psycopg[binary]>=3.3.9",
|
|
29
|
+
"pydantic>=2.14.3",
|
|
30
|
+
"pyyaml>=6.0.3",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[build-system]
|
|
34
|
+
requires = ["uv_build>=0.11.19,<0.12.0"]
|
|
35
|
+
build-backend = "uv_build"
|
|
36
|
+
|
|
37
|
+
[dependency-groups]
|
|
38
|
+
dev = [
|
|
39
|
+
"mypy>=2.4.1",
|
|
40
|
+
"pytest>=9.2.0",
|
|
41
|
+
"pytest-asyncio>=1.5.0",
|
|
42
|
+
"ruff>=0.17.5",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
[tool.pytest.ini_options]
|
|
46
|
+
asyncio_mode = "auto"
|
|
47
|
+
pythonpath = [
|
|
48
|
+
"src",
|
|
49
|
+
"examples/invoice_flow/src",
|
|
50
|
+
"examples/supplier_onboarding/src",
|
|
51
|
+
"examples/bulk_import/src",
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
[tool.mypy]
|
|
55
|
+
python_version = "3.11"
|
|
56
|
+
ignore_missing_imports = true
|
|
57
|
+
|
|
58
|
+
[[tool.mypy.overrides]]
|
|
59
|
+
module = "chancy.*"
|
|
60
|
+
ignore_missing_imports = true
|
|
61
|
+
|
|
62
|
+
[tool.uv.workspace]
|
|
63
|
+
members = [
|
|
64
|
+
".spikes/kernel",
|
|
65
|
+
"examples/*",
|
|
66
|
+
]
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"""SuperAdmin — deliberately rough server-rendered ops UI (T15).
|
|
2
|
+
|
|
3
|
+
`make_app(dsn)` returns a small WSGI/HTTP handler for `python -m
|
|
4
|
+
keelflow.admin`. Read-only views: queue overview, job list/detail, workflow
|
|
5
|
+
runs/detail, failed rows, metrics. Mutations limited to `POST /retry`.
|
|
6
|
+
"""
|
|
7
|
+
from .app import make_app
|
|
8
|
+
|
|
9
|
+
__all__ = ["make_app"]
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"""Minimal server-rendered SuperAdmin (deliberately rough).
|
|
2
|
+
|
|
3
|
+
Serves: / (metrics+queues), /jobs, /jobs/<id>, /runs, /runs/<id>,
|
|
4
|
+
/jobs/<id>/rows, POST /jobs/<id>/retry. No auth — bind locally or put
|
|
5
|
+
behind your own gateway.
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import asyncio
|
|
10
|
+
import html
|
|
11
|
+
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
12
|
+
from urllib.parse import parse_qs, urlparse
|
|
13
|
+
|
|
14
|
+
import psycopg
|
|
15
|
+
|
|
16
|
+
from ..engine.monitoring import MonitoringService
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def make_app(dsn: str, host: str = "127.0.0.1", port: int = 8410):
|
|
20
|
+
"""Start the admin HTTP server (blocking). Ctrl+C to stop."""
|
|
21
|
+
svc = MonitoringService()
|
|
22
|
+
|
|
23
|
+
def q(coro):
|
|
24
|
+
async def _run():
|
|
25
|
+
conn = await psycopg.AsyncConnection.connect(dsn, autocommit=True)
|
|
26
|
+
try:
|
|
27
|
+
return await coro(conn)
|
|
28
|
+
finally:
|
|
29
|
+
await conn.close()
|
|
30
|
+
return asyncio.run(_run())
|
|
31
|
+
|
|
32
|
+
class H(BaseHTTPRequestHandler):
|
|
33
|
+
def _send(self, body: str, code: int = 200):
|
|
34
|
+
data = body.encode()
|
|
35
|
+
self.send_response(code)
|
|
36
|
+
self.send_header("Content-Type", "text/html; charset=utf-8")
|
|
37
|
+
self.send_header("Content-Length", str(len(data)))
|
|
38
|
+
self.end_headers()
|
|
39
|
+
self.wfile.write(data)
|
|
40
|
+
|
|
41
|
+
def log_message(self, *args): # quieter
|
|
42
|
+
pass
|
|
43
|
+
|
|
44
|
+
def do_GET(self):
|
|
45
|
+
p = urlparse(self.path)
|
|
46
|
+
path, qs = p.path, parse_qs(p.query)
|
|
47
|
+
if path == "/":
|
|
48
|
+
return self._page("keelflow admin", self._home())
|
|
49
|
+
if path == "/jobs":
|
|
50
|
+
return self._page("jobs", self._jobs(qs))
|
|
51
|
+
if path.startswith("/jobs/") and path.endswith("/rows"):
|
|
52
|
+
jid = path.split("/")[2]
|
|
53
|
+
return self._page("failed rows", self._rows(jid))
|
|
54
|
+
if path.startswith("/jobs/"):
|
|
55
|
+
return self._page("job", self._job(path.split("/")[2]))
|
|
56
|
+
if path == "/runs":
|
|
57
|
+
return self._page("runs", self._runs())
|
|
58
|
+
if path.startswith("/runs/"):
|
|
59
|
+
return self._page("run", self._run(path.split("/")[2]))
|
|
60
|
+
return self._send("<h1>404</h1>", 404)
|
|
61
|
+
|
|
62
|
+
def do_POST(self):
|
|
63
|
+
p = urlparse(self.path)
|
|
64
|
+
if p.path.startswith("/jobs/") and p.path.endswith("/retry"):
|
|
65
|
+
jid = p.path.split("/")[2]
|
|
66
|
+
q(lambda c: svc.retry_job(c, jid))
|
|
67
|
+
return self._send("<meta http-equiv='refresh' content='0;/jobs'>")
|
|
68
|
+
return self._send("<h1>404</h1>", 404)
|
|
69
|
+
|
|
70
|
+
# ── views ────────────────────────────────────────────────────
|
|
71
|
+
|
|
72
|
+
def _page(self, title, body):
|
|
73
|
+
nav = ("<p><a href='/'>home</a> · <a href='/jobs'>jobs</a> · "
|
|
74
|
+
"<a href='/runs'>runs</a></p>")
|
|
75
|
+
return self._send(
|
|
76
|
+
f"<html><head><title>{title}</title>"
|
|
77
|
+
"<style>body{{font-family:monospace;margin:2em}}"
|
|
78
|
+
"table{{border-collapse:collapse}}td,th{{border:1px solid #ccc;"
|
|
79
|
+
"padding:4px 8px}} .failed{{color:#b00}} .pending{{color:#a60}}"
|
|
80
|
+
" .done{{color:#070}}</style></head><body>"
|
|
81
|
+
f"{nav}<h1>{title}</h1>{body}</body></html>"
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
def _esc(self, v):
|
|
85
|
+
return html.escape(str(v)) if v is not None else ""
|
|
86
|
+
|
|
87
|
+
def _table(self, rows, cols, links=None):
|
|
88
|
+
links = links or {}
|
|
89
|
+
out = ["<table><tr>" + "".join(f"<th>{c}</th>" for c in cols) + "</tr>"]
|
|
90
|
+
for r in rows:
|
|
91
|
+
cells = []
|
|
92
|
+
for c in cols:
|
|
93
|
+
v = r.get(c)
|
|
94
|
+
cls = v if c == "status" and v in ("failed", "pending", "done") else ""
|
|
95
|
+
cell = f"<td class='{cls}'>{self._esc(v)}</td>"
|
|
96
|
+
if c in links:
|
|
97
|
+
cell = f"<td><a href='{links[c]}{r[c]}'>{self._esc(v)}</a></td>"
|
|
98
|
+
cells.append(cell)
|
|
99
|
+
out.append("<tr>" + "".join(cells) + "</tr>")
|
|
100
|
+
out.append("</table>")
|
|
101
|
+
return "".join(out)
|
|
102
|
+
|
|
103
|
+
def _home(self):
|
|
104
|
+
m = q(svc.metrics_snapshot)
|
|
105
|
+
return (
|
|
106
|
+
f"<h2>metrics</h2><ul>"
|
|
107
|
+
f"<li>queue depth: {m['queue_depth']}</li>"
|
|
108
|
+
f"<li>running: {m['running']}</li>"
|
|
109
|
+
f"<li>failed (24h): {m['failed_24h']}</li>"
|
|
110
|
+
f"<li>oldest pending: {self._esc(round(m['oldest_pending_s'] or 0,1))}s</li>"
|
|
111
|
+
f"</ul>"
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
def _jobs(self, qs):
|
|
115
|
+
types = qs.get("type", [None])[0]
|
|
116
|
+
statuses = qs.get("status", [None])[0]
|
|
117
|
+
res = q(lambda c: svc.jobs_list(
|
|
118
|
+
c, types=[types] if types else None,
|
|
119
|
+
statuses=[statuses] if statuses else None, limit=100,
|
|
120
|
+
))
|
|
121
|
+
rows = [
|
|
122
|
+
{
|
|
123
|
+
"id": str(r["id"]), "type": r["type"], "status": r["status"],
|
|
124
|
+
"attempts": r["attempts"], "run_at": r["run_at"],
|
|
125
|
+
"claimed_by": r["claimed_by"], "assignee": r["assignee"],
|
|
126
|
+
"created_at": r["created_at"],
|
|
127
|
+
}
|
|
128
|
+
for r in res["items"]
|
|
129
|
+
]
|
|
130
|
+
return self._table(
|
|
131
|
+
rows, ["id", "type", "status", "attempts", "assignee", "created_at"],
|
|
132
|
+
links={"id": "/jobs/"},
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
def _job(self, jid):
|
|
136
|
+
r = q(lambda c: svc.job_get(c, jid))
|
|
137
|
+
if not r:
|
|
138
|
+
return "<p>not found</p>"
|
|
139
|
+
kv = "".join(
|
|
140
|
+
f"<tr><td>{k}</td><td>{self._esc(v)}</td></tr>"
|
|
141
|
+
for k, v in r.items() if k != "id"
|
|
142
|
+
)
|
|
143
|
+
return (
|
|
144
|
+
f"<table><tr><td>id</td><td>{r['id']}</td></tr>{kv}</table>"
|
|
145
|
+
f"<p><a href='/jobs/{r['id']}/rows'>failed rows</a> · "
|
|
146
|
+
f"<form method='post' action='/jobs/{r['id']}/retry'>"
|
|
147
|
+
"<button>retry</button></form></p>"
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
def _runs(self):
|
|
151
|
+
rows = q(svc.runs_list)
|
|
152
|
+
return self._table(
|
|
153
|
+
[{**r, "id": str(r["id"])} for r in rows],
|
|
154
|
+
["id", "workflow", "status", "created_at"],
|
|
155
|
+
links={"id": "/runs/"},
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
def _run(self, rid):
|
|
159
|
+
r = q(lambda c: svc.runs_get(c, rid))
|
|
160
|
+
if not r:
|
|
161
|
+
return "<p>not found</p>"
|
|
162
|
+
steps = [
|
|
163
|
+
{
|
|
164
|
+
"step_id": s["step_id"], "type": s["type"],
|
|
165
|
+
"status": s["status"], "attempts": s["attempts"],
|
|
166
|
+
"outputs": s["outputs"], "job_id": s["job_id"],
|
|
167
|
+
}
|
|
168
|
+
for s in r["steps"]
|
|
169
|
+
]
|
|
170
|
+
return (
|
|
171
|
+
f"<p>workflow: {r['workflow']} · status: {r['status']}</p>"
|
|
172
|
+
+ self._table(steps, ["step_id", "type", "status", "attempts", "outputs"])
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
def _rows(self, jid):
|
|
176
|
+
rows = q(lambda c: svc.rows_list_failed(c, jid))
|
|
177
|
+
return self._table(
|
|
178
|
+
[{**r, "id": str(r["id"])} for r in rows],
|
|
179
|
+
["id", "row_key", "error", "updated_at"],
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
server = ThreadingHTTPServer((host, port), H)
|
|
183
|
+
print(f"keelflow admin → http://{host}:{port}")
|
|
184
|
+
server.serve_forever()
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
if __name__ == "__main__": # pragma: no cover
|
|
188
|
+
import os
|
|
189
|
+
|
|
190
|
+
make_app(os.environ["KEELFLOW_DSN"])
|