diff --git a/Dockerfile b/Dockerfile index ccb23460..87defc3f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -55,7 +55,7 @@ COPY . . # for collectstatic. RUN cp webvirtcloud/settings.py.template webvirtcloud/settings.py \ && SECRET_KEY=build-only WEBVIRTCLOUD_LOG_FILE=/tmp/build.log venv/bin/python3 manage.py collectstatic --noinput \ - && rm webvirtcloud/settings.py /tmp/build.log + && rm -f webvirtcloud/settings.py /tmp/build.log # the log handler makes the file at its first line only COPY conf/docker/nginx.conf /etc/nginx/nginx.conf COPY conf/nginx/webvirtcloud.conf /etc/nginx/conf.d/ diff --git a/conf/requirements.txt b/conf/requirements.txt index 336a4cfe..1e8b884f 100644 --- a/conf/requirements.txt +++ b/conf/requirements.txt @@ -6,6 +6,8 @@ django-otp==1.7.5 idna>=3.15 requests>=2.33.0 urllib3>=2.8.0 +# the panel's processes rotate one log file without losing each other's lines +concurrent-log-handler==0.9.31 django-axes==8.3.1 djangorestframework==3.18.3 django-rest-knox==5.1.1 diff --git a/console/socketiod b/console/socketiod index 49f5b474..c4110cbc 100755 --- a/console/socketiod +++ b/console/socketiod @@ -97,6 +97,7 @@ def connect(sid, environ, auth): # pty.fork's master is inheritable: without this each worker would # hold the other sessions' consoles open os.dup2(LOG_FD, 3) + os.set_inheritable(3, True) # LOG_FD may be 3 itself: dup2 then keeps it close-on-exec os.closerange(4, os.sysconf("SC_OPEN_MAX")) os.environ["CONSOLECALLBACK_LOG_FD"] = "3" os.execv(sys.executable, [sys.executable, CONSOLECALLBACK, str(instance.pk), str(user.pk)]) diff --git a/webvirtcloud/settings.py.template b/webvirtcloud/settings.py.template index 661d971b..03ab1aee 100644 --- a/webvirtcloud/settings.py.template +++ b/webvirtcloud/settings.py.template @@ -289,7 +289,8 @@ LOGGING = { }, 'default': { 'level': 'INFO', - 'class': 'logging.handlers.RotatingFileHandler', + # every web worker writes the file: one rotates it at a time, under a lock + 'class': 'concurrent_log_handler.ConcurrentRotatingFileHandler', 'filename': WEBVIRTCLOUD_LOG_FILE, 'maxBytes': 10 * 1024 * 1024, 'backupCount': 5, diff --git a/webvirtcloud/tests/test_logging.py b/webvirtcloud/tests/test_logging.py index f9a9a51e..97162b4e 100644 --- a/webvirtcloud/tests/test_logging.py +++ b/webvirtcloud/tests/test_logging.py @@ -18,7 +18,7 @@ from webvirtcloud.middleware import RequestIdMiddleware class LoggingConfigTestCase(SimpleTestCase): def test_default_handler_rotates(self): handler = settings.LOGGING["handlers"]["default"] - self.assertEqual(handler["class"], "logging.handlers.RotatingFileHandler") + self.assertEqual(handler["class"], "concurrent_log_handler.ConcurrentRotatingFileHandler") self.assertGreater(handler["maxBytes"], 0) self.assertGreater(handler["backupCount"], 0) @@ -49,6 +49,31 @@ class LoggingConfigTestCase(SimpleTestCase): self.assertTrue(os.path.isdir(os.path.dirname(log)), log) + def test_a_process_writes_into_the_file_another_one_rotated(self): + # each web worker has its own handler on the file; with a plain + # RotatingFileHandler the others went on writing into the rotated one + import tempfile + + from django.utils.module_loading import import_string + + tmp = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, tmp) + log = os.path.join(tmp, "wvc.log") + handler_class = import_string(settings.LOGGING["handlers"]["default"]["class"]) + first, second = (handler_class(log, maxBytes=100, backupCount=5) for _ in range(2)) + self.addCleanup(first.close) + self.addCleanup(second.close) + + def write(handler, text): + handler.emit(logging.LogRecord("wvc", logging.INFO, __file__, 0, text, None, None)) + + write(second, "second, before") + for _ in range(3): + write(first, "first " + "x" * 60) # rotates + write(second, "second, after") + with open(log) as current: + self.assertIn("second, after", current.read()) + class RequestIdTestCase(TestCase): def test_a_line_of_a_request_names_the_request_user_and_address(self): stream = io.StringIO()