Skip to content

Commit

Permalink
support signals
Browse files Browse the repository at this point in the history
with this, we are closing down the application safely, and additionally
adding support for some nice interfacing

Signed-off-by: Mazunki Hoksaas <rolferen@gmail.com>
  • Loading branch information
mazunki committed Jul 29, 2024
1 parent 4b8d958 commit 60d1532
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ If a configuration file is not found, a default one will be generated. If you wa
- `frequency` (default `5`): how often (in seconds) we wait between updates
- `cards` (required): a list of card names (from `/sys/class/drm`) which we want to control.

Note! You can send a **SIGHUP** signal to the daemon to request a reload of the config without restarting the whole service.
Note! You can send a **SIGHUP** signal to the daemon to request a reload of the config without restarting the whole service. Additionally, if you're using a pidfile, you can send a signal to reload the config with `amdfan daemon --signal=reload`

# Install

Expand Down
14 changes: 13 additions & 1 deletion amdfan/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
""" entry point for amdfan """
# noqa: E501
import os
import signal
import sys
import time
from typing import Dict
Expand Down Expand Up @@ -122,7 +123,18 @@ def run_daemon(
logfile = None

if action:
pass # nyi
try:
with open(pidfile) as f:
pid = int(f.read())
if action == "stop":
os.kill(pid, signal.SIGTERM)
elif action == "reload":
os.kill(pid, signal.SIGHUP)

except FileNotFoundError:
LOGGER.warning("Could not find pidfile=%s", pidfile)
except ValueError:
LOGGER.warning("Invalid PID value in pidfile=%s", pidfile)
else:
FanController.start_manager(
notification_fd=notification_fd,
Expand Down
25 changes: 19 additions & 6 deletions amdfan/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import re
import signal
import sys
import threading
import time
from typing import Any, Callable, Dict, List, Optional, Self

Expand Down Expand Up @@ -238,11 +239,20 @@ class FanController: # pylint: disable=too-few-public-methods

def __init__(self, config_path, notification_fd=None) -> None:
self.config_path = config_path
self.reload_config()
self.load_config()
self._last_temp = 0
self._ready_fd = notification_fd
self._running = False
self._stop_event = threading.Event()

def reload_config(self, *_) -> None:
self.load_config()

def terminate(self, *_) -> None:
self._running = False
self._stop_event.set()

def load_config(self) -> None:
config = load_config(self.config_path)
self.apply(config)

Expand All @@ -256,15 +266,17 @@ def apply(self, config) -> None:
self._frequency = config.get("frequency", 5)

def main(self) -> None:
LOGGER.info("Starting amdfan")
if self._ready_fd is not None:
report_ready(self._ready_fd)

while True:
self._running = True
LOGGER.info("Controller is running")
while self._running:
for name, card in self._scanner.cards.items():
self.refresh_card(name, card)

time.sleep(self._frequency)
self._stop_event.wait(self._frequency)
LOGGER.info("Stopped controller")

def refresh_card(self, name, card):
apply = True
Expand Down Expand Up @@ -348,9 +360,10 @@ def start_manager(

controller = cls(config_path, notification_fd=notification_fd)
signal.signal(signal.SIGHUP, controller.reload_config)
signal.signal(signal.SIGTERM, controller.terminate)
signal.signal(signal.SIGINT, controller.terminate)
controller.main()

return controller
LOGGER.info("Goodbye")


def load_config(path) -> Callable:
Expand Down

0 comments on commit 60d1532

Please sign in to comment.