From b16878f36f72a6b967a91065c6224edf305e08e0 Mon Sep 17 00:00:00 2001 From: Ilan Steemers Date: Sun, 5 Jul 2020 17:45:07 +0200 Subject: [PATCH] Allow for platforms not supporting resource module --- django_q/__init__.py | 5 +++++ django_q/cluster.py | 9 ++++++--- django_q/conf.py | 5 +++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/django_q/__init__.py b/django_q/__init__.py index 2800bb4..f96c270 100644 --- a/django_q/__init__.py +++ b/django_q/__init__.py @@ -10,3 +10,8 @@ try: from croniter import croniter except ImportError: croniter = None + +try: + import resource +except ModuleNotFoundError: + resource = None diff --git a/django_q/cluster.py b/django_q/cluster.py index f887cb7..96da4c5 100644 --- a/django_q/cluster.py +++ b/django_q/cluster.py @@ -1,7 +1,6 @@ # Standard import ast import importlib -import resource import signal import socket import traceback @@ -28,7 +27,7 @@ from django_q.queues import Queue from django_q.signals import pre_execute from django_q.signing import SignedPackage, BadSignature from django_q.status import Stat, Status -from django_q import croniter +from django_q import croniter, resource class Cluster: @@ -435,7 +434,11 @@ def worker( result_queue.put(task) timer.value = -1 # Idle # Recycle - if task_count == Conf.RECYCLE or (Conf.MAX_RSS and resource.getrusage(resource.RUSAGE_SELF).ru_maxrss >= Conf.MAX_RSS): + if task_count == Conf.RECYCLE or ( + resource + and Conf.MAX_RSS + and resource.getrusage(resource.RUSAGE_SELF).ru_maxrss >= Conf.MAX_RSS + ): timer.value = -2 # Recycled break logger.info(_(f"{name} stopped doing work")) diff --git a/django_q/conf.py b/django_q/conf.py index 93b0d27..5b9b5b3 100644 --- a/django_q/conf.py +++ b/django_q/conf.py @@ -104,7 +104,8 @@ class Conf: # Number of tasks each worker can handle before it gets recycled. Useful for releasing memory RECYCLE = conf.get("recycle", 500) - # The maximum resident set size in kilobytes before a worker will recycle. Useful for limiting memory usage. + # The maximum resident set size in kilobytes before a worker will recycle. Useful for limiting memory usage + # Not available on all platforms MAX_RSS = conf.get("max_rss", None) # Number of seconds to wait for a worker to finish. @@ -214,7 +215,7 @@ if Conf.ERROR_REPORTER: # and instantiate an ErrorReporter using the provided config for name, conf in error_conf.items(): for entry in pkg_resources.iter_entry_points( - "djangoq.errorreporters", name + "djangoq.errorreporters", name ): Reporter = entry.load() reporters.append(Reporter(**conf))