Created
October 9, 2018 19:44
-
-
Save craigderington/6d02e6b372269503da5fe7b9fd84b76a to your computer and use it in GitHub Desktop.
Views module for Celery project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from datetime import datetime, timedelta | |
| from flask import Blueprint, jsonify, url_for | |
| from app import db | |
| from app.models import User | |
| home = Blueprint('home', __name__) | |
| @home.before_app_first_request | |
| def init_db(): | |
| db.create_all() | |
| db.session.commit() | |
| @home.route('/status/<task_id>/', methods=['GET', 'POST']) | |
| def taskstatus(task_id): | |
| task = User.AsyncResult(task_id) | |
| if task.state == 'PENDING': | |
| # job did not start yet | |
| response = { | |
| 'state': task.state, | |
| 'status': 'Pending...' | |
| } | |
| elif task.state != 'FAILURE': | |
| response = { | |
| 'state': task.state, | |
| 'current': task.info.get('current', 0), | |
| 'total': task.info.get('total', 1), | |
| 'status': task.info.get('status', '') | |
| } | |
| if 'result' in task.info: | |
| response['result'] = task.info['result'] | |
| else: | |
| # something went wrong in the background job | |
| response = { | |
| 'state': task.state, | |
| 'current': 1, | |
| 'total': 1, | |
| 'status': str(task.info), # this is the exception raised | |
| } | |
| return jsonify(response) | |
| @home.route('/users/') | |
| def get_users(): | |
| users = User.query.all() | |
| return jsonify([user.id for user in users]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment