From 92fe119cffb9afe5fafac016af9eac12e24f6f10 Mon Sep 17 00:00:00 2001
From: Fabio Manganiello <fabio@manganiello.tech>
Date: Sat, 27 Jul 2024 14:02:53 +0200
Subject: [PATCH] Removed `psutil` as a required pip dependency.

Moved to optional dependencies for `system` plugin.

It requires gcc, linux-headers and python-dev to be installed on the
system.

The `python-psutil` system package however will still be installed when
Platypush is installed through a package manager.
---
 platypush/backend/http/__init__.py     | 10 ++++++++--
 platypush/plugins/system/manifest.json | 17 +++++++++++------
 requirements.txt                       |  1 -
 3 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/platypush/backend/http/__init__.py b/platypush/backend/http/__init__.py
index d979337e..bdc21fbf 100644
--- a/platypush/backend/http/__init__.py
+++ b/platypush/backend/http/__init__.py
@@ -10,8 +10,6 @@ from multiprocessing import Process
 from time import time
 from typing import Mapping, Optional
 
-import psutil
-
 from tornado.httpserver import HTTPServer
 from tornado.netutil import bind_sockets, bind_unix_socket
 from tornado.process import cpu_count, fork_processes
@@ -421,6 +419,14 @@ class HttpBackend(Backend):
         workers when the server terminates:
         https://github.com/tornadoweb/tornado/issues/1912.
         """
+        try:
+            import psutil
+        except ImportError:
+            self.logger.warning(
+                'Could not import psutil, hanging worker processes might remain active'
+            )
+            return
+
         parent_pid = (
             self._server_proc.pid
             if self._server_proc and self._server_proc.pid
diff --git a/platypush/plugins/system/manifest.json b/platypush/plugins/system/manifest.json
index a2302fa9..b54bdcdf 100644
--- a/platypush/plugins/system/manifest.json
+++ b/platypush/plugins/system/manifest.json
@@ -3,22 +3,27 @@
     "events": {},
     "install": {
       "apk": [
-        "py3-py-cpuinfo"
+        "py3-py-cpuinfo",
+        "py3-psutil"
       ],
       "apt": [
-        "python3-cpuinfo"
+        "python3-cpuinfo",
+        "python3-psutil"
       ],
       "dnf": [
-        "python-cpuinfo"
+        "python-cpuinfo",
+        "python-psutil"
       ],
       "pacman": [
-        "python-py-cpuinfo"
+        "python-py-cpuinfo",
+        "python-psutil"
       ],
       "pip": [
-        "py-cpuinfo"
+        "py-cpuinfo",
+        "psutil"
       ]
     },
     "package": "platypush.plugins.system",
     "type": "plugin"
   }
-}
\ No newline at end of file
+}
diff --git a/requirements.txt b/requirements.txt
index 398ee37d..6e6803e8 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -8,7 +8,6 @@ docutils
 flask
 marshmallow
 marshmallow_dataclass
-psutil
 python-dateutil
 python-magic
 pyyaml