fractal-events 1.0.0__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.
- fractal_events-1.0.0/.github/workflows/build.yml +44 -0
- fractal_events-1.0.0/.github/workflows/publish.yml +28 -0
- fractal_events-1.0.0/.gitignore +90 -0
- fractal_events-1.0.0/LICENSE +21 -0
- fractal_events-1.0.0/MANIFEST.in +9 -0
- fractal_events-1.0.0/Makefile +69 -0
- fractal_events-1.0.0/PKG-INFO +160 -0
- fractal_events-1.0.0/README.md +124 -0
- fractal_events-1.0.0/fractal_events/__init__.py +65 -0
- fractal_events-1.0.0/fractal_events/event.py +77 -0
- fractal_events-1.0.0/fractal_events/event_projector.py +23 -0
- fractal_events-1.0.0/fractal_events/event_publisher.py +31 -0
- fractal_events-1.0.0/fractal_events/event_store.py +193 -0
- fractal_events-1.0.0/fractal_events/event_stream.py +11 -0
- fractal_events-1.0.0/fractal_events/message.py +15 -0
- fractal_events-1.0.0/fractal_events/projectors/__init__.py +0 -0
- fractal_events-1.0.0/fractal_events/projectors/command_bus_projector.py +103 -0
- fractal_events-1.0.0/fractal_events/projectors/event_store_projector.py +17 -0
- fractal_events-1.0.0/fractal_events/projectors/print_projector.py +23 -0
- fractal_events-1.0.0/pyproject.toml +116 -0
- fractal_events-1.0.0/tests/test_command_bus_projector.py +210 -0
- fractal_events-1.0.0/tests/test_event_store.py +160 -0
- fractal_events-1.0.0/tests/test_publishing.py +90 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
name: build
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
workflow_dispatch:
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
build:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
strategy:
|
|
14
|
+
matrix:
|
|
15
|
+
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
|
|
16
|
+
|
|
17
|
+
env:
|
|
18
|
+
USING_COVERAGE: "3.14"
|
|
19
|
+
|
|
20
|
+
steps:
|
|
21
|
+
- name: Checkout sources
|
|
22
|
+
uses: actions/checkout@v2
|
|
23
|
+
|
|
24
|
+
- name: Set up Python
|
|
25
|
+
uses: actions/setup-python@v2
|
|
26
|
+
with:
|
|
27
|
+
python-version: ${{ matrix.python-version }}
|
|
28
|
+
|
|
29
|
+
- name: Install uv
|
|
30
|
+
uses: astral-sh/setup-uv@v4
|
|
31
|
+
|
|
32
|
+
- name: Install dependencies
|
|
33
|
+
run: |
|
|
34
|
+
uv pip install --system -e ".[dev]"
|
|
35
|
+
|
|
36
|
+
- name: Run tests
|
|
37
|
+
run: |
|
|
38
|
+
uv run pytest
|
|
39
|
+
|
|
40
|
+
- name: Upload coverage to Codecov
|
|
41
|
+
uses: codecov/codecov-action@v3
|
|
42
|
+
if: contains(env.USING_COVERAGE, matrix.python-version)
|
|
43
|
+
with:
|
|
44
|
+
fail_ci_if_error: false
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
name: publish
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [created]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
publish:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
steps:
|
|
11
|
+
- name: Checkout sources
|
|
12
|
+
uses: actions/checkout@v2
|
|
13
|
+
|
|
14
|
+
- name: Set up Python
|
|
15
|
+
uses: actions/setup-python@v2
|
|
16
|
+
with:
|
|
17
|
+
python-version: "3.14"
|
|
18
|
+
|
|
19
|
+
- name: Install dependencies
|
|
20
|
+
run: |
|
|
21
|
+
make deps
|
|
22
|
+
|
|
23
|
+
- name: Publish to PyPi
|
|
24
|
+
env:
|
|
25
|
+
FLIT_USERNAME: ${{ secrets.PYPI_USERNAME }}
|
|
26
|
+
FLIT_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
|
|
27
|
+
run: |
|
|
28
|
+
make publish
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
*.py[cod]
|
|
2
|
+
|
|
3
|
+
# C extensions
|
|
4
|
+
*.so
|
|
5
|
+
|
|
6
|
+
# Packages
|
|
7
|
+
*.egg
|
|
8
|
+
*.egg-info
|
|
9
|
+
dist
|
|
10
|
+
build
|
|
11
|
+
eggs
|
|
12
|
+
parts
|
|
13
|
+
bin
|
|
14
|
+
var
|
|
15
|
+
sdist
|
|
16
|
+
develop-eggs
|
|
17
|
+
.installed.cfg
|
|
18
|
+
lib
|
|
19
|
+
lib64
|
|
20
|
+
|
|
21
|
+
# Created during tests
|
|
22
|
+
.cache/
|
|
23
|
+
dj_package/
|
|
24
|
+
|
|
25
|
+
# Installer logs
|
|
26
|
+
pip-log.txt
|
|
27
|
+
|
|
28
|
+
# Unit test / coverage reports
|
|
29
|
+
.coverage
|
|
30
|
+
.tox
|
|
31
|
+
nosetests.xml
|
|
32
|
+
htmlcov/
|
|
33
|
+
.tox/
|
|
34
|
+
.nox/
|
|
35
|
+
.coverage.*
|
|
36
|
+
.cache
|
|
37
|
+
coverage.xml
|
|
38
|
+
*.cover
|
|
39
|
+
*.py,cover
|
|
40
|
+
.hypothesis/
|
|
41
|
+
.pytest_cache/
|
|
42
|
+
cover/
|
|
43
|
+
|
|
44
|
+
# SonarQube
|
|
45
|
+
sonar-project.properties
|
|
46
|
+
.scannerwork
|
|
47
|
+
|
|
48
|
+
# Translations
|
|
49
|
+
*.mo
|
|
50
|
+
|
|
51
|
+
# Mr Developer
|
|
52
|
+
.mr.developer.cfg
|
|
53
|
+
.project
|
|
54
|
+
.pydevproject
|
|
55
|
+
|
|
56
|
+
# Complexity
|
|
57
|
+
output/*.html
|
|
58
|
+
output/*/index.html
|
|
59
|
+
|
|
60
|
+
# Sphinx
|
|
61
|
+
docs/_build
|
|
62
|
+
|
|
63
|
+
# Cookiecutter
|
|
64
|
+
output/
|
|
65
|
+
dj-package/
|
|
66
|
+
|
|
67
|
+
# Virtualenvs
|
|
68
|
+
venv/
|
|
69
|
+
env/
|
|
70
|
+
|
|
71
|
+
.idea
|
|
72
|
+
.ipynb_checkpoints
|
|
73
|
+
.mypy_cache
|
|
74
|
+
.vscode
|
|
75
|
+
__pycache__
|
|
76
|
+
.pytest_cache
|
|
77
|
+
htmlcov
|
|
78
|
+
site
|
|
79
|
+
.netlify
|
|
80
|
+
test.db
|
|
81
|
+
log.txt
|
|
82
|
+
Pipfile.lock
|
|
83
|
+
env3.*
|
|
84
|
+
docs_build
|
|
85
|
+
docs.zip
|
|
86
|
+
archive.zip
|
|
87
|
+
|
|
88
|
+
# vim temporary files
|
|
89
|
+
*~
|
|
90
|
+
.*.sw?
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Douwe van der Meij
|
|
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,69 @@
|
|
|
1
|
+
.PHONY: help install deps dev-deps test lint format clean build publish dev-install
|
|
2
|
+
|
|
3
|
+
# Default target
|
|
4
|
+
help:
|
|
5
|
+
@echo "Available targets:"
|
|
6
|
+
@echo " install - Install the package using uv"
|
|
7
|
+
@echo " deps - Install production dependencies (for CI)"
|
|
8
|
+
@echo " dev-deps - Install development dependencies"
|
|
9
|
+
@echo " dev-install - Install package in development mode with all dependencies"
|
|
10
|
+
@echo " test - Run tests with pytest"
|
|
11
|
+
@echo " lint - Run linters (ruff)"
|
|
12
|
+
@echo " format - Format code with black"
|
|
13
|
+
@echo " clean - Remove build artifacts and cache files"
|
|
14
|
+
@echo " build - Build distribution packages"
|
|
15
|
+
@echo " publish - Publish to PyPI using flit"
|
|
16
|
+
|
|
17
|
+
# Install package using uv
|
|
18
|
+
install:
|
|
19
|
+
uv pip install -e .
|
|
20
|
+
|
|
21
|
+
# Install production dependencies (for CI/CD)
|
|
22
|
+
deps:
|
|
23
|
+
pip install flit
|
|
24
|
+
|
|
25
|
+
# Install development dependencies using uv
|
|
26
|
+
dev-deps:
|
|
27
|
+
uv pip install -e ".[dev]"
|
|
28
|
+
|
|
29
|
+
# Full development setup with uv
|
|
30
|
+
dev-install:
|
|
31
|
+
@echo "Setting up development environment with uv..."
|
|
32
|
+
uv venv
|
|
33
|
+
uv pip install -e ".[dev]"
|
|
34
|
+
@echo "Development environment ready! Activate with: source .venv/bin/activate"
|
|
35
|
+
|
|
36
|
+
# Run tests
|
|
37
|
+
test:
|
|
38
|
+
uv run pytest
|
|
39
|
+
|
|
40
|
+
# Run linters
|
|
41
|
+
lint:
|
|
42
|
+
uv run ruff check .
|
|
43
|
+
uv run mypy fractal_events
|
|
44
|
+
|
|
45
|
+
# Format code
|
|
46
|
+
format:
|
|
47
|
+
uv run black .
|
|
48
|
+
uv run isort .
|
|
49
|
+
uv run ruff check --fix .
|
|
50
|
+
|
|
51
|
+
# Clean build artifacts
|
|
52
|
+
clean:
|
|
53
|
+
rm -rf build/
|
|
54
|
+
rm -rf dist/
|
|
55
|
+
rm -rf *.egg-info
|
|
56
|
+
rm -rf .pytest_cache
|
|
57
|
+
rm -rf .mypy_cache
|
|
58
|
+
rm -rf .ruff_cache
|
|
59
|
+
find . -type d -name __pycache__ -exec rm -rf {} +
|
|
60
|
+
find . -type f -name "*.pyc" -delete
|
|
61
|
+
find . -type f -name "*.pyo" -delete
|
|
62
|
+
|
|
63
|
+
# Build distribution packages
|
|
64
|
+
build: clean
|
|
65
|
+
flit build
|
|
66
|
+
|
|
67
|
+
# Publish to PyPI (requires FLIT_USERNAME and FLIT_PASSWORD env vars)
|
|
68
|
+
publish: build
|
|
69
|
+
flit publish
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fractal-events
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Event sourcing for the Fractal stack: events, publisher, event store and the projectors that persist them or turn them back into commands.
|
|
5
|
+
Keywords: event sourcing,events,cqrs,ddd,projector
|
|
6
|
+
Author-email: Douwe van der Meij <douwe@karibu-online.nl>
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Requires-Dist: fractal-core
|
|
21
|
+
Requires-Dist: fractal-commands
|
|
22
|
+
Requires-Dist: fractal-repositories
|
|
23
|
+
Requires-Dist: fractal-specifications
|
|
24
|
+
Requires-Dist: pytest>=7.0.0 ; extra == "dev"
|
|
25
|
+
Requires-Dist: pytest-cov>=4.0.0 ; extra == "dev"
|
|
26
|
+
Requires-Dist: black>=23.0.0 ; extra == "dev"
|
|
27
|
+
Requires-Dist: ruff>=0.1.0 ; extra == "dev"
|
|
28
|
+
Requires-Dist: mypy>=1.0.0 ; extra == "dev"
|
|
29
|
+
Requires-Dist: isort>=5.13.2 ; extra == "dev"
|
|
30
|
+
Project-URL: Documentation, https://github.com/Fractal-Forge/fractal-events#readme
|
|
31
|
+
Project-URL: Homepage, https://github.com/Fractal-Forge/fractal-events
|
|
32
|
+
Project-URL: Issues, https://github.com/Fractal-Forge/fractal-events/issues
|
|
33
|
+
Project-URL: Repository, https://github.com/Fractal-Forge/fractal-events
|
|
34
|
+
Provides-Extra: dev
|
|
35
|
+
|
|
36
|
+
# Fractal Events
|
|
37
|
+
|
|
38
|
+
> Fractal Events is event sourcing for the Fractal stack: events, a publisher that fans them out to projectors, an event store to keep them in, and the projectors that print them, persist them, or turn them back into commands.
|
|
39
|
+
|
|
40
|
+
[![PyPI Version][pypi-image]][pypi-url]
|
|
41
|
+
[![Build Status][build-image]][build-url]
|
|
42
|
+
|
|
43
|
+
<!-- Badges -->
|
|
44
|
+
|
|
45
|
+
[pypi-image]: https://img.shields.io/pypi/v/fractal-events
|
|
46
|
+
[pypi-url]: https://pypi.org/project/fractal-events/
|
|
47
|
+
[build-image]: https://github.com/Fractal-Forge/fractal-events/actions/workflows/build.yml/badge.svg
|
|
48
|
+
[build-url]: https://github.com/Fractal-Forge/fractal-events/actions/workflows/build.yml
|
|
49
|
+
|
|
50
|
+
## Installation
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
pip install fractal-events
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Background
|
|
57
|
+
|
|
58
|
+
An event says something already happened: *this road was added*. A command asks
|
|
59
|
+
for something to happen. Keeping those apart is what makes the write side of an
|
|
60
|
+
application replayable — events are facts, and facts can be stored, re-read and
|
|
61
|
+
projected onto as many things as you like without any of those things knowing
|
|
62
|
+
about each other.
|
|
63
|
+
|
|
64
|
+
That is what this package is: the fact, the fan-out, and somewhere to keep the
|
|
65
|
+
facts.
|
|
66
|
+
|
|
67
|
+
## Publishing
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from fractal_events import EventPublisher, PrintEventProjector
|
|
71
|
+
|
|
72
|
+
publisher = EventPublisher([PrintEventProjector()])
|
|
73
|
+
publisher.publish_event(RoadAddedEvent(command=command, road_id="1"))
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Events published together share one stream id; separate calls get separate
|
|
77
|
+
ones. Every projector sees every event — the publisher does not filter, and
|
|
78
|
+
projectors do not know about each other.
|
|
79
|
+
|
|
80
|
+
## Projectors
|
|
81
|
+
|
|
82
|
+
- **`PrintEventProjector`** — writes each event as one JSON line. Useful in
|
|
83
|
+
development, and the cheapest possible audit trail.
|
|
84
|
+
- **`EventStoreProjector`** — commits each event to an `EventStore`.
|
|
85
|
+
- **`CommandBusProjector`** — maps events back onto commands, or onto Processes.
|
|
86
|
+
|
|
87
|
+
### Mapping events back to commands
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
from fractal_events import CommandBusProjector, EventCommandMapper
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
class RebuildOnRoadAdded(EventCommandMapper):
|
|
94
|
+
def mappers(self):
|
|
95
|
+
return {RoadAddedEvent: [lambda e: RebuildCommand(road_id=e.road_id)]}
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
projector = CommandBusProjector(
|
|
99
|
+
lambda: context.command_bus,
|
|
100
|
+
[RebuildOnRoadAdded()],
|
|
101
|
+
)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
**Give this projector a lenient bus.** Fan-out legitimately produces commands
|
|
105
|
+
this particular deployment has no handler for — that is normal, not broken. A
|
|
106
|
+
`CommandBus()` is strict and will raise; construct it as
|
|
107
|
+
`CommandBus(strict=False)` for a projector, so a miss is logged rather than
|
|
108
|
+
thrown. A strict bus is right for a service call, where a missing handler does
|
|
109
|
+
mean the application is wired wrong.
|
|
110
|
+
|
|
111
|
+
### Mapping events to Processes
|
|
112
|
+
|
|
113
|
+
Process mappers run a stateful workflow instead of dispatching a command. They
|
|
114
|
+
need [fractal-processes](https://github.com/Fractal-Forge/fractal-processes)
|
|
115
|
+
(`pip install fractal-processes`), which is imported only when a process mapper
|
|
116
|
+
actually fires, and they need an application context:
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
projector = CommandBusProjector(
|
|
120
|
+
lambda: context.command_bus,
|
|
121
|
+
[],
|
|
122
|
+
process_mappers=[BuildRoadWorkflow()],
|
|
123
|
+
context_func=lambda: context,
|
|
124
|
+
)
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
`context_func` is injected rather than imported. The application context lives a
|
|
128
|
+
layer above this package, and importing it here would point the middle of the
|
|
129
|
+
stack at the top for one constructor call. Leaving it out while passing process
|
|
130
|
+
mappers raises at construction time, not on whichever event happens to arrive
|
|
131
|
+
first.
|
|
132
|
+
|
|
133
|
+
## Event stores
|
|
134
|
+
|
|
135
|
+
Four flavours, differing only in how an event is stored and read back:
|
|
136
|
+
|
|
137
|
+
| store | stored as | needs |
|
|
138
|
+
|---|---|---|
|
|
139
|
+
| `ObjectEventStore` | the event object itself | an in-memory repository |
|
|
140
|
+
| `DictEventStore` | `asdict(event)` | the event classes, to rebuild them |
|
|
141
|
+
| `JsonEventStore` | a JSON string | the event classes, plus an encoder |
|
|
142
|
+
| `PickleEventStore` | a pickle | — |
|
|
143
|
+
|
|
144
|
+
All of them take an `EventStoreRepository`, so where the messages actually land
|
|
145
|
+
is a [fractal-repositories](https://github.com/douwevandermeij/fractal-repositories)
|
|
146
|
+
concern rather than this package's.
|
|
147
|
+
|
|
148
|
+
Reading back an event whose class is no longer mapped raises
|
|
149
|
+
`EventNotMappedError` rather than skipping it — a gap in a replayed stream is
|
|
150
|
+
worse than a loud failure.
|
|
151
|
+
|
|
152
|
+
## Development
|
|
153
|
+
|
|
154
|
+
```sh
|
|
155
|
+
make dev-install
|
|
156
|
+
make test
|
|
157
|
+
make lint
|
|
158
|
+
make format
|
|
159
|
+
```
|
|
160
|
+
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# Fractal Events
|
|
2
|
+
|
|
3
|
+
> Fractal Events is event sourcing for the Fractal stack: events, a publisher that fans them out to projectors, an event store to keep them in, and the projectors that print them, persist them, or turn them back into commands.
|
|
4
|
+
|
|
5
|
+
[![PyPI Version][pypi-image]][pypi-url]
|
|
6
|
+
[![Build Status][build-image]][build-url]
|
|
7
|
+
|
|
8
|
+
<!-- Badges -->
|
|
9
|
+
|
|
10
|
+
[pypi-image]: https://img.shields.io/pypi/v/fractal-events
|
|
11
|
+
[pypi-url]: https://pypi.org/project/fractal-events/
|
|
12
|
+
[build-image]: https://github.com/Fractal-Forge/fractal-events/actions/workflows/build.yml/badge.svg
|
|
13
|
+
[build-url]: https://github.com/Fractal-Forge/fractal-events/actions/workflows/build.yml
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
pip install fractal-events
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Background
|
|
22
|
+
|
|
23
|
+
An event says something already happened: *this road was added*. A command asks
|
|
24
|
+
for something to happen. Keeping those apart is what makes the write side of an
|
|
25
|
+
application replayable — events are facts, and facts can be stored, re-read and
|
|
26
|
+
projected onto as many things as you like without any of those things knowing
|
|
27
|
+
about each other.
|
|
28
|
+
|
|
29
|
+
That is what this package is: the fact, the fan-out, and somewhere to keep the
|
|
30
|
+
facts.
|
|
31
|
+
|
|
32
|
+
## Publishing
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
from fractal_events import EventPublisher, PrintEventProjector
|
|
36
|
+
|
|
37
|
+
publisher = EventPublisher([PrintEventProjector()])
|
|
38
|
+
publisher.publish_event(RoadAddedEvent(command=command, road_id="1"))
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Events published together share one stream id; separate calls get separate
|
|
42
|
+
ones. Every projector sees every event — the publisher does not filter, and
|
|
43
|
+
projectors do not know about each other.
|
|
44
|
+
|
|
45
|
+
## Projectors
|
|
46
|
+
|
|
47
|
+
- **`PrintEventProjector`** — writes each event as one JSON line. Useful in
|
|
48
|
+
development, and the cheapest possible audit trail.
|
|
49
|
+
- **`EventStoreProjector`** — commits each event to an `EventStore`.
|
|
50
|
+
- **`CommandBusProjector`** — maps events back onto commands, or onto Processes.
|
|
51
|
+
|
|
52
|
+
### Mapping events back to commands
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from fractal_events import CommandBusProjector, EventCommandMapper
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class RebuildOnRoadAdded(EventCommandMapper):
|
|
59
|
+
def mappers(self):
|
|
60
|
+
return {RoadAddedEvent: [lambda e: RebuildCommand(road_id=e.road_id)]}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
projector = CommandBusProjector(
|
|
64
|
+
lambda: context.command_bus,
|
|
65
|
+
[RebuildOnRoadAdded()],
|
|
66
|
+
)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
**Give this projector a lenient bus.** Fan-out legitimately produces commands
|
|
70
|
+
this particular deployment has no handler for — that is normal, not broken. A
|
|
71
|
+
`CommandBus()` is strict and will raise; construct it as
|
|
72
|
+
`CommandBus(strict=False)` for a projector, so a miss is logged rather than
|
|
73
|
+
thrown. A strict bus is right for a service call, where a missing handler does
|
|
74
|
+
mean the application is wired wrong.
|
|
75
|
+
|
|
76
|
+
### Mapping events to Processes
|
|
77
|
+
|
|
78
|
+
Process mappers run a stateful workflow instead of dispatching a command. They
|
|
79
|
+
need [fractal-processes](https://github.com/Fractal-Forge/fractal-processes)
|
|
80
|
+
(`pip install fractal-processes`), which is imported only when a process mapper
|
|
81
|
+
actually fires, and they need an application context:
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
projector = CommandBusProjector(
|
|
85
|
+
lambda: context.command_bus,
|
|
86
|
+
[],
|
|
87
|
+
process_mappers=[BuildRoadWorkflow()],
|
|
88
|
+
context_func=lambda: context,
|
|
89
|
+
)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
`context_func` is injected rather than imported. The application context lives a
|
|
93
|
+
layer above this package, and importing it here would point the middle of the
|
|
94
|
+
stack at the top for one constructor call. Leaving it out while passing process
|
|
95
|
+
mappers raises at construction time, not on whichever event happens to arrive
|
|
96
|
+
first.
|
|
97
|
+
|
|
98
|
+
## Event stores
|
|
99
|
+
|
|
100
|
+
Four flavours, differing only in how an event is stored and read back:
|
|
101
|
+
|
|
102
|
+
| store | stored as | needs |
|
|
103
|
+
|---|---|---|
|
|
104
|
+
| `ObjectEventStore` | the event object itself | an in-memory repository |
|
|
105
|
+
| `DictEventStore` | `asdict(event)` | the event classes, to rebuild them |
|
|
106
|
+
| `JsonEventStore` | a JSON string | the event classes, plus an encoder |
|
|
107
|
+
| `PickleEventStore` | a pickle | — |
|
|
108
|
+
|
|
109
|
+
All of them take an `EventStoreRepository`, so where the messages actually land
|
|
110
|
+
is a [fractal-repositories](https://github.com/douwevandermeij/fractal-repositories)
|
|
111
|
+
concern rather than this package's.
|
|
112
|
+
|
|
113
|
+
Reading back an event whose class is no longer mapped raises
|
|
114
|
+
`EventNotMappedError` rather than skipping it — a gap in a replayed stream is
|
|
115
|
+
worse than a loud failure.
|
|
116
|
+
|
|
117
|
+
## Development
|
|
118
|
+
|
|
119
|
+
```sh
|
|
120
|
+
make dev-install
|
|
121
|
+
make test
|
|
122
|
+
make lint
|
|
123
|
+
make format
|
|
124
|
+
```
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Fractal Events
|
|
3
|
+
|
|
4
|
+
Event sourcing for the Fractal stack: events, the publisher that fans them out
|
|
5
|
+
to projectors, an event store to keep them in, and the projectors that print
|
|
6
|
+
them, persist them, or turn them back into commands.
|
|
7
|
+
|
|
8
|
+
Sits on fractal-core and fractal-commands and imports nothing above itself.
|
|
9
|
+
Process mappers need fractal-processes, which is optional and imported only
|
|
10
|
+
when one actually fires.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from fractal_events.event import (
|
|
14
|
+
BasicSendingEvent,
|
|
15
|
+
Event,
|
|
16
|
+
EventCommandMapper,
|
|
17
|
+
EventProcessMapper,
|
|
18
|
+
ReceivingEvent,
|
|
19
|
+
SendingEvent,
|
|
20
|
+
)
|
|
21
|
+
from fractal_events.event_projector import EventProjector
|
|
22
|
+
from fractal_events.event_publisher import EventPublisher
|
|
23
|
+
from fractal_events.event_store import (
|
|
24
|
+
BasicEventStore,
|
|
25
|
+
DictEventStore,
|
|
26
|
+
EventNotMappedError,
|
|
27
|
+
EventStore,
|
|
28
|
+
EventStoreRepository,
|
|
29
|
+
InMemoryEventStoreRepository,
|
|
30
|
+
JsonEventStore,
|
|
31
|
+
ObjectEventStore,
|
|
32
|
+
PickleEventStore,
|
|
33
|
+
)
|
|
34
|
+
from fractal_events.event_stream import EventStream
|
|
35
|
+
from fractal_events.message import Message
|
|
36
|
+
from fractal_events.projectors.command_bus_projector import CommandBusProjector
|
|
37
|
+
from fractal_events.projectors.event_store_projector import EventStoreProjector
|
|
38
|
+
from fractal_events.projectors.print_projector import PrintEventProjector
|
|
39
|
+
|
|
40
|
+
__version__ = "1.0.0"
|
|
41
|
+
|
|
42
|
+
__all__ = [
|
|
43
|
+
"BasicEventStore",
|
|
44
|
+
"BasicSendingEvent",
|
|
45
|
+
"CommandBusProjector",
|
|
46
|
+
"DictEventStore",
|
|
47
|
+
"Event",
|
|
48
|
+
"EventCommandMapper",
|
|
49
|
+
"EventNotMappedError",
|
|
50
|
+
"EventProcessMapper",
|
|
51
|
+
"EventProjector",
|
|
52
|
+
"EventPublisher",
|
|
53
|
+
"EventStore",
|
|
54
|
+
"EventStoreProjector",
|
|
55
|
+
"EventStoreRepository",
|
|
56
|
+
"EventStream",
|
|
57
|
+
"InMemoryEventStoreRepository",
|
|
58
|
+
"JsonEventStore",
|
|
59
|
+
"Message",
|
|
60
|
+
"ObjectEventStore",
|
|
61
|
+
"PickleEventStore",
|
|
62
|
+
"PrintEventProjector",
|
|
63
|
+
"ReceivingEvent",
|
|
64
|
+
"SendingEvent",
|
|
65
|
+
]
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Type
|
|
4
|
+
|
|
5
|
+
from fractal_commands import Command
|
|
6
|
+
|
|
7
|
+
if TYPE_CHECKING:
|
|
8
|
+
# Type-only: process mappers are an optional feature and fractal-processes
|
|
9
|
+
# is an optional dependency, so this must never become a runtime import.
|
|
10
|
+
from fractal_processes.process import Process
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass
|
|
14
|
+
class Event(object):
|
|
15
|
+
pass
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass
|
|
19
|
+
class BasicSendingEvent(Event, ABC):
|
|
20
|
+
@property
|
|
21
|
+
@abstractmethod
|
|
22
|
+
def object_id(self):
|
|
23
|
+
raise NotImplementedError
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
@abstractmethod
|
|
27
|
+
def aggregate_root_id(self):
|
|
28
|
+
raise NotImplementedError
|
|
29
|
+
|
|
30
|
+
@property
|
|
31
|
+
@abstractmethod
|
|
32
|
+
def aggregate_root_type(self):
|
|
33
|
+
raise NotImplementedError
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass
|
|
37
|
+
class SendingEvent(BasicSendingEvent, ABC):
|
|
38
|
+
command: Command
|
|
39
|
+
|
|
40
|
+
@staticmethod
|
|
41
|
+
def to_event(event: Any):
|
|
42
|
+
return event
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@dataclass
|
|
46
|
+
class ReceivingEvent(Event):
|
|
47
|
+
def to_command(self):
|
|
48
|
+
raise NotImplementedError
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class EventCommandMapper(ABC):
|
|
52
|
+
@abstractmethod
|
|
53
|
+
def mappers(self) -> Dict[Type[Event], List[Callable[[Event], List[Command]]]]:
|
|
54
|
+
raise NotImplementedError
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class EventProcessMapper(ABC):
|
|
58
|
+
"""Maps events to Process workflows for complex orchestration with state."""
|
|
59
|
+
|
|
60
|
+
@abstractmethod
|
|
61
|
+
def mappers(self) -> Dict[Type[Event], List[Callable[[Event], "Process"]]]:
|
|
62
|
+
"""
|
|
63
|
+
Returns mapping of Event types to Process factory functions.
|
|
64
|
+
|
|
65
|
+
The Process factory receives an Event and returns a Process instance
|
|
66
|
+
that will be executed with a ProcessContext containing fractal.context.
|
|
67
|
+
|
|
68
|
+
Example:
|
|
69
|
+
{
|
|
70
|
+
HouseAddedEvent: [lambda event: create_house_workflow(event)],
|
|
71
|
+
UserRegisteredEvent: [lambda event: create_user_workflow(event)],
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
Dictionary mapping Event types to lists of Process factory callables
|
|
76
|
+
"""
|
|
77
|
+
raise NotImplementedError
|