daemonise (class)

class fundamentals.daemonise(log, name, **akws)[source][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:

```text 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:

```python 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)[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