<commit_before>#!/usr/bin/env python
"""\
Hello Redis Tasks
A quick example of how to use Redis as a task queue.
"""

import os
from redis import Redis, ConnectionError
from flask import Flask, render_template, abort, request, session, abort, redirect, url_for, flash, jsonify
from tasks import add


# Flask application
app = Flask(__name__)
app.config['DEBUG'] = __name__ == '__main__'
app.config.from_pyfile('config.py')

# Views
@app.route('/')
def index():
    return render_template('index.html')


@app.route('/add')
def add_start():
    """Grabs the args from the URL, starts the task, then redirects to show progress."""
    x = request.args.get('x', 2, type=int)
    y = request.args.get('y', 3, type=int)
    task = add.delay(x, y)
    return redirect('/progress?tid=' + task.id)


@app.route('/progress')
def add_progress():
    """Shows the progress of the current task or redirect home."""
    task_id = request.args.get('tid')
    return render_template('progress.html', task_id=task_id) if task_id else redirect('/')


@app.route('/poll')
def add_poll():
    """Called by the progress page using AJAX to check whether the task is complete."""
    task_id = request.args.get('tid')
    task = add.get_task(task_id)
    ready = task.return_value is not None if task else None
    return jsonify(ready=ready)


@app.route('/results')
def add_results():<commit_msg>Polling now correctly handles ConnectionError and returns a message instead of having the custom error handler return an entire error page during processing/progress errors.<commit_after>#!/usr/bin/env python
"""\
Hello Redis Tasks
A quick example of how to use Redis as a task queue.
"""

import os
from redis import Redis, ConnectionError
from flask import Flask, render_template, abort, request, session, abort, redirect, url_for, flash, jsonify
from tasks import add


# Flask application
app = Flask(__name__)
app.config['DEBUG'] = __name__ == '__main__'
app.config.from_pyfile('config.py')

# Views
@app.route('/')
def index():
    return render_template('index.html')


@app.route('/add')
def add_start():
    """Grabs the args from the URL, starts the task, then redirects to show progress."""
    x = request.args.get('x', 2, type=int)
    y = request.args.get('y', 3, type=int)
    task = add.delay(x, y)
    return redirect('/progress?tid=' + task.id)


@app.route('/progress')
def add_progress():
    """Shows the progress of the current task or redirect home."""
    task_id = request.args.get('tid')
    return render_template('progress.html', task_id=task_id) if task_id else redirect('/')


@app.route('/poll')
def add_poll():
    """Called by the progress page using AJAX to check whether the task is complete."""
    task_id = request.args.get('tid')
    try:
        task = add.get_task(task_id)
    except ConnectionError:
        # Return the error message as an HTTP 500 error
        return 'Coult not connect to the task queue. Check to make sure that <strong>redis-server</strong> is running and try again.', 500
    ready = task.return_value is not None if task else None
    return jsonify(ready=ready)


@app.route('/results')
def add_results():
{'fraction_non_alphanumeric': 0.1288888888888889, 'fraction_numerical': 0.0031746031746031746, 'mean_word_length': 4.106969205834684, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '877926', 'n_tokens_mistral': 983, 'n_tokens_neox': 955, 'n_words': 340}