linod: asynchronous Lino daemon

The lino.modlib.linod plugin adds functionality for background work such as running scheduled tasks, sending notifications, and logging to the lino.log from multiple processes. It uses the ASGI interface to run an asynchronous Lino daemon process.

What it does

The Lino daemon process is responsible for running the scheduled background jobs defined by your application. This includes for example:

Usage

Applications can register a job using the dd.api.schedule_often() or dd.api.schedule_daily() decorators. For example (taken from lino.modlib.checkdata):

@dd.schedule_daily()
def checkdata():
    """Run all data checkers."""
    check_data(fix=False)

Applications can also use the schedule API directly for registering the jobs. For example:

if settings.SITE.use_linod:
    def func():
        ...
    schedule.every(2).months.do(func)

The code for both examples above should be in one of your application's models.py modules.

You can see a list of these jobs by running:

$ cd ~/projects/mysite
$ python manage.py linod --list

How to activate

A site maintainer can activate this plugin automatically by setting the use_lino site setting to True.

use_linod

Whether to install the lino.modlib.linod plugin and activate asynchronous functionality.

Simulating the daemon in a developer environment

To run the Lino daemon in a development environment run python manage.py runworker linod_{{project_name}} here if your project name is my_project you will need to run python manage.py runworker linod_my_project. And afterwards to initiate the background tasks and logging you will have to run the python manage.py initiate_linod command in an another terminal.

Usage for developers

The lino.modlib.linod plugin is dependent on a running redis-server in the background and also requires the django-channels, channels-redis and schedule Python packages to be installed.

To install redis on a Debian-based Linux distribution run the following command as root:

$ apt update
$ apt install redis

To install the dependent python packages run the following command after activating your Python environment:

$ pip install django-channels channels-redis schedule

Now you simply go to your project directory and invoke the admin command:

$ cd ~/projects/mysite
$ python manage.py linod_mysite

This process will run as long as you don't kill it, e.g. until you hit Ctrl-C.

Open up another terminal window, on which invoke the following command to initiate the background task's cor-routine and logger cor-routine:

$ cd ~/projects/mysite
$ python manage.py initiate_linod