Skip to main content

Explicit, lifecycle-scoped control of side effects

Project description

airlock

Express side effects anywhere. Control whether & when they escape.

Tests Docs codecov

Airlock diagram

tl;dr

import airlock

class Order:
    def process(self):
        self.status = "processed"
        airlock.enqueue(notify_warehouse, self.id)
        airlock.enqueue(send_confirmation_email)

The execution context decides when (and whether) your side effects actually get dispatched:

# Production API endpoint: flush at end of request
with airlock.scope():
    order.process()
# side effects dispatch here

# Migration: suppress everything
with airlock.scope(policy=airlock.DropAll()):
    order.process()
# nothing dispatched

# Test: fail if anything tries to escape
with airlock.scope(policy=airlock.AssertNoEffects()):
    order.hopefully_pure_function() # raises if any enqueue() called

# Test: surface the side effects
with airlock.scope(policy=airlock.DropAll()) as scope:
    order.process()
    assert len(scope.intents) == 2
    print((intent.name, intent.args, intent.kwargs) for intent in scope.intents)

# Admin API endpoint: selective control
with airlock.scope(policy=airlock.BlockTasks({"send_confirmation_email"})) as scope:
    order.process()
    assert len(scope.intents) == 2  # the blocked task remains enqueued while we're in the scope
# side effects dispatch or discard here -- warehouse notified, but no confirmation email sent 

Using Django? Maybe with Celery?

# settings.py
MIDDLEWARE = [
    # ... other middleware ...
    "airlock.integrations.django.AirlockMiddleware",
]

# models.py
import airlock
from . import tasks

class Order(models.Model):
    def process(self):
        self.status = "processed"
        self.save()
        airlock.enqueue(tasks.send_confirmation_email, order_id=self.id)
        airlock.enqueue(tasks.notify_warehouse, order_id=self.id)

# views.py
def checkout(request):
    order = Order.objects.get(id=request.POST['order_id'])
    order.process()
    return HttpResponse("OK")
# Celery tasks dispatch in middleware, after transaction has committed

Read more: Django integration

Installation

pip install airlock-py

Documentation

Full documentation

Key pages:

Contributing

See CONTRIBUTING.md for development setup.

License

MIT

Project details


Download files

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

Source Distribution

airlock_py-0.1.0a2.tar.gz (46.5 kB view details)

Uploaded Source

Built Distribution

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

airlock_py-0.1.0a2-py3-none-any.whl (24.9 kB view details)

Uploaded Python 3

File details

Details for the file airlock_py-0.1.0a2.tar.gz.

File metadata

  • Download URL: airlock_py-0.1.0a2.tar.gz
  • Upload date:
  • Size: 46.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.6

File hashes

Hashes for airlock_py-0.1.0a2.tar.gz
Algorithm Hash digest
SHA256 fe2de902d2dd22bc5ba9887dae09c380e1c9ddeea4dad93a61fc8634aba9fa88
MD5 ca98d01c396d2c1b98658cf2d30f12f9
BLAKE2b-256 769cb9545bef3e1378a4c91f53f602d240ce850bb27449e4aa3ed36d73de9550

See more details on using hashes here.

File details

Details for the file airlock_py-0.1.0a2-py3-none-any.whl.

File metadata

  • Download URL: airlock_py-0.1.0a2-py3-none-any.whl
  • Upload date:
  • Size: 24.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.6

File hashes

Hashes for airlock_py-0.1.0a2-py3-none-any.whl
Algorithm Hash digest
SHA256 f2f76563465a89dd69c0e72a035dc52f68e974f580c17100135da0671ef2ab9b
MD5 df6ee1e6e0d5794b004197c0c6a36a42
BLAKE2b-256 b5180b8672bb7c6b1381dea61728e52f2ae8738e529319cd96e830c50a92a93b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page