Skip to content

Commit 5d8a969

Browse files
authored
Merge pull request #6879 from readthedocs/dynamic-build-queue
Allocate docker limits based on server size.
2 parents ecba0f1 + 4c870f0 commit 5d8a969

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

readthedocs/doc_builder/constants.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import logging
66
import re
7+
import subprocess
78

89
from django.conf import settings
910

@@ -25,8 +26,44 @@
2526
)
2627
DOCKER_IMAGE_SETTINGS.update(old_config)
2728

28-
DOCKER_LIMITS = {'memory': '200m', 'time': 600}
29-
DOCKER_LIMITS.update(settings.DOCKER_LIMITS)
29+
DOCKER_LIMITS = {
30+
'memory': '200m',
31+
'time': 600,
32+
}
33+
34+
# Set docker limits dynamically based on system memory
35+
# This assumes 1-builder per server
36+
try:
37+
total_memory = int(subprocess.check_output("free -m | awk '/^Mem:/{print $2}'", shell=True))
38+
except ValueError:
39+
# On systems without a `free` command it will return a string to int and raise a ValueError
40+
log.exception('Failed to get memory size. Using defaults docker limits')
41+
total_memory = 0
42+
43+
if total_memory > 14000:
44+
DOCKER_LIMITS.update({
45+
'memory': '13g',
46+
'time': 2400,
47+
})
48+
elif total_memory > 8000:
49+
DOCKER_LIMITS.update({
50+
'memory': '7g',
51+
'time': 1800,
52+
})
53+
elif total_memory > 4000:
54+
DOCKER_LIMITS.update({
55+
'memory': '3g',
56+
'time': 900,
57+
})
58+
elif total_memory > 2000:
59+
DOCKER_LIMITS.update({
60+
'memory': '1g',
61+
'time': 600,
62+
})
63+
64+
if hasattr(settings, 'DOCKER_LIMITS'):
65+
DOCKER_LIMITS.update(settings.DOCKER_LIMITS)
66+
3067

3168
DOCKER_TIMEOUT_EXIT_CODE = 42
3269
DOCKER_OOM_EXIT_CODE = 137

0 commit comments

Comments
 (0)