Repository: comic/grand-challenge.org

Issue:
Add stdout and stderr logging to the workstation session
This would help with debugging in the admin panel rather than going to external systems.


Discussion:

Resolving patch:
diff --git a/app/grandchallenge/workstations/admin.py b/app/grandchallenge/workstations/admin.py
--- a/app/grandchallenge/workstations/admin.py
+++ b/app/grandchallenge/workstations/admin.py
@@ -7,6 +7,13 @@
     WorkstationImage,
 )
 
+
+class SessionHistoryAdmin(SimpleHistoryAdmin):
+    list_display = ["pk", "created", "status", "creator"]
+    list_filter = ["status"]
+    readonly_fields = ["creator", "workstation_image", "status", "logs"]
+
+
 admin.site.register(Workstation)
 admin.site.register(WorkstationImage)
-admin.site.register(Session, SimpleHistoryAdmin)
+admin.site.register(Session, SessionHistoryAdmin)
diff --git a/app/grandchallenge/workstations/migrations/0006_session_logs.py b/app/grandchallenge/workstations/migrations/0006_session_logs.py
new file mode 100644
--- /dev/null
+++ b/app/grandchallenge/workstations/migrations/0006_session_logs.py
@@ -0,0 +1,18 @@
+# Generated by Django 2.2.8 on 2019-12-06 14:34
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ("workstations", "0005_auto_20191015_1005"),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name="session",
+            name="logs",
+            field=models.TextField(blank=True, editable=False),
+        ),
+    ]
diff --git a/app/grandchallenge/workstations/models.py b/app/grandchallenge/workstations/models.py
--- a/app/grandchallenge/workstations/models.py
+++ b/app/grandchallenge/workstations/models.py
@@ -1,3 +1,4 @@
+import logging
 from datetime import datetime, timedelta
 from urllib.parse import unquote, urljoin
 
@@ -9,6 +10,7 @@
 from django.db.models.signals import post_delete
 from django.dispatch import receiver
 from django_extensions.db.models import TitleSlugDescriptionModel
+from docker.errors import APIError
 from guardian.shortcuts import assign_perm
 from rest_framework.authtoken.models import Token
 from simple_history.models import HistoricalRecords
@@ -33,6 +35,8 @@
 The user is able to stop the container, otherwise it will be terminated after ``maxmium_duration`` is reached.
 """
 
+logger = logging.getLogger(__name__)
+
 
 class Workstation(UUIDModel, TitleSlugDescriptionModel):
     """Store the title and description of a workstation."""
@@ -266,7 +270,8 @@ class Session(UUIDModel):
     )
     maximum_duration = models.DurationField(default=timedelta(minutes=10))
     user_finished = models.BooleanField(default=False)
-    history = HistoricalRecords()
+    logs = models.TextField(editable=False, blank=True)
+    history = HistoricalRecords(excluded_fields=["logs"])
 
     class Meta(UUIDModel.Meta):
         ordering = ("created", "creator")
@@ -389,6 +394,11 @@ def start(self) -> None:
 
     def stop(self) -> None:
         """Stop the service for this session, cleaning up all of the containers."""
+        try:
+            self.logs = self.service.container.logs().decode()
+        except APIError:
+            logger.warning(f"Could not get the logs for {self}.")
+
         self.service.stop_and_cleanup()
         self.update_status(status=self.STOPPED)
 


Test patch: