Skip to main content

Fractal Application

Fractal Application is the top of the Fractal stack: the application context that installs an application's repositories and services, wires its command bus and event publisher, and health-checks them on startup.

PyPI Version Build Status

Installation

pip install fractal-application
pip install fractal-application[fastapi]        # FastAPI ingress
pip install fractal-application[gcp]            # Firestore event store, Pub/Sub projector
pip install fractal-application[processes]      # event → Process mappers

Background

This is the layer that knows about all the others. fractal-core, fractal-commands, fractal-events and fractal-processes are all deliberately ignorant of it, which is what lets any of them be used on their own. Everything that has to know how the pieces fit together lives here.

The application context

from fractal_application import ApplicationContext
from fractal_core import Settings


class AppSettings(Settings):
    def load(self):
        self.EVENT_STORE_BACKEND = os.getenv("EVENT_STORE_BACKEND", "memory")


class Context(ApplicationContext):
    settings = AppSettings()


@ApplicationContext.register_repository("user_repository")
def select_user_repository(settings):
    return MongoUserRepository(settings.MONGO_URL)


@ApplicationContext.register_command_handler
class AddUserCommandHandler(CommandHandler):
    ...


context = Context()          # loads services, repositories, bus, publisher
context.user_repository      # installed and health-checked
context.command_bus          # every registered handler already on it

Construction is the wiring check. load() installs internal services, repositories, egress services, the event publisher, the command bus and ingress services, then asserts every repository and service reports itself healthy. A context that constructed is a context whose adapters answered.

Registration happens through decorators at import time, which means it is a side effect of the import graph — if a module holding handlers is never imported, its handlers are never registered. fractal-commands raises on a command with no handler precisely because that failure is otherwise silent.

Fractal

from fractal_application import Fractal


class MyService(Fractal):
    settings = AppSettings()
    context = Context()

Instantiating it asserts the service was wired: a Fractal without settings or without a context refuses to construct rather than failing later on the first attribute nobody set.

contrib

FastAPI

from fractal_application.contrib.fastapi.install import install_fastapi

app = install_fastapi(context, title="My Service")

Also provides the default REST routers, token/role dependencies, an error response model and a JSON encoder that understands pydantic models.

Google Cloud

FirestoreEventStoreRepository backs the event store with Firestore (selected by EVENT_STORE_BACKEND=firestore), and PubSubEventBusProjector publishes events onto a Pub/Sub topic when GCP_PROJECT_ID is set.

Processes

Event → Process mappers need fractal-processes. The context passes itself to the projector as context_func, so the process engine gets an application context without fractal-events ever importing this package.

Development

make dev-install
make test
make lint
make format

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fractal_application-1.0.0.tar.gz (18.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fractal_application-1.0.0-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file fractal_application-1.0.0.tar.gz.

File metadata

  • Download URL: fractal_application-1.0.0.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.34.2

File hashes

Hashes for fractal_application-1.0.0.tar.gz
Algorithm Hash digest
SHA256 75d2159a32c5bdd0ee3b644336f24362eff66e177d67e5df9ac35d3a9b801583
MD5 c63d9588ec384563b6a1c59647d39602
BLAKE2b-256 2354035a21df7c15be9ab35da4de603e7f196c8f9e0a86c9c144ec8c3ae8a4c7

See more details on using hashes here.

File details

Details for the file fractal_application-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fractal_application-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d26f2111167c6f9129826c01fc77e351e0b4aa6eddcc8023bdd5ed4f1ec010d3
MD5 56b40a6eb6b6b4e3d07e008760808107
BLAKE2b-256 d6a43113a29d7a65ec0324252e096ca37cd5689d4b9bd637f56b31d76cf6f670

See more details on using hashes here.

Release history Release notifications | RSS feed

2.0.0

2 files

1.2.0

2 files

1.1.0

2 files

This release

1.0.0 This release

2 files

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