Celery – bringing to the front

We display the results of the work of periodic Celery tasks to the front.

For this purpose, you can set flower. But it has a number of disadvantages:

  • Access on the server is configured floridly: either additionally write configs in Nginx or connect via google api… In my opinion, all this is too far from the code, then I can’t remember where it lies.
  • There is no monitoring of tasks recorded in the database. Monitoring only in live mode.

It is easy to get around all the shortcomings by quickly building a page in the admin panel to view the results of the work of periodic tasks.

All code:

from flask import Blueprint, json
from flask_login import login_required
from app import db

celery_bp = Blueprint(‘celery_bp’, __name__)

def serialize_query(msg):
return {
“task_id”: msg.task_id,
“status”: msg.status,
“result”: msg.resulttobytes()decode(‘utf-8’, errors=‘ignore’)replace(
x05C x00 x00 x00 x00 x00 x00 x00? ‘, ),
“date_done”: msg.date_donestrftime(‘% H:% M | % d% B% Y ‘),
}

@login_required
@celery_bp.route(‘/ get-logs /’, methods=[‘GET’])
def get_logs():
query = [serialize_query(x) for x in db.engine.execute(‘select * from celery_taskmeta order by date_done’)]
return json.dumps(query)

This is getting data from the backend. Flask, Blueprint, SQLAlchemy are used here. It remains only at the front to send a request to the specified url, parse the json and display it on the page.

Errors

TypeError: Object of type memoryview is not JSON serializable

Of the features of Celery. It saves the result to the database in the format memoryview… We convert this format into bytes with the command:

Then we decode ignoring errors:

decode(‘utf-8’, errors=‘ignore’)

For some reason, celery keeps memoryview with wrong characters, which is what the interpreter actually swears at. Then we delete these symbols with the command:

replace( x05C x00 x00 x00 x00 x00 x00 x00? ‘, )

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *