Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ Changelog

All notable changes to this project will be documented in this file.

`0.27.1`_ -- 2020-04-14
-----------------------
Added
^^^^^
* MaxMemory: pass default max_memory via env. variable (remoulade_max_memory)

`0.27.0`_ -- 2020-04-06
-----------------------
Added
Expand Down Expand Up @@ -503,6 +509,7 @@ Fixed

* pipe_ignore was not recovered from right message

.. _0.27.1: https://github.com/wiremind/remoulade/releases/tag/v0.27.1
.. _0.27.0: https://github.com/wiremind/remoulade/releases/tag/v0.27.0
.. _0.26.7: https://github.com/wiremind/remoulade/releases/tag/v0.26.7
.. _0.26.6: https://github.com/wiremind/remoulade/releases/tag/v0.26.6
Expand Down
15 changes: 12 additions & 3 deletions remoulade/middleware/max_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import resource
from typing import Optional

from ..logging import get_logger
from .middleware import Middleware

#: The default max_memory (in kilobytes)
DEFAULT_MAX_MEMORY = os.getenv("remoulade_max_memory", None)


class MaxMemory(Middleware):
"""Middleware that stop a worker if its amount of resident memory exceed max_memory (in kilobytes)
Expand All @@ -30,11 +34,16 @@ class MaxMemory(Middleware):
max_memory(int): The maximum amount of resident memory (in kilobytes)
"""

def __init__(self, *, max_memory: int):
def __init__(self, *, max_memory: Optional[int] = DEFAULT_MAX_MEMORY):
self.logger = get_logger(__name__, type(self))
self.max_memory = max_memory
self.max_memory: Optional[int] = None
if max_memory is not None:
self.max_memory = int(max_memory)

def after_worker_thread_process_message(self, broker, thread):
if self.max_memory is None:
return

used_memory = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
if used_memory <= 0:
self.logger.error("Worker unable to determine memory usage")
Expand Down