Reference

Service class

class core_service.Service(*, loop=None, monitoring_interval: float = 0.1)

Base service class.

Your services should be inherited from this class.

Service class provides a basic asynchronous service lifecycle methods. You should extend it for your needs.

async healthcheck()

Healthcheck method.

This method can be invoked to check if service is healthy and running. Should raise core_service.exceptions.UnhealthyException if it is not healthy.

Default implementation checks that running flag is True. You can overload this method to add additional checks.

property log

Service logger with service name under the service key of extra

property loop

Event loop

async monitoring_task()

Monitoring task.

Started with a service. Run healthcheck periodically and force service to stop if it failed.

property name

Service name.

Default implementation return class name.

Used in log output etc. Should include unique identifiers.

async start()

Start service.

Set running flag to True, start service tasks, nested services and create monitoring task.

You can override this method in your service implementation to apply custom start logic. But don’t forget to invoke super implementation.

async stop()

Stop service.

Set should_stop flag to True, running to False and start shutdown sequence.

Nested services will be stopped first, service tasks will be cancelled than.

You can override this method in your service implementation to apply custom start logic. But don’t forget to invoke super implementation.

Decorators

core_service.requirements(*deps: List[str])

Decorator marking service method as a source of nested services list.

Marked method should return a list of service instances that should be instantiated.

Optional prop_name may contain name of object property with dependent service. Such method will be invoked only after attributes will be available, they will be a service and this service will be running.

core_service.task(periodic: bool = True, sleep_interval: float = 0.1, workers: int = 1)

Decorator defining Service method as service task.

Task will be started and stopped with a service.

Task is periodic by default. It means that after task execution finished it will be started again after sleeping for sleep_interval seconds.

Multiple instances of the service task can be started in parallel. It is started in single instance by default but you can control this behavior using workers argument.