layerindex-web/layerindex/tasks.py
Paul Eggleton 74b1b9c895 Show update task output more smoothly
We were refreshing the page constantly in order to show output while
a task was running, which basically worked but is horrible. Instead,
write the task output to a file and then use AJAX calls to request
whatever output has been written to the file since the last call
and call this roughly every second. Put the output in a scrollable <pre>
element instead of making it the length of the page, and auto-scroll
to the end (unless the user grabs the scrollbar and pulls it upwards -
it may not be immediately obvious that you can do this if there is a lot
of output since you have to pull it up when the scrolling animation is
not running, but it is possible).

An alternative would be to have used some kind of long-lived HTTP
session or a websocket, but those come with their own set of problems
so I elected to use this much simpler method.

Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com>
2018-09-20 16:04:49 +12:00

60 lines
1.8 KiB
Python

# Celery task definitions for the layer index app
#
# Copyright (C) 2018 Intel Corporation
# Author: Paul Eggleton <paul.eggleton@linux.intel.com>
#
# Licensed under the MIT license, see COPYING.MIT for details
from celery import Celery
from django.core.mail import EmailMessage
from . import utils
import os
import time
import subprocess
from datetime import datetime
try:
import settings
except ImportError:
# not in a full django env, so settings is inaccessible.
# setup django to access settings.
utils.setup_django()
import settings
tasks = Celery('layerindex',
broker=settings.RABBIT_BROKER,
backend=settings.RABBIT_BACKEND)
@tasks.task
def send_email(subject, text_content, from_email=settings.DEFAULT_FROM_EMAIL, to_emails=[]):
# We seem to need to run this within the task
utils.setup_django()
msg = EmailMessage(subject, text_content, from_email, to_emails)
msg.send()
@tasks.task(bind=True)
def run_update_command(self, branch_name, update_command):
utils.setup_django()
from layerindex.models import Update
updateobj = Update.objects.get(task_id=self.request.id)
updateobj.started = datetime.now()
updateobj.save()
output = ''
update_command = update_command.replace('%update%', str(updateobj.id))
update_command = update_command.replace('%branch%', branch_name)
try:
os.makedirs(settings.TASK_LOG_DIR)
except FileExistsError:
pass
logfile = os.path.join(settings.TASK_LOG_DIR, 'task_%s.log' % str(self.request.id))
try:
output = utils.runcmd(update_command, os.path.dirname(os.path.dirname(__file__)), outfile=logfile)
except subprocess.CalledProcessError as e:
output = e.output
except Exception as e:
output = str(e)
finally:
updateobj.log = output
updateobj.finished = datetime.now()
updateobj.save()