Browse Source

Load celery status asynchronously

Peter Justin 7 years ago
parent
commit
140cb608bd
2 changed files with 62 additions and 28 deletions
  1. 21 10
      flaskbb/management/views.py
  2. 41 18
      flaskbb/templates/management/overview.html

+ 21 - 10
flaskbb/management/views.py

@@ -868,6 +868,22 @@ class DeleteReport(MethodView):
         return redirect(url_for("management.reports"))
 
 
+class CeleryStatus(MethodView):
+    decorators = [allows.requires(IsAtleastModerator)]
+
+    def get(self):
+        celery_inspect = celery.control.inspect()
+        try:
+            celery_running = True if celery_inspect.ping() else False
+        except Exception:
+            # catching Exception is bad, and just catching ConnectionError
+            # from redis is also bad because you can run celery with other
+            # brokers as well.
+            celery_running = False
+
+        return jsonify(celery_running=celery_running, status=200)
+
+
 class ManagementOverview(MethodView):
     decorators = [allows.requires(IsAtleastModerator)]
 
@@ -887,15 +903,6 @@ class ManagementOverview(MethodView):
             order_by(Report.id.desc()).\
             count()
 
-        celery_inspect = celery.control.inspect()
-        try:
-            celery_running = True if celery_inspect.ping() else False
-        except Exception:
-            # catching Exception is bad, and just catching ConnectionError
-            # from redis is also bad because you can run celery with other
-            # brokers as well.
-            celery_running = False
-
         python_version = "{}.{}.{}".format(
             sys.version_info[0], sys.version_info[1], sys.version_info[2]
         )
@@ -914,7 +921,6 @@ class ManagementOverview(MethodView):
             # components
             "python_version": python_version,
             "celery_version": celery_version,
-            "celery_running": celery_running,
             "flask_version": flask_version,
             "flaskbb_version": flaskbb_version,
             # plugins
@@ -1177,6 +1183,11 @@ def flaskbb_load_blueprints(app):
     )
     register_view(
         management,
+        routes=['/celerystatus'],
+        view_func=CeleryStatus.as_view('celery_status')
+    )
+    register_view(
+        management,
         routes=['/'],
         view_func=ManagementOverview.as_view('overview')
     )

+ 41 - 18
flaskbb/templates/management/overview.html

@@ -19,22 +19,14 @@
             <div class="settings-content">
                 <div class="stats">
                     <div class="row stats-row">
-                        <div class="col-md-12 col-sm-12 col-xs-12">
-                            {# TODO: I have a feeling that this can be done so much nicer #}
-                            {% if not celery_running %}
-                            <div class="alert-message alert-message-danger">
-                                <h4>{% trans %}There is a problem.{% endtrans %}</h4>
-                                <p>{% trans %}Celery is <strong>not</strong> running.{% endtrans %}</p>
-                                <p>{% trans %}You can start celery with this command:{% endtrans %}</p>
-                                <pre>flaskbb --config {{ current_app.config["CONFIG_PATH"] }} celery worker</pre>
-                            </div>
-                            {% elif unread_reports > 0 %}
+                        <div class="overview-notifications col-md-12 col-sm-12 col-xs-12">
+                            {% if unread_reports > 0 %}
                             <div class="alert-message alert-message-warning">
                                 <h4>{% trans %}There is something that wants your attention.{% endtrans %}</h4>
                                 <p>{% trans url=url_for('management.unread_reports') %}You have <a href="{{ url }}">{{ unread_reports }} unread reports</a>.{% endtrans %}</p>
                             </div>
                             {% else %}
-                            <div class="alert-message alert-message-success">
+                            <div id="overview-no-notifications" class="alert-message alert-message-success">
                                 <h4>{% trans %}Everything seems alright.{% endtrans %}</h4>
                                 <p>{% trans %}No new notifications.{% endtrans %}</p>
                             </div>
@@ -118,13 +110,10 @@
                             </div>
                             -->
                             <div class="row stats-item">
-                                <div class="key pull-left">Celery</div><div class="value pull-right">
-                                {% if celery_running %}
-                                    <span class="text-success"><strong>running</strong></span>
-                                {% else %}
-                                <span class="text-danger"><strong>not running</strong></span>
-                                {% endif %}
-                                {{ celery_version }}
+                                <div class="key pull-left">Celery</div>
+                                <div class="value pull-right">
+                                    <span id="celery-status" class="text-warning">checking status</span>
+                                    {{ celery_version }}
                                 </div>
                             </div>
                             <div class="row stats-item">
@@ -167,3 +156,37 @@
     </div>
 </div>
 {% endblock %}
+
+{% block scripts %}
+<script>
+
+function celery_not_running_notification() {
+    content = "<div class='alert-message alert-message-danger'>" +
+        "<h4>{% trans %}There is a problem.{% endtrans %}</h4>" +
+        "<p>{% trans %}Celery is <strong>not</strong> running.{% endtrans %}</p>" +
+        "<p>{% trans %}You can start celery with this command:{% endtrans %}</p>" +
+        "<pre>flaskbb --config {{ current_app.config["CONFIG_PATH"] }} celery worker</pre>" +
+        "</div>";
+
+    // replace the no notifications notice with ours
+    if(document.getElementById("#overview-no-notifications") == null) {
+        $("#overview-no-notifications").replaceWith(content);
+    } else {
+        $(".overview-notifications").append(content);
+    }
+}
+
+$(document).ready(function () {
+    var $celerystatus = $('#celery-status');
+
+    $.getJSON('/admin/celerystatus', function(data) {
+        if(data.celery_running) {
+            $celerystatus.replaceWith("<span id='celery-status' class='text-success'><strong>running</strong></span>");
+        } else {
+            $celerystatus.replaceWith("<span id='celery-status' class='text-danger'><strong>not running</strong></span>");
+            celery_not_running_notification()
+        }
+    });
+});
+</script>
+{% endblock %}