Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V0.16e #362

Merged
merged 26 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
daemon fromconfig
  • Loading branch information
jblance committed Jun 2, 2023
commit cd393dec22c82b9fb1aedea7a8bda293ba3b900d
2 changes: 1 addition & 1 deletion powermon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def main():
log.info(mqtt_broker)

# build the daemon object (optional)
daemon = Daemon(config=config.get("daemon"))
daemon = Daemon.fromConfig(config=config.get("daemon"))
log.info(daemon)

# build api coordinator
Expand Down
112 changes: 63 additions & 49 deletions powermon/libs/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from enum import Enum, auto
from time import time

from strenum import LowercaseStrEnum

# Set-up logger
log = logging.getLogger("daemon")
Expand All @@ -14,62 +15,77 @@ class dummyNotification(Enum):
WATCHDOG = auto()


class DaemonType(LowercaseStrEnum):
DISABLED = auto()
SYSTEMD = auto()


class Daemon:
def __str__(self):
if self.disabled:
if not self.enabled:
return "Daemon DISABLED"
return f"Daemon name: {self.type}"

def __init__(self, config={}):
@classmethod
def fromConfig(cls, config={}):
log.debug(f"daemon config: {config}")

if config is None:
self.type = "disabled"
self.keepalive = 0
self.disabled = True
_type = DaemonType.DISABLED
keepalive = 0
enabled = False
log.debug("daemon not configured, disabling")
if config is not None:
self.disabled = False
self.type = config.get("type", None)
self.keepalive = config.get("keepalive", 60)
log.debug(f"got daemon type: {self.type}, keepalive: {self.keepalive}")

if self.type == "systemd":
try:
from cysystemd.daemon import Notification, notify
from cysystemd import journal

self._notify = notify
self._journal = journal.write
self._Notification = Notification
except ModuleNotFoundError as e:
print(
f"error: {e}, try 'pip install cysystemd' (which may need 'apt install build-essential libsystemd-dev'), see https://pypi.org/project/cysystemd/ for further info"
)
exit(1)
else:
self._notify = self._dummyNotify
self._journal = self._dummyNotify
self._Notification = dummyNotification
enabled = True
_type = config.get("type", None)
keepalive = config.get("keepalive", 60)
log.debug(f"got daemon: {_type=}, {keepalive=}")

return cls(_type=_type, keepalive=keepalive, enabled=enabled)

def __init__(self, _type, keepalive, enabled):

self.enabled = enabled
self.type = _type
self.keepalive = keepalive
log.debug(f"got daemon type: {self.type}, keepalive: {self.keepalive}")

match self.type:
case DaemonType.SYSTEMD:
try:
from cysystemd import journal
from cysystemd.daemon import Notification, notify

self._notify = notify
self._journal = journal.write
self._Notification = Notification
except ModuleNotFoundError as e:
print(
f"error: {e}, try 'pip install cysystemd' (which may need 'apt install build-essential libsystemd-dev'), see https://pypi.org/project/cysystemd/ for further info"
)
exit(1)
case _:
self._notify = self._dummyNotify
self._journal = self._dummyNotify
self._Notification = dummyNotification
self.notify(f"got daemon type: {self.type}, keepalive: {self.keepalive}")

def initialize(self, *args, **kwargs):
if self.disabled:
return
# Send READY=1
self._notify(self._Notification.READY)
self._lastNotify = time()
if self.enabled:
# Send READY=1
self._notify(self._Notification.READY)
self._lastNotify = time()

def watchdog(self, *args, **kwargs):
if self.disabled:
return
elapsed = time() - self._lastNotify
if (elapsed) > self.keepalive:
self._notify(self._Notification.WATCHDOG)
self._lastNotify = time()
self._journal(f"Daemon notify at {self._lastNotify}")
if self.enabled:
elapsed = time() - self._lastNotify
if (elapsed) > self.keepalive:
self._notify(self._Notification.WATCHDOG)
self._lastNotify = time()
self._journal(f"Daemon notify at {self._lastNotify}")

def notify(self, *args, **kwargs):
if self.disabled:
if not self.enabled:
return
# Send status
if args:
Expand All @@ -79,17 +95,15 @@ def notify(self, *args, **kwargs):
self._notify(self._Notification.STATUS, status)

def stop(self, *args, **kwargs):
if self.disabled:
return
# Send stopping
self._notify(self._Notification.STOPPING)
if self.enabled:
# Send stopping
self._notify(self._Notification.STOPPING)

def log(self, *args, **kwargs):
if self.disabled:
return
# Print log message
if args:
self._journal(args[0])
if self.enabled:
# Print log message
if args:
self._journal(args[0])

def _dummyNotify(self, *args, **kwargs):
# Print log message
Expand Down