Add Locking
This commit is contained in:
parent
34e68e2a9e
commit
01cb854f50
5 changed files with 48 additions and 10 deletions
|
@ -5,6 +5,7 @@ import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import systemd.daemon
|
import systemd.daemon
|
||||||
|
from compLib.Lock import Lock
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from compLib.LogstashLogging import Logging
|
from compLib.LogstashLogging import Logging
|
||||||
|
@ -29,6 +30,7 @@ RUN_IP_CHECK = False
|
||||||
try:
|
try:
|
||||||
from compLib.Display import Display
|
from compLib.Display import Display
|
||||||
from compLib.Spi import Spi
|
from compLib.Spi import Spi
|
||||||
|
from compLib import __version__
|
||||||
RUN_IP_CHECK = True
|
RUN_IP_CHECK = True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Could not import display or spi for ip output {str(e)}")
|
print(f"Could not import display or spi for ip output {str(e)}")
|
||||||
|
@ -58,13 +60,16 @@ def get_ip():
|
||||||
def write_ip_to_screen():
|
def write_ip_to_screen():
|
||||||
while os.getenv("IP_OUTPUT", "true") == "true":
|
while os.getenv("IP_OUTPUT", "true") == "true":
|
||||||
try:
|
try:
|
||||||
ip = str(get_ip())
|
if not Lock.is_locked():
|
||||||
print(f"writing {ip} to display")
|
Lock.lock()
|
||||||
Display.write(2, f"FW. V{Spi.get_version()}")
|
ip = str(get_ip())
|
||||||
Display.write(3, "IP:")
|
print(f"writing {ip} to display")
|
||||||
Display.write(4, ip)
|
Display.write(2, f"LIB: V{__version__}")
|
||||||
Display.write(1, datetime.datetime.now().strftime("%b %d %H:%M:%S"))
|
Display.write(3, f"FW: V{Spi.get_version()}")
|
||||||
time.sleep(5)
|
Display.write(4, f"IP: {ip}")
|
||||||
|
Display.write(1, datetime.datetime.now().strftime("%b %d %H:%M:%S"))
|
||||||
|
Lock.unlock()
|
||||||
|
time.sleep(10)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Exception in write ip thread: {e}")
|
print(f"Exception in write ip thread: {e}")
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
|
@ -82,6 +87,7 @@ if __name__ == '__main__':
|
||||||
if RUN_IP_CHECK and IP_OUTPUT:
|
if RUN_IP_CHECK and IP_OUTPUT:
|
||||||
print("starting ip output")
|
print("starting ip output")
|
||||||
Logging.get_logger().info("starting ip output")
|
Logging.get_logger().info("starting ip output")
|
||||||
|
Lock.unlock()
|
||||||
try:
|
try:
|
||||||
Spi.disable_health_check()
|
Spi.disable_health_check()
|
||||||
ip_output = threading.Thread(target=write_ip_to_screen)
|
ip_output = threading.Thread(target=write_ip_to_screen)
|
25
compLib/Lock.py
Normal file
25
compLib/Lock.py
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
from filelock import FileLock, Timeout
|
||||||
|
import atexit
|
||||||
|
|
||||||
|
FILELOCK_PATH = "/root/complib.lock"
|
||||||
|
|
||||||
|
global_lock = FileLock(FILELOCK_PATH, timeout=1)
|
||||||
|
class Lock(object):
|
||||||
|
@staticmethod
|
||||||
|
def lock():
|
||||||
|
global_lock.acquire()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def unlock():
|
||||||
|
global_lock.release()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def is_locked():
|
||||||
|
try:
|
||||||
|
global_lock.acquire()
|
||||||
|
global_lock.release()
|
||||||
|
return False
|
||||||
|
except Timeout:
|
||||||
|
return True
|
||||||
|
|
||||||
|
atexit.register(Lock.unlock)
|
|
@ -1,9 +1,10 @@
|
||||||
__version__ = "0.3.9-0"
|
__version__ = "0.3.9-0"
|
||||||
|
|
||||||
|
import sys
|
||||||
import importlib
|
import importlib
|
||||||
import compLib.LogstashLogging
|
import compLib.LogstashLogging
|
||||||
import compLib.MetricsLogging
|
import compLib.MetricsLogging
|
||||||
|
from compLib.Lock import Lock
|
||||||
|
|
||||||
apt_found = importlib.util.find_spec("apt") is not None
|
apt_found = importlib.util.find_spec("apt") is not None
|
||||||
spi_found = importlib.util.find_spec("spidev") is not None
|
spi_found = importlib.util.find_spec("spidev") is not None
|
||||||
|
@ -25,6 +26,12 @@ if apt_found:
|
||||||
else:
|
else:
|
||||||
print("apt is not installed! This is for local development only!")
|
print("apt is not installed! This is for local development only!")
|
||||||
|
|
||||||
|
if Lock.is_locked():
|
||||||
|
print("Another program using compLib is still running! Delete /root/complib.lock if this is an error. Exiting...")
|
||||||
|
sys.exit()
|
||||||
|
else:
|
||||||
|
Lock.lock()
|
||||||
|
|
||||||
if spi_found:
|
if spi_found:
|
||||||
from compLib.Spi import Spi
|
from compLib.Spi import Spi
|
||||||
from compLib.Reset import Reset
|
from compLib.Reset import Reset
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=Monitoring service
|
Description=Monitoring service
|
||||||
[Service]
|
[Service]
|
||||||
ExecStart=/usr/bin/python3 /usr/local/lib/python3.7/dist-packages/compLib/VisionDaemon.py
|
ExecStart=/usr/bin/python3 /usr/local/lib/python3.7/dist-packages/compLib/IPService.py
|
||||||
Environment="debug=False"
|
Environment="debug=False"
|
||||||
Restart=always
|
Restart=always
|
||||||
RestartSec=5
|
RestartSec=5
|
||||||
|
|
|
@ -23,7 +23,7 @@ install_package() {
|
||||||
#install_package "spidev"
|
#install_package "spidev"
|
||||||
#install_package "influxdb_client"
|
#install_package "influxdb_client"
|
||||||
|
|
||||||
pip3 install smbus requests flask python-logstash-async RPi.GPIO spidev influxdb_client
|
pip3 install smbus requests flask python-logstash-async RPi.GPIO spidev influxdb_client filelock
|
||||||
|
|
||||||
echo "Setting up opencv4"
|
echo "Setting up opencv4"
|
||||||
pkg-config --modversion opencv4
|
pkg-config --modversion opencv4
|
||||||
|
|
Reference in a new issue