daemonise (class)

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

Bases: object

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.

Methods

action(**akws)

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

cleanup(signum, frame)

the code to run when daemon is killed

restart()

stop and start the daemon

start()

start the daemonise running

status()

print the status of the daemon

stop()

stop the daemon and cleanup

action(**akws)[source]

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

cleanup(signum, frame)[source]

the code to run when daemon is killed

restart()[source]

stop and start the daemon

start()[source]

start the daemonise running

status()[source]

print the status of the daemon

stop()[source]

stop the daemon and cleanup