common.app.app_base module
The application base class 1.
- 1(1,2)
The KeyboardInterrupt handling, and signal handler shadowing is implemented based on the Holy grail of graceful shutdown in Python. Credits to Petr Beneš (wbenny).
- class common.app.app_base.ApplicationBase(config)
Bases:
ABC
Abstract base class for applications 1.
It is responsible for
storing the configuration,
setting up the central logger,
starting internal services,
keep running until graceful shut-down (caused by a termination signal, or internal error),
stop the internal services when shuts down.
When you create a new instance of a derived application class, you are handing over the actual deployment-dependent configuration to it. The constructor of the base class stores this config, as well as it initializes the logger, according to the configuration’s
LOG_LEVEL
andLOG_FORMAT
properties.Applications that are subclass of this base class must implement two
async
member functions:start()
andstop()
. Thestart()
will be called by therun()
function, when the application starts running. You can put those operations here, for example, that initializes resources, open database connections, etc. Thestop()
will be called, when the application is shutting down. This is, where you can put those operations that needs to be executed, before the application exits, e.g. closing database connections.After creating the application class, you only need to call the
run()
member function, then the application will start running, calls thestart()
function and enters into its internalwait()
function, which is waiting for a termination signal.The
wait()
function also keep running those asynchronous tasks, that were created via thestart()
function.It is also possible to run some extra jobs, because the
ApplicationBase
will call itsasync jobs()
function before it enters its wait function. The default implementation ofjobs()
is empty. You can overload it with your implementation. It is also possible to execute theterminate()
function at the end of thejobs()
function, then the application will automatically shuts down, after finished the jobs.- __init__(config)
Initializes the new application object.
- Parameters
config (Config) – the configuration object of the overall application.
It creates an internal event-loop, that will be used to create and run the application’s internal services, tasks.
- async jobs()
The subclasses can place here their jobs, that run after start() finished.
- run()
Runs the application according to the following steps:
Starts the internal services, defined by the
start()
member function.Executes the
jobs()
member function.Enters the internal event processing execution loop, and wait until either an unhandled interruption occur, or the application receives a signal for stopping.
Shuts down the internal services according to the implementation of the
stop()
function.Exits.
- abstract async start()
Starts the application
This function will be called by the
run()
function, when the application starts running. You can put those operations here, for example, that initializes resources, open database connections, starts tasks, etc.This is an abstract member function that the subclass of ApplicationBase class must implement.
- abstract async stop()
Shuts down the application
It will be called, when the application is shutting down. This is, where you can put those operations that needs to be executed, before the application exits, e.g. closing database connections.
This is an abstract member function that the subclass of ApplicationBase class must implement.
- async wait()
Wait until the application got stop signal
- class common.app.app_base.HealthCheckMock(logger)
Bases:
object
Mock class of HealthCheck to swallow the set_state_* calls
- __init__(logger)
- set_state_warm_up()
Mock set_state_warm_up method
- set_state_working()
Mock set_state_working method
- set_state_shut_down()
Mock set_state_shut_down method
- set_state_no_info()
Mock set_state_no_info method