Skip to main content

Fractal Events

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.

PyPI Version Build Status

Installation

pip install fractal-events

Background

An event says something already happened: this road was added. A command asks for something to happen. Keeping those apart is what makes the write side of an application replayable — events are facts, and facts can be stored, re-read and projected onto as many things as you like without any of those things knowing about each other.

That is what this package is: the fact, the fan-out, and somewhere to keep the facts.

Publishing

from fractal_events import EventPublisher, PrintEventProjector

publisher = EventPublisher([PrintEventProjector()])
publisher.publish_event(RoadAddedEvent(command=command, road_id="1"))

Events published together share one stream id; separate calls get separate ones. Every projector sees every event — the publisher does not filter, and projectors do not know about each other.

Event identity and causation

Every sending event carries four keyword-only fields, set when it is created:

field meaning
event_id its own id (uuid4)
correlation_id the event_id of the event the chain started from; a source is its own
causation_id the event_id of the event whose handling created this one; empty for a source
depth links from the source: 0 for a source, one more per link

CommandBusProjector runs the mappers and Processes for an event inside caused_by(event), so every event created meanwhile continues that event's chain. Work that leaves the current context takes the cause along and restores it later:

from fractal_events import Cause, caused_by, current_cause

job.cause = current_cause().to_dict()          # when scheduling
with caused_by(Cause.from_dict(job.cause)):    # in the worker
    run(job)

The fields are left out of equality, so two events that say the same thing still compare equal. The event stores use event_id as the message id, so a live event and its replay share one key; events stored before identities existed read back with their message id as event_id, the same on every replay.

Projectors

  • PrintEventProjector — writes each event as one JSON line. Useful in development, and the cheapest possible audit trail.
  • EventStoreProjector — commits each event to an EventStore.
  • CommandBusProjector — maps events back onto commands, or onto Processes.

Mapping events back to commands

from fractal_events import CommandBusProjector, EventCommandMapper


class RebuildOnRoadAdded(EventCommandMapper):
    def mappers(self):
        return {RoadAddedEvent: [lambda e: RebuildCommand(road_id=e.road_id)]}


projector = CommandBusProjector(
    lambda: context.command_bus,
    [RebuildOnRoadAdded()],
)

Give this projector a lenient bus. Fan-out legitimately produces commands this particular deployment has no handler for — that is normal, not broken. A CommandBus() is strict and will raise; construct it as CommandBus(strict=False) for a projector, so a miss is logged rather than thrown. A strict bus is right for a service call, where a missing handler does mean the application is wired wrong.

When an event has both command mappers and process mappers, both run (before 1.2 a command mapper hid the Processes mapped to the same event).

Mapping events to Processes

Process mappers run a stateful workflow instead of dispatching a command. They need fractal-processes (pip install fractal-processes), which is imported only when a process mapper actually fires, and they need an application context:

projector = CommandBusProjector(
    lambda: context.command_bus,
    [],
    process_mappers=[BuildRoadWorkflow()],
    context_func=lambda: context,
)

context_func is injected rather than imported. The application context lives a layer above this package, and importing it here would point the middle of the stack at the top for one constructor call. Leaving it out while passing process mappers raises at construction time, not on whichever event happens to arrive first.

Event stores

Four flavours, differing only in how an event is stored and read back:

store stored as needs
ObjectEventStore the event object itself an in-memory repository
DictEventStore asdict(event) the event classes, to rebuild them
JsonEventStore a JSON string the event classes, plus an encoder
PickleEventStore a pickle —

All of them take an EventStoreRepository, so where the messages actually land is a fractal-repositories concern rather than this package's.

Reading back an event whose class is no longer mapped raises EventNotMappedError rather than skipping it — a gap in a replayed stream is worse than a loud failure.

Development

make dev-install
make test
make lint
make format

Metadata

Release files for fractal-events 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fractal-events 1.2.0
File Size Uploaded Provenance Actions
fractal_events-1.2.0.tar.gz 14.1 kB Without provenance Details for fractal_events-1.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fractal-events 1.2.0
File Interpreter ABI Platform Actions
fractal_events-1.2.0-py3-none-any.whl Python 3 none any Details for fractal_events-1.2.0-py3-none-any.whl

Total release size: 30.3 kB

Release files / fractal_events-1.2.0.tar.gz

Download URL fractal_events-1.2.0.tar.gz
Size 14.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ae09526ce4cc6259dc7a653b7ab615f821df7959b6ce66158ae0c6c3d53d0f3f
BLAKE2b-256 checksum
How to use checksums
ede7610f63095ee13f3e4b86a81e0ea0ecf693906d6120d53fb8f0a141e43f6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.34.2

Release files / fractal_events-1.2.0-py3-none-any.whl

Download URL fractal_events-1.2.0-py3-none-any.whl
Size 16.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
39edf666abc612e8fccbda26281a5a83ee0a895489d3709c7d7fda251b417f9b
BLAKE2b-256 checksum
How to use checksums
0accd2969eaae2e625c7266105aa70e3a822044246db516f83c6a6fae2a56171
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.34.2

Security for fractal-events 1.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page