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

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:

from brewblox_service import brewblox_logger

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

service.py

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

The shortest implementation is:

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:

# 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

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.

mqtt.py

The replacement for the AMQP-based events module.

The major advantages over AMQP is that the protocol is simpler, and supports websocket transport. This allows for it to be used outside a Brewblox network.

Messages have a /-separated topic and a JSON-serialized body.

To publish data, all you need is the publish(topic, message) function.

To listen to incoming messages, you can combine subscribe(topic) with one or more calls to listen(topic, callback). The subscribe/listen functions allow for + and # wildcards to be used.

For a detailed explanation of how to use MQTT topics, see http://www.steves-internet-guide.com/understanding-mqtt-topics/.

For the Brewblox spec on how and where to publish data, see https://brewblox.netlify.app/dev/reference/event_logging.html.

Includes top-level convenience functions for publish(topic, message), listen(topic, callback) and subscribe(topic).

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.29.0.tar.gz (28.9 kB view details)

Uploaded Source

Built Distribution

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

brewblox_service-0.29.0-py3-none-any.whl (32.3 kB view details)

Uploaded Python 3

File details

Details for the file brewblox-service-0.29.0.tar.gz.

File metadata

  • Download URL: brewblox-service-0.29.0.tar.gz
  • Upload date:
  • Size: 28.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.8.0 tqdm/4.47.0 CPython/3.7.7

File hashes

Hashes for brewblox-service-0.29.0.tar.gz
Algorithm Hash digest
SHA256 579f1aecab313dd549e26abef78d7699275b7be013e8fd85cf5f8ac2ca71ee35
MD5 3b3b5272e5d65a975b0be093e4602e56
BLAKE2b-256 d109401299444d404a3a09a1d7c3c3ebbaab320250974ab05c71becf7d68fc98

See more details on using hashes here.

File details

Details for the file brewblox_service-0.29.0-py3-none-any.whl.

File metadata

  • Download URL: brewblox_service-0.29.0-py3-none-any.whl
  • Upload date:
  • Size: 32.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.8.0 tqdm/4.47.0 CPython/3.7.7

File hashes

Hashes for brewblox_service-0.29.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6e5fce82989c9477f75e596c82ed4b24f2b2fa918d77a19bc3c7cdb4d36b4c8d
MD5 26c10687a738fb441962bc3c9284eca7
BLAKE2b-256 33f649d71fbeea7d14b002b5e66f27ed263001beab1db629e33ca705cf2838c4

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