Skip to content

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()
  1. The method add_module() initializes and adds a new module. The module_class class will be instantiated during the method call.
  2. The method get_service() locates a service by its name and returns the asab.Service object.
  3. Modules that have been added to the application are stored in asab.Application.Modules list. Similarly, Services are stored in asab.Application.Services dictionary.

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:

my_module/
        - __init__.py
        - my_service.py

Content of __init__.py:

__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:

from mymodule import MyModule
...
app.add_module(MyModule)
Source code in asab/abc/module.py
class Module(abc.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:

	```
	my_module/
		- __init__.py
		- my_service.py
	```

	Content of `__init__.py`:

	```python title="__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:

	```python
	from mymodule import MyModule
	...
	app.add_module(MyModule)
	```

	"""

	def __init__(self, app):
		pass

	# Lifecycle

	async def initialize(self, app):
		"""
		This method is called when the Module is initialized. It can be overridden by an user.

		Args:
			app (asab.Application): Reference to ASAB application.
		"""
		pass

	async def finalize(self, app):
		"""
		This method is called when the Module is finalized, e.g., during application `exit-time`. It can be overridden by an user.

		Args:
			app (asab.Application): Reference to ASAB application.
		"""
		pass

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
Source code in asab/abc/module.py
async def finalize(self, app):
	"""
	This method is called when the Module is finalized, e.g., during application `exit-time`. It can be overridden by an user.

	Args:
		app (asab.Application): Reference to ASAB application.
	"""
	pass

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
Source code in asab/abc/module.py
async def initialize(self, app):
	"""
	This method is called when the Module is initialized. It can be overridden by an user.

	Args:
		app (asab.Application): Reference to ASAB application.
	"""
	pass

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:

my_service = MyService(app, "my_service")

This is how Service is located and used:

my_service = app.get_service("my_service")
my_service.service_method()

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
class Service(abc.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:
	```python
	my_service = MyService(app, "my_service")
	```

	This is how Service is located and used:

	```python
	my_service = app.get_service("my_service")
	my_service.service_method()
	```

	Example of a typical Service class skeleton:

	```python
	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):
			...
	```
	"""

	def __init__(self, app, service_name: str):
		"""
		Register the service to `asab.Application.Services` dictionary with the provided `service_name`.

		Args:
			app (asab.Application): Reference to ASAB application.
			service_name: Reference name of the Service.
		"""
		self.Name = service_name
		"""
		The reference name for the Service.
		"""
		self.App = app
		"""
		The reference to the Application object.
		"""
		app._register_service(self)

	# Lifecycle

	async def initialize(self, app):
		"""
		This method is called when the Service is initialized.
		It can be overridden by an user.

		Args:
			app (asab.Application): Reference to ASAB application.
		"""
		pass

	async def finalize(self, app):
		"""
		This method is called when the Service is finalized, e.g., during application `exit-time`.
		It can be overridden by an user.

		Args:
			app (asab.Application): Reference to ASAB application.
		"""
		pass

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
def __init__(self, app, service_name: str):
	"""
	Register the service to `asab.Application.Services` dictionary with the provided `service_name`.

	Args:
		app (asab.Application): Reference to ASAB application.
		service_name: Reference name of the Service.
	"""
	self.Name = service_name
	"""
	The reference name for the Service.
	"""
	self.App = app
	"""
	The reference to the Application object.
	"""
	app._register_service(self)

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
Source code in asab/abc/service.py
async def finalize(self, app):
	"""
	This method is called when the Service is finalized, e.g., during application `exit-time`.
	It can be overridden by an user.

	Args:
		app (asab.Application): Reference to ASAB application.
	"""
	pass

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
Source code in asab/abc/service.py
async def initialize(self, app):
	"""
	This method is called when the Service is initialized.
	It can be overridden by an user.

	Args:
		app (asab.Application): Reference to ASAB application.
	"""
	pass