~comcloudway/builds.sr.ht

c31eb624f5dc7acc07e5f244e8295881bce144bd — Drew DeVault 5 years ago 97e3bad
Truncage large log files
1 files changed, 30 insertions(+), 9 deletions(-)

M buildsrht/blueprints/jobs.py
M buildsrht/blueprints/jobs.py => buildsrht/blueprints/jobs.py +30 -9
@@ 215,8 215,25 @@ def tag_svg(username, path):
        .filter(Job.tags.ilike(path + "%"))
    return svg_page(jobs)

def logify(text, task):
    text = Markup('<pre>') + escape(text) + Markup('</pre>')
def logify(text, task, log_url):
    #log_max = 16384
    log_max = 512
    if len(text) > log_max:
        text = text[-log_max:]
        try:
            text = text[text.index('\n')+1:]
        except ValueError:
            pass
        text = (Markup('<pre>')
                + Markup('<span class="text-muted">'
                    'This is a big file! Only the last 16KiB is shown. '
                    f'<a target="_blank" href="{escape(log_url)}">'
                        'Click here to download the full log</a>.'
                    '</span>\n\n')
                + escape(text)
                + Markup('</pre>'))
    else:
        text = Markup('<pre>') + escape(text) + Markup('</pre>')
    nlines = text.encode().count(b'\n')
    linenos = Markup('<pre>')
    for no in range(1, nlines + 1):


@@ 234,30 251,34 @@ def job_by_id(username, job_id):
        abort(404)
    logs = list()
    try:
        r = requests.get("http://{}/logs/{}/log".format(job.runner, job.id))
        log_url = "http://{}/logs/{}/log".format(job.runner, job.id)
        r = requests.get(log_url)
        if r.status_code == 200:
            logs.append({
                "name": None,
                "log": logify(r.text, "setup")
                "log": logify(r.text, "setup", log_url)
            })
    except:
        pass
    for task in sorted(job.tasks, key=lambda t: t.id):
        if task.status == TaskStatus.pending:
            continue
        log_url = "http://{}/logs/{}/{}/log".format(
                job.runner, job.id, task.name)
        try:
            r = requests.get("http://{}/logs/{}/{}/log".format(job.runner,
                job.id, task.name))
            r = requests.get(log_url)
        except:
            logs.append({
                "name": "error",
                "log": "Error fetching logs for this job"
                "name": task.name,
                "log": Markup('<td></td><td><pre><strong class="text-danger">'
                    f'Error fetching logs for task "{escape(task.name)}"</strong>'
                    '</pre></td>')
            })
            break
        if r.status_code == 200:
            logs.append({
                "name": task.name,
                "log": logify(r.text, "task-" + task.name)
                "log": logify(r.text, "task-" + task.name, log_url)
            })
    return render_template("job.html",
            job=job,