]> git.mxchange.org Git - fba.git/blobdiff - fba.py
Continued:
[fba.git] / fba.py
diff --git a/fba.py b/fba.py
index 612bc03af1d58c14fb1910878ccacb1f86a08e3b..c8a088c587d7a2fde0ff2e7371641ad5b5224b0d 100644 (file)
--- a/fba.py
+++ b/fba.py
@@ -19,10 +19,13 @@ import hashlib
 import re
 import reqto
 import json
+import os
 import sqlite3
 import sys
+import tempfile
 import time
 import validators
+import zc.lockfile
 
 with open("config.json") as f:
     config = json.loads(f.read())
@@ -143,6 +146,10 @@ patterns = [
     re.compile("^[a-f0-9]{7}$"),
 ]
 
+# Lock file
+lockfile = tempfile.gettempdir() + '/.' + __name__ + '.lock'
+LOCK = None
+
 ##### Cache #####
 
 def is_cache_initialized(key: str) -> bool:
@@ -1544,3 +1551,23 @@ def tidyup(string: str) -> str:
     string = re.sub("(.+)\@", "", string)
 
     return string
+
+def lock_process():
+    global LOCK
+    try:
+        print(f"DEBUG: Acquiring lock: '{lockfile}'")
+        LOCK = zc.lockfile.LockFile(lockfile)
+        print("DEBUG: Lock obtained.")
+
+    except zc.lockfile.LockError:
+        print(f"ERROR: Cannot aquire lock: '{lockfile}'")
+        sys.exit(100)
+
+def shutdown():
+    print("DEBUG: Closing database connection ...")
+    connection.close()
+    print("DEBUG: Releasing lock ...")
+    LOCK.close()
+    print(f"DEBUG: Deleting lockfile='{lockfile}' ...")
+    os.remove(lockfile)
+    print("DEBUG: Shutdown completed.")