Modules and Services¤
ASAB applications contain several Services. Each Service is located in a separate Module.
Registering Modules and Services¤
class MyApplication(asab.Application):
async def initialize(self):
from my_module import MyModule
self.add_module(MyModule) #(1)!
self.MyService = self.get_service("MyService") #(2)!
...
# ...somewhere in the code
def custom_function():
my_service = app.Services.get("MyService") #(3)!
my_service.do_stuff()
- The method
add_module()initializes and adds a new module. Themodule_classclass will be instantiated during the method call. - The method
get_service()locates a service by its name and returns theasab.Serviceobject. - Modules that have been added to the application are stored in
asab.Application.Moduleslist. Similarly, Services are stored inasab.Application.Servicesdictionary.
Built-in Services¤
Table of ASAB built-in Services and Modules:
| Service | Module | Features |
|---|---|---|
WebService |
asab.web |
Creating a web server |
StorageService |
asab.storage |
Storing the data in various databases |
LibraryService |
asab.library |
Reading the data from various sources |
ZooKeeperService |
asab.zookeeper |
Synchronizing data with Apache Zookeeper |
MetricService |
asab.metric |
Analysis of the application state in a timescale manner |
AlertService |
asab.alert |
Integration of Alert Managers |
TaskService |
asab.task |
Execution of one-off background tasks |
ProactorService |
asab.proactor |
Running CPU bound operations asynchronously |
ApiService |
asab.api |
Implementation of Swagger documentation |
asab.Module
¤
Bases: ABC
Abstract class for ASAB modules.
Modules are registered at the module registry asab.Application.Modules, managed by an application object.
Module can be loaded by ASAB and typically provides one or more Service objects.
Every module provides asynchronous methods initialize() and finalize().
initialize() is called in the very beginning of the run-time,
finalize() in the gentle shut-down of the application.
Examples:
Recommended structure of the ASAB module:
Content of __init__.py:
import asab
from .my_service import MyService
# Extend ASAB configuration defaults
asab.Config.add_defaults({
'my_module': {
'foo': 'bar'
}
})
class MyModule(asab.Module):
def __init__(self, app):
super().__init__(app)
self.service = MyService(app, "MyService")
And this is how the module is loaded:
Source code in asab/abc/module.py
finalize(app)
async
¤
This method is called when the Module is finalized, e.g., during application exit-time. It can be overridden by an user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
Application
|
Reference to ASAB application. |
required |
initialize(app)
async
¤
This method is called when the Module is initialized. It can be overridden by an user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
Application
|
Reference to ASAB application. |
required |
asab.Service
¤
Bases: ABC
Abstract class for ASAB services.
Service objects are registered at the service registry asab.Application.Services, managed by an application object.
Examples:
This is how Service is created and registered:
This is how Service is located and used:
Example of a typical Service class skeleton:
class MyService(asab.Service):
def __init__(self, app, service_name):
super().__init__(app, service_name)
...
async def initialize(self, app):
...
async def finalize(self, app):
...
def service_method(self):
...
Source code in asab/abc/service.py
App = app
instance-attribute
¤
The reference to the Application object.
Name = service_name
instance-attribute
¤
The reference name for the Service.
__init__(app, service_name)
¤
Register the service to asab.Application.Services dictionary with the provided service_name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
Application
|
Reference to ASAB application. |
required |
service_name
|
str
|
Reference name of the Service. |
required |
Source code in asab/abc/service.py
finalize(app)
async
¤
This method is called when the Service is finalized, e.g., during application exit-time.
It can be overridden by an user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
Application
|
Reference to ASAB application. |
required |
initialize(app)
async
¤
This method is called when the Service is initialized. It can be overridden by an user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
Application
|
Reference to ASAB application. |
required |