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.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 and LOG_FORMAT properties.

Applications that are subclass of this base class must implement two async member functions: start() and stop(). The start() 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, etc. The stop() 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 the start() function and enters into its internal wait() function, which is waiting for a termination signal.

The wait() function also keep running those asynchronous tasks, that were created via the start() function.

It is also possible to run some extra jobs, because the ApplicationBase will call its async jobs() function before it enters its wait function. The default implementation of jobs() is empty. You can overload it with your implementation. It is also possible to execute the terminate() function at the end of the jobs() 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:

  1. Starts the internal services, defined by the start() member function.

  2. Executes the jobs() member function.

  3. Enters the internal event processing execution loop, and wait until either a an unhandled interruption occur, or the application receives a signal for stopping.

  4. Shuts down the internal services according to the implementation of the stop() function.

  5. 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