fundamentals.daemonise

helper class to daemonise you code

Author

David Young

Date Created

May 23, 2023

Module Contents

Classes

daemonise

A class to daemonise a python code

API

class fundamentals.daemonise.daemonise(log, name, **akws)[source][source]

A class to daemonise a python code

Key Arguments:

  • log – logger

  • name – a name for the daemon (e.g. python package name)

Usage:

Add something like this to your command-line usage:

Usage:
myCommand (start|stop|restart|status)

Options:
    start                 start the myCommand daemon
    stop                  stop the myCommand daemon
    restart               restart the myCommand daemon
    status                print the staus of the myCommand daemon

and then when executing the commands:

from fundamental import daemonise
class myDaemon(daemonise):
    def action(
            self,
            **kwargs):
        self.log.info('starting the ``action`` method')

        anotherParameter = kwargs["anotherParameter"]

        import time
        while True:
            print(f"OVERRIDE ACTION - {anotherParameter}")
            time.sleep(3)

        self.log.info('completed the ``action`` method')
        return None

d = myDaemon(log=log, name="gocart", anotherParameter=42.)

if a['start']:
    d.start()
elif a['stop']:
    d.stop()
elif a['restart']:
    d.restart()
elif a['status']:
    d.status()

Replace **akws with any keywords you need.

Initialization

action(**akws)[source][source]

the code to execute in daemon mode, this method should be overriden to execute novel code

cleanup(signum, frame)[source][source]

the code to run when daemon is killed

restart()[source][source]

stop and start the daemon

start()[source][source]

start the daemonise running

status()[source][source]

print the status of the daemon

stop()[source][source]

stop the daemon and cleanup