Use posix select combined with pg listen to provide mechanism to wake up sleeping processes

Registered by Thomas Adamcik

Using the method shown in https://dndg.it/cgi-bin/gitweb.cgi?p=public/psycopg2.git;a=blob;f=examples/notify.py;hb=HEAD we can create a general sleep method for NAV that can be short-circuited with a NOTIFY call in postgres. This proposal can either provide a new sleep method that takes the keyword to listen for and timeout or extend the existing methid nav.daemon.safesleep.

The direct motivation for this proposal is to use the new function in alertengine and eventengine so that alertengine runs as soon as possible with respect to new alerts.

Blueprint information

Status:
Not started
Approver:
None
Priority:
Undefined
Drafter:
None
Direction:
Needs approval
Assignee:
None
Definition:
New
Series goal:
None
Implementation:
Unknown
Milestone target:
None

Related branches

Sprints

Whiteboard

Quick mockup implementation that does not handle any DB-errors:

import select

from nav.daemon import safesleep
from nav.db import getConnection

def sleep(timeout, notify=None):
    if not notify:
        safesleep(timeout)
        return

    connection = getConnection('nav')
    cursor = connection.cursor()
    cursor.execute('LISTEN %s', [notify])

    select.select([cursor], [], [], timeout)

    cursor.isready()

    while cursor.connection.notifies:
        cursor.connection.notifies.pop()

def notify(event):
    connection = getConnection('nav')
    cursor = connection.cursor()
    cursor.execute('NOTIFY %s', [event])

----

Did not know this blueprint existed, but something like this was implemented for the initial prototype of an eventengine rewrite, see https://bitbucket.org/mbrekkevold/nav-mirror/src/14a44e5190e6/python/nav/eventengine/__init__.py#cl-51

(?)

Work Items

This blueprint contains Public information 
Everyone can see this information.

Subscribers

No subscribers.