Commit 96741268 authored by BERJON Matthieu's avatar BERJON Matthieu
Browse files

Reading of Redis logs for a specific job

I added a helper function to extract the job logs for a specific job in the Redis database.
The function is a copy of another part of the code not merged yet in the master branch.

I updated the view accordingly, added a proper docstring and moved the class in the module to keep the code ordered.
I updated as well the template to display the data.
parent 63e8f656
import base64
import hashlib
import os
import redis
from django.conf import settings
import config
DEFAULT_ENTROPY = 32 # number of bytes to return by default
......@@ -54,3 +57,15 @@ def upload_data(file_obj, job_id):
with open(endpoint, 'wb+') as destination:
for chunk in file_data.chunks():
destination.write(chunk)
def read_runner_logs(jobid):
redis_host = config.env.ALLGO_REDIS_HOST
r = redis.StrictRedis(host=redis_host, port=6379, db=0)
linenumber = 0
k = "job:log:%s:%s" % (jobid, linenumber)
log.debug("Runner read logs k %s - %s", k, r.exists(k))
while r.exists(k):
yield r.get(k)
linenumber += 1
k = "job:log:%s:%s" % (jobid, linenumber)
......@@ -7,7 +7,6 @@ import os
import zipfile
import config.env
import redis
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.forms import PasswordChangeForm
......@@ -34,7 +33,8 @@ from .forms import (
WebappForm,
WebappSandboxForm,
)
from .helpers import get_ssh_data, upload_data
from .helpers import get_ssh_data, read_runner_logs, upload_data
log = logging.getLogger('allgo')
......@@ -217,33 +217,6 @@ class WebappSandboxCreation(SuccessMessageMixin, LoginRequiredMixin, CreateView)
return context
class JobDetailView(LoginRequiredMixin, DetailView):
"""
Display the data related to a particular job of an identified user
"""
model = Job
template_name = 'job_detail.html'
context_object_name = 'job'
def get_context_data(self, **kwargs):
queryset = Job.objects.get(id=self.object.pk)
dirname = os.path.join(settings.DATASTORE, str(queryset.id))
if os.path.exists(dirname):
# put all files in a list
kwargs['files'] = [os.path.basename(x) for x in glob.glob(os.path.join(dirname, '*'))]
else:
kwargs['files'] = []
return super(JobDetailView, self).get_context_data(**kwargs)
class JobDeleteView(SuccessMessageMixin, LoginRequiredMixin, DeleteView):
model = Job
success_message = 'Job successfully deleted.'
success_url = reverse_lazy('main:job_list')
template_name = 'job_delete.html'
class UserWebappListView(LoginRequiredMixin, ListView):
"""
Display the list of jobs for a given identified user
......@@ -454,6 +427,37 @@ class JobList(LoginRequiredMixin, ListView):
return queryset
class JobDetail(LoginRequiredMixin, DetailView):
"""Get a job detail for a specific user
Attributes:
model: model used in the view.
context_object_name: the name used in the template to display each
variable.
template_name: name of the template loaded with this view.
"""
model = Job
template_name = 'job_detail.html'
context_object_name = 'job'
def get_context_data(self, **kwargs):
"""Recover the logs and files related to this job"""
logs = read_runner_logs(self.object.pk)
kwargs['logs'] = log
queryset = Job.objects.get(id=self.object.pk)
webapp = Webapp.objects.get(docker_name=self.object.webapp)
webapp_version = WebappVersion.objects.get(webapp=webapp)
dirname = os.path.join(settings.DATASTORE, str(queryset.id))
kwargs['webapp_version'] = webapp_version
if os.path.exists(dirname):
# put all files in a list
kwargs['files'] = [os.path.basename(x) for x in glob.glob(os.path.join(dirname, '*'))]
else:
kwargs['files'] = []
return super(JobDetail, self).get_context_data(**kwargs)
class JobCreate(SuccessMessageMixin, LoginRequiredMixin, CreateView):
model = Job
form_class = JobForm
......
......@@ -108,6 +108,13 @@
<td></td>
</tr>
</table>
</div>
<div class="col-sm">
<h5 class="card-title">Logs</h5>
<pre><code class="lang-json">{{ logs }}</code></pre>
</div>
</div>
</div>
{% endif %}
</div>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment