...
 
Commits (3)
import logging
import os
import re
import config.env
from django.core.validators import ValidationError
......@@ -8,7 +9,7 @@ from django.shortcuts import redirect
from django.urls import reverse
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View
from main.helpers import upload_data, get_base_url, lookup_job_file, get_request_user
from main.helpers import upload_data, get_base_url, lookup_job_file, get_request_user, slurp_job_log
from main.mixins import JobAuthMixin
from main.models import Job, Webapp, JobQueue
......@@ -32,11 +33,26 @@ class APIJobView(JobAuthMixin, View):
if lookup_job_file(job.id, f):
files[f] = get_link(job.id, job.data_dir, f, request)
# the pb here is that the whole log file is loaded
# it would be better to start reading from the end
# but I'm not sure it is possible.
logs = slurp_job_log(job)
pc_complete = "null"
pattern = re.compile("^\d*\.*\d+%", re.MULTILINE)
match_pc = None
for match_pc in pattern.finditer(logs):
pass
else: # after the last turn, to get the last match
if match_pc is not None:
pc_complete = match_pc.group()
response = {
job.id: files,
"status": job.get_state_display().lower(),
'complete': pc_complete,
}
return JsonResponse(response)
except Job.DoesNotExist as e:
log.error("Job not found %s", str(e))
return JsonResponse({'error': 'Job not found'}, status=404)
......
......@@ -212,4 +212,14 @@ def query_webapps_for_user(user):
return Webapp.objects.filter(Q(private=False) | Q(user_id=user.id))
def slurp_job_log(job):
""""""
log_file = os.path.join(job.data_dir, 'allgo.log')
try:
with open(log_file, 'r', errors="replace") as log_data:
logs = log_data.read()
except OSError as e:
logs = '(logs not available)'
log.error("Log file not available for job #%d (%s)", job.id, e)
return logs
......@@ -69,7 +69,16 @@ from .forms import (
)
# Local imports
import config
from .helpers import get_base_url, get_ssh_data, upload_data, notify_controller, lookup_job_file, get_request_user, query_webapps_for_user
from .helpers import (
get_base_url,
get_ssh_data,
upload_data,
notify_controller,
lookup_job_file,
get_request_user,
query_webapps_for_user,
slurp_job_log,
)
from .mixins import UserAccessMixin, ProviderAccessMixin, AllAccessMixin, JobAuthMixin
from .models import (
AllgoUser,
......@@ -1061,13 +1070,7 @@ class JobDetail(JobAuthMixin, DetailView):
if job.state == Job.DONE:
# job is done
# -> read the `allgo.log` file
log_file = os.path.join(job.data_dir, 'allgo.log')
try:
with open(log_file, 'r', errors="replace") as log_data:
logs = log_data.read()
except OSError as e:
logs = '(logs not available)'
log.error("Log file not available for job #%d (%s)", job.id, e)
logs = slurp_job_log(job)
else:
# job is pending
# -> logs will be streamed (ajax request)
......