Allow for platforms not supporting resource module

This commit is contained in:
Ilan Steemers
2020-07-05 17:45:07 +02:00
parent 83b8b7c0d7
commit b16878f36f
3 changed files with 14 additions and 5 deletions
+5
View File
@@ -10,3 +10,8 @@ try:
from croniter import croniter from croniter import croniter
except ImportError: except ImportError:
croniter = None croniter = None
try:
import resource
except ModuleNotFoundError:
resource = None
+6 -3
View File
@@ -1,7 +1,6 @@
# Standard # Standard
import ast import ast
import importlib import importlib
import resource
import signal import signal
import socket import socket
import traceback import traceback
@@ -28,7 +27,7 @@ from django_q.queues import Queue
from django_q.signals import pre_execute from django_q.signals import pre_execute
from django_q.signing import SignedPackage, BadSignature from django_q.signing import SignedPackage, BadSignature
from django_q.status import Stat, Status from django_q.status import Stat, Status
from django_q import croniter from django_q import croniter, resource
class Cluster: class Cluster:
@@ -435,7 +434,11 @@ def worker(
result_queue.put(task) result_queue.put(task)
timer.value = -1 # Idle timer.value = -1 # Idle
# Recycle # 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 timer.value = -2 # Recycled
break break
logger.info(_(f"{name} stopped doing work")) logger.info(_(f"{name} stopped doing work"))
+3 -2
View File
@@ -104,7 +104,8 @@ class Conf:
# Number of tasks each worker can handle before it gets recycled. Useful for releasing memory # Number of tasks each worker can handle before it gets recycled. Useful for releasing memory
RECYCLE = conf.get("recycle", 500) 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) MAX_RSS = conf.get("max_rss", None)
# Number of seconds to wait for a worker to finish. # 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 # and instantiate an ErrorReporter using the provided config
for name, conf in error_conf.items(): for name, conf in error_conf.items():
for entry in pkg_resources.iter_entry_points( for entry in pkg_resources.iter_entry_points(
"djangoq.errorreporters", name "djangoq.errorreporters", name
): ):
Reporter = entry.load() Reporter = entry.load()
reporters.append(Reporter(**conf)) reporters.append(Reporter(**conf))