Skip to main content

Scaffolding for BrewBlox backend services

Project description

# Scaffolding for BrewBlox service applications

In order to reduce code duplication between services, generic functionality is implemented here.

For an example on how to implement your own service based on `brewblox-service`, see <https://github.com/brewblox/brewblox-boilerplate>.

`brewblox-service` can technically be launched as a standalone application, but will not be very useful.

## [brewblox_service](./brewblox_service/__init__.py)

Small generic tools are defined here.

`brewblox_logger` can be used for creating module-specific loggers. It is not required, but will play a bit nicer with default formatting of the log.

Example:

```python
from brewblox_service import brewblox_logger

LOGGER = brewblox_logger(__name__)
LOGGER.info('hello')
```

## [service.py](./brewblox_service/service.py)

Parses commandline arguments, creates an `aiohttp` app, and runs it.

The shortest implementation is:

```python
app = service.create_app(default_name='my_service')
service.furnish(app)
service.run(app)
```

This will get you a working web application, but it will only support the `/_service/status` health check endpoint.

Applications can configure their own features, and add new commandline arguments.

Example:

```python
# Separately creating the parser allows adding arguments to the default set
parser = service.create_parser(default_name='my_service')
parser.add_argument('--my-arg')

# Now create the app
app = service.create_app(parser=create_parser())

# Add features for this service
device.setup(app)
api.setup(app)

# Furnish and run
service.furnish(app)
service.run(app)
```

## [features.py](./brewblox_service/features.py)

Many service features are application-scoped. Their lifecycle should span multiple requests, either because they are not request-driven, or because they manage asynchronous I/O operations (such as listening to AMQP messages).

The `ServiceFeature` class offers an abstract base class for this behavior. Implementing classes should define `startup(app)` and `shutdown(app)` functions, and those will be automatically called when the application starts up and shuts down.

Both `startup()` and `shutdown()` are called in an async context, making them the async counterparts of `__init__()` and `__del__()` functions.

Features must be constructed after the app is created, but before it starts running. (`service.create_app()` and `service.run(app)`)

The `add()` and `get()` functions make it easy to centrally declare a feature, and then use it in any function that has a reference to the aiohttp app.

## [events.py](./brewblox_service/events.py)

Both incoming and outgoing communication with the AMQP eventbus is handled here.

`EventListener` allows subscribing to eventbus messages. It will fire a callback when one is received. Subscriptions can be set at any time (also before the app starts running).

The listener is designed to gracefully degrade when the eventbus can't be reached. No errors will be raised, and it will periodically attempt to reconnect and restore its subscriptions.

For a practical implementation of `EventListener`, see [brewblox_history](https://github.com/BrewBlox/brewblox-history/blob/develop/brewblox_history/influx.py)

`EventPublisher` is responsible for sending new messages to the eventbus. A single publisher per application is sufficient.

In contrast with `EventListener`, the publisher will raise an exception when attempting to publish to an unreachable eventbus host.
It will attempt to reconnect for each subsequent message - no explicit connection management is required.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

brewblox-service-0.15.1.dev5.tar.gz (46.2 kB view details)

Uploaded Source

File details

Details for the file brewblox-service-0.15.1.dev5.tar.gz.

File metadata

  • Download URL: brewblox-service-0.15.1.dev5.tar.gz
  • Upload date:
  • Size: 46.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.0

File hashes

Hashes for brewblox-service-0.15.1.dev5.tar.gz
Algorithm Hash digest
SHA256 6419c42ab176d28d7af8d85c00e00ed07216d2f1fdf1a249a5c85af8c76dc8c6
MD5 9d1ecb5552d8480323a689c35f126637
BLAKE2b-256 f32847c04cb760c449c0ae59cdb6934d09ff867ec6c410603da6d7ac710f2ff5

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