Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
by-justin committed Aug 7, 2022
1 parent 2925691 commit 5fd31f3
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 8 deletions.
8 changes: 3 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ ENV TZ="Asia/Shanghai"
ENV CPU_NUM=1
ENV SPEED_FUNC="tanh((t-55)/10)*40 + 50"
ENV TIME_COND=1

# Update the base image
RUN apk -U upgrade
WORKDIR /

# Install impitool and curl
RUN apk add --no-cache ipmitool tzdata python3
RUN apk add --no-cache -U ipmitool tzdata python3

# Copy the entrypoint script into the container
COPY fanoverlord.py /
COPY better-fanoverlord/* /

# Load the entrypoint script to be run later
ENTRYPOINT ["/usr/bin/python3", "/fanoverlord.py"]
8 changes: 5 additions & 3 deletions fanoverlord.py → better-fanoverlord/fanoverlord.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from inspect import _void
from operator import is_
import os
import atexit
import logging
import subprocess
import time
from datetime import datetime
from math import *
from datetime import datetime
from register_exit_fun import register_exit_fun

# Env Variables
TZ = os.getenv('TZ', "Asia/Shanghai")
CPU_NUM = int(os.getenv('CPU_NUM', 1))
FAN_NUM = int(os.getenv('FAN_NUM', 1))
SPEED_FUNC = os.getenv('SPEED_FUNC', "tanh((t-55)/10)*40 + 50")
Expand Down Expand Up @@ -75,6 +76,7 @@ def calc_fan_speed(t : int):
def is_fanoverlord_enabled(t : datetime.time):
return bool(eval(TIME_COND))

@register_exit_fun
def on_exit():
logging.warning("Exiting... Give over control to system.")
set_fan_control(isTakeover=False)
Expand All @@ -83,7 +85,7 @@ def main():

# Init
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(levelname)s] %(message)s', datefmt='%d-%b-%y %H:%M:%S')
atexit.register(on_exit)
os.system("cp /usr/share/zoneinfo/" + TZ + " /etc/localtime")

# Main Loop

Expand Down
85 changes: 85 additions & 0 deletions better-fanoverlord/register_exit_fun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#https://www.pybloggers.com/2016/02/how-to-always-execute-exit-functions-in-python/

import atexit
import os
import signal
import sys


if os.name != 'posix':
raise ImportError("POSIX only")
_registered_exit_funs = set()
_executed_exit_funs = set()
_exit_signals = frozenset([signal.SIGTERM])


def register_exit_fun(fun, signals=_exit_signals):
"""Register a function which will be executed on "normal"
interpreter exit or in case one of the `signals` is received
by this process (differently from atexit.register()).
Also, it makes sure to execute any previously registered
via signal.signal().If any, it will be executed after `fun`.
Functions which were already registered or executed via this
function will be skipped.
Exit function will not be executed on SIGKILL, SIGSTOP or
os._exit(0).
"""
def fun_wrapper():
if fun not in _executed_exit_funs:
try:
fun()
finally:
_executed_exit_funs.add(fun)

def signal_wrapper(signum=None, frame=None):
if signum is not None:
pass
# smap = dict([(getattr(signal, x), x) for x in dir(signal)
# if x.startswith('SIG')])
# print("signal {} received by process with PID {}".format(
# smap.get(signum, signum), os.getpid()))
fun_wrapper()
# Only return the original signal this process was hit with
# in case fun returns with no errors, otherwise process will
# return with sig 1.
if signum is not None:
if signum == signal.SIGINT:
raise KeyboardInterrupt
# XXX - should we do the same for SIGTERM / SystemExit?
sys.exit(signum)

if not callable(fun):
raise TypeError("{!r} is not callable".format(fun))
set([fun]) # raise exc if obj is not hash-able

for sig in signals:
# Register function for this signal and pop() the previously
# registered one (if any). This can either be a callable,
# SIG_IGN (ignore signal) or SIG_DFL (perform default action
# for signal).
old_handler = signal.signal(sig, signal_wrapper)
if old_handler not in (signal.SIG_DFL, signal.SIG_IGN):
# ...just for extra safety.
if not callable(old_handler):
continue
# This is needed otherwise we'll get a KeyboardInterrupt
# strace on interpreter exit, even if the process exited
# with sig 0.
if (sig == signal.SIGINT and
old_handler is signal.default_int_handler):
continue
# There was a function which was already registered for this
# signal. Register it again so it will get executed (after our
# new fun).
if old_handler not in _registered_exit_funs:
atexit.register(old_handler)
_registered_exit_funs.add(old_handler)

# This further registration will be executed in case of clean
# interpreter exit (no signals received).
if fun not in _registered_exit_funs or not signals:
atexit.register(fun_wrapper)
_registered_exit_funs.add(fun)

0 comments on commit 5fd31f3

Please sign in to comment.