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 systemd.daemon
|
||||
from compLib.Lock import Lock
|
||||
|
||||
try:
|
||||
from compLib.LogstashLogging import Logging
|
||||
|
@ -29,6 +30,7 @@ RUN_IP_CHECK = False
|
|||
try:
|
||||
from compLib.Display import Display
|
||||
from compLib.Spi import Spi
|
||||
from compLib import __version__
|
||||
RUN_IP_CHECK = True
|
||||
except Exception as 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():
|
||||
while os.getenv("IP_OUTPUT", "true") == "true":
|
||||
try:
|
||||
ip = str(get_ip())
|
||||
print(f"writing {ip} to display")
|
||||
Display.write(2, f"FW. V{Spi.get_version()}")
|
||||
Display.write(3, "IP:")
|
||||
Display.write(4, ip)
|
||||
Display.write(1, datetime.datetime.now().strftime("%b %d %H:%M:%S"))
|
||||
time.sleep(5)
|
||||
if not Lock.is_locked():
|
||||
Lock.lock()
|
||||
ip = str(get_ip())
|
||||
print(f"writing {ip} to display")
|
||||
Display.write(2, f"LIB: V{__version__}")
|
||||
Display.write(3, f"FW: V{Spi.get_version()}")
|
||||
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:
|
||||
print(f"Exception in write ip thread: {e}")
|
||||
time.sleep(5)
|
||||
|
@ -82,6 +87,7 @@ if __name__ == '__main__':
|
|||
if RUN_IP_CHECK and IP_OUTPUT:
|
||||
print("starting ip output")
|
||||
Logging.get_logger().info("starting ip output")
|
||||
Lock.unlock()
|
||||
try:
|
||||
Spi.disable_health_check()
|
||||
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"
|
||||
|
||||
import sys
|
||||
import importlib
|
||||
import compLib.LogstashLogging
|
||||
import compLib.MetricsLogging
|
||||
|
||||
from compLib.Lock import Lock
|
||||
|
||||
apt_found = importlib.util.find_spec("apt") is not None
|
||||
spi_found = importlib.util.find_spec("spidev") is not None
|
||||
|
@ -25,6 +26,12 @@ if apt_found:
|
|||
else:
|
||||
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:
|
||||
from compLib.Spi import Spi
|
||||
from compLib.Reset import Reset
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
[Unit]
|
||||
Description=Monitoring 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"
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
|
|
|
@ -23,7 +23,7 @@ install_package() {
|
|||
#install_package "spidev"
|
||||
#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"
|
||||
pkg-config --modversion opencv4
|
||||
|
|
Reference in a new issue