瀏覽代碼

hard exit when memory is more than 300MB

Son NK 5 年之前
父節點
當前提交
e988573cb4
共有 1 個文件被更改,包括 13 次插入1 次删除
  1. 13 1
      app/pgp_utils.py

+ 13 - 1
app/pgp_utils.py

@@ -1,3 +1,4 @@
+import os
 from io import BytesIO
 
 import gnupg
@@ -24,11 +25,22 @@ def load_public_key(public_key: str) -> str:
         raise PGPException("Cannot load key") from e
 
 
+def hard_exit():
+    pid = os.getpid()
+    LOG.warning("kill pid %s", pid)
+    os.kill(pid, 9)
+
+
 def encrypt_file(data: BytesIO, fingerprint: str) -> str:
     LOG.d("encrypt for %s", fingerprint)
-    mem_usage = memory_usage(-1, interval=1, timeout=1)
+    mem_usage = memory_usage(-1, interval=1, timeout=1)[0]
     LOG.d("mem_usage %s", mem_usage)
 
+    # todo
+    if mem_usage > 300:
+        LOG.error("Force exit")
+        hard_exit()
+
     r = gpg.encrypt_file(data, fingerprint, always_trust=True)
     if not r.ok:
         LOG.error("Try encrypt again %s", fingerprint)