From 1acd71c9166f8060356bdc522d7cd2061a281b51 Mon Sep 17 00:00:00 2001
From: Stefano Alberto Russo <stefano.russo@gmail.com>
Date: Fri, 8 Apr 2022 09:05:17 +0200
Subject: [PATCH] Added check if the task interface is up, and marked the task
 status accordingly - including graying out the connect button if not up yet.

---
 .../core_app/templates/components/task.html   |  6 ++--
 .../webapp/code/rosetta/core_app/views.py     | 32 +++++++++++++++++--
 2 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/services/webapp/code/rosetta/core_app/templates/components/task.html b/services/webapp/code/rosetta/core_app/templates/components/task.html
index a74d134..fe72829 100644
--- a/services/webapp/code/rosetta/core_app/templates/components/task.html
+++ b/services/webapp/code/rosetta/core_app/templates/components/task.html
@@ -44,10 +44,10 @@
             <!-- <a href="/computing/?uuid={{ task.computing.uuid }}" no_style="color:{{task.computing.color}}"><i class="fa fa-external-link" ></i></a><br/> -->           
             
             <div style="margin-top:2px">
-            {% if task.status == "running" %}
+            {% if task.verified_status == "running" %}
              <b>Status:</b> <font color="green">running</font>
             {% else %}
-             <b>Status:</b> {{ task.status }}
+             <b>Status:</b> {{ task.verified_status }}
             {% endif %}
             </div>
             </div>
@@ -63,7 +63,7 @@
             
             <!-- Connect -->
             {% if task.interface_port %}
-            {% if task.status == "running" %}
+            {% if task.verified_status == "running" %}
             <a href="/task_connect/?uuid={{task.uuid}}" class="btn btn-connect" target="_blank">Connect</a>
             {% else %}
             <a href="" class="btn btn-disabled">Connect</a>  
diff --git a/services/webapp/code/rosetta/core_app/views.py b/services/webapp/code/rosetta/core_app/views.py
index 68164b7..06d53c2 100644
--- a/services/webapp/code/rosetta/core_app/views.py
+++ b/services/webapp/code/rosetta/core_app/views.py
@@ -2,6 +2,7 @@ import os
 import uuid
 import json
 import requests
+import socket
 import subprocess
 import base64
 from django.conf import settings
@@ -341,6 +342,30 @@ def account(request):
 #  Tasks view
 #=========================
 
+def set_verified_status(task):
+    # Chech status with ping
+    if task.status == 'running':
+        logger.debug('Task is running, check if startup completed')
+
+        logger.debug('Trying to establish connection on: "{}:{}"'.format(task.interface_ip,task.interface_port))
+        s = socket.socket()
+        try:
+            s.settimeout(1)
+            s.connect((task.interface_ip, task.interface_port))
+            # Not necessary, we just check that the container interfcae is up
+            #if not s.recv(10):
+            #    logger.debug('No data read from socket')
+            #    raise Exception('Could not read any data from socket')
+        except Exception as e:
+            logger.debug('Could not connect to socket')
+            task.verified_status = 'starting up...'
+        else:
+            task.verified_status = 'running'
+        finally:
+            s.close()
+    else:
+        task.verified_status = task.status
+
 @private_view
 def tasks(request):
 
@@ -367,8 +392,10 @@ def tasks(request):
                 task = Task.objects.get(user=request.user, uuid=uuid)
             except Task.DoesNotExist:
                 raise ErrorMessage('Task does not exists or no access rights')
+            
+            set_verified_status(task)
             data['task'] = task
-    
+            
             #  Task actions
             if action=='delete':
                 if task.status not in [TaskStatuses.stopped, TaskStatuses.exited]:
@@ -447,7 +474,8 @@ def tasks(request):
         # Update task statuses
         for task in tasks:
             task.update_status()
-    
+            set_verified_status(task)
+        
         # Set task and tasks variables
         data['task']  = None   
         data['tasks'] = tasks
-- 
GitLab