Example #1
0
    def show_signals_config(self):
        import_signals_and_functions()

        def display_callable(conn):
            fn = conn.function
            if getattr(fn, "__module__", None) and getattr(
                    fn, "__name__", None):
                path = "%s.%s" % (fn.__module__, fn.__name__)
            elif getattr(fn, "__name__", None):
                path = fn.__name__
            else:
                path = str(fn)
            return path

        self.stdout.write(self.style.ERROR("Signals"))
        data = list(decorators.REGISTERED_SIGNALS.items())
        for name, connections in sorted(data, key=lambda x: x[0]):

            self.stdout.write(self.style.WARNING('    "%s"' % name))
            for connection in connections:
                self.stdout.write(
                    self.style.NOTICE("      -> %s" %
                                      display_callable(connection)))
        self.stdout.write(self.style.ERROR("Functions"))
        data = list(decorators.REGISTERED_FUNCTIONS.items())
        for name, connection in sorted(data, key=lambda x: x[0]):
            self.stdout.write(
                self.style.WARNING('    "%s" -> %s' %
                                   (name, display_callable(connection))))
Example #2
0
    def show_signals_config(self):
        import_signals_and_functions()

        def display_callable(conn):
            fn = conn.function
            if getattr(fn, "__module__", None) and getattr(fn, "__name__", None):
                path = "%s.%s" % (fn.__module__, fn.__name__)
            elif getattr(fn, "__name__", None):
                path = fn.__name__
            else:
                path = str(fn)
            return path

        self.stdout.write(self.style.ERROR("Signals"))
        data = list(decorators.REGISTERED_SIGNALS.items())
        for name, connections in sorted(data, key=lambda x: x[0]):

            self.stdout.write(self.style.WARNING('    "%s"' % name))
            for connection in connections:
                self.stdout.write(
                    self.style.NOTICE("      -> %s" % display_callable(connection))
                )
        self.stdout.write(self.style.ERROR("Functions"))
        data = list(decorators.REGISTERED_FUNCTIONS.items())
        for name, connection in sorted(data, key=lambda x: x[0]):
            self.stdout.write(
                self.style.WARNING(
                    '    "%s" -> %s' % (name, display_callable(connection))
                )
            )
Example #3
0
 def get_context(self, request):
     if not settings.USE_CELERY:
         return {"celery_required": False}
     celery_stats = app.control.inspect().stats()
     import_signals_and_functions()
     expected_queues = {x: ("danger", "remove") for x in get_expected_queues()}
     queue_stats = app.control.inspect().active_queues()
     if queue_stats is None:
         queue_stats = {}
     for stats in queue_stats.values():
         for queue_data in stats:
             # noinspection PyTypeChecker
             if queue_data["name"] in expected_queues:
                 # noinspection PyTypeChecker
                 expected_queues[queue_data["name"]] = ("success", "ok")
     missing_queues = {x for (x, y) in expected_queues.items() if y[0] == "danger"}
     if len(missing_queues) == 1:
         messages.error(
             request,
             _('There is no worker for the "%(name)s" Celery queue.')
             % {"name": missing_queues.pop()},
         )
     elif missing_queues:
         messages.error(
             request,
             _("There is no worker for the the following Celery queues: %(name)s.")
             % {"name": ", ".join(sorted(missing_queues))},
         )
     workers = []
     if celery_stats is None:
         celery_stats = {}
     for key in sorted(celery_stats.keys(), key=lambda y: y.lower()):
         worker = {"name": key}
         infos = celery_stats[key]
         url = "%s://%s" % (
             infos["broker"]["transport"],
             infos["broker"]["hostname"],
         )
         if infos["broker"].get("port"):
             url += ":%s" % infos["broker"]["port"]
         url += "/"
         if infos["broker"].get("virtual_host"):
             url += infos["broker"]["virtual_host"]
         worker["broker"] = url
         pids = [str(infos["pid"])] + [str(y) for y in infos["pool"]["processes"]]
         worker["pid"] = ", ".join(pids)
         worker["threads"] = infos["pool"]["max-concurrency"]
         worker["timeouts"] = sum(infos["pool"]["timeouts"])
         worker["state"] = ("success", "check")
         if worker["timeouts"] > 0:
             worker["state"] = ("danger", "remove")
         # noinspection PyTypeChecker
         worker["queues"] = list({y["name"] for y in queue_stats.get(key, [])})
         worker["queues"].sort()
         workers.append(worker)
     return {
         "workers": workers,
         "expected_queues": expected_queues,
         "celery_required": True,
     }
Example #4
0
 def publish_message(window_info, message):
     if isinstance(message, bytes):
         message = message.decode("utf-8")
     if not message:
         return
     if message == settings.WEBSOCKET_HEARTBEAT:
         return
     try:
         unserialized_message = json.loads(message)
         kwargs = unserialized_message["opts"]
         # logger.debug('WS message received "%s"' % message)
         if "signal" in unserialized_message:
             signal_name = unserialized_message["signal"]
             eta = int(unserialized_message.get("eta", 0)) or None
             expires = int(unserialized_message.get("expires", 0)) or None
             countdown = int(unserialized_message.get("countdown",
                                                      0)) or None
             _call_signal(
                 window_info,
                 signal_name,
                 to=[SERVER],
                 kwargs=kwargs,
                 from_client=True,
                 eta=eta,
                 expires=expires,
                 countdown=countdown,
             )
         elif "func" in unserialized_message:
             function_name = unserialized_message["func"]
             result_id = unserialized_message["result_id"]
             import_signals_and_functions()
             if function_name in REGISTERED_FUNCTIONS:
                 fn = REGISTERED_FUNCTIONS[function_name]
                 queue = fn.get_queue(window_info, kwargs)
                 _server_function_call.apply_async(
                     [
                         function_name,
                         window_info.to_dict(), result_id, kwargs
                     ],
                     queue=queue,
                 )
             else:
                 logger.warning(
                     'Unknown function "%s" called by client "%s"' %
                     (function_name, window_info.window_key))
     except TypeError as e:
         logger.exception(e)
     except ValueError:
         logger.error("Invalid Websocket JSON message %r" % message)
         pass
     except KeyError as e:
         logger.exception(e)
Example #5
0
 def publish_message(window_info, message):
     if isinstance(message, bytes):
         message = message.decode("utf-8")
     if not message:
         return
     if message == settings.WEBSOCKET_HEARTBEAT:
         return
     try:
         unserialized_message = json.loads(message)
         kwargs = unserialized_message["opts"]
         # logger.debug('WS message received "%s"' % message)
         if "signal" in unserialized_message:
             signal_name = unserialized_message["signal"]
             eta = int(unserialized_message.get("eta", 0)) or None
             expires = int(unserialized_message.get("expires", 0)) or None
             countdown = int(unserialized_message.get("countdown", 0)) or None
             _call_signal(
                 window_info,
                 signal_name,
                 to=[SERVER],
                 kwargs=kwargs,
                 from_client=True,
                 eta=eta,
                 expires=expires,
                 countdown=countdown,
             )
         elif "func" in unserialized_message:
             function_name = unserialized_message["func"]
             result_id = unserialized_message["result_id"]
             import_signals_and_functions()
             if function_name in REGISTERED_FUNCTIONS:
                 fn = REGISTERED_FUNCTIONS[function_name]
                 queue = fn.get_queue(window_info, kwargs)
                 _server_function_call.apply_async(
                     [function_name, window_info.to_dict(), result_id, kwargs],
                     queue=queue,
                 )
             else:
                 logger.warning(
                     'Unknown function "%s" called by client "%s"'
                     % (function_name, window_info.window_key)
                 )
     except TypeError as e:
         logger.exception(e)
     except ValueError:
         logger.error("Invalid Websocket JSON message %r" % message)
         pass
     except KeyError as e:
         logger.exception(e)
Example #6
0
def signals(request):
    """Generate a JS file with the list of signals. Also configure jQuery with a CSRF header for AJAX requests.
    """
    signal_request = WindowInfo.from_request(request)
    import_signals_and_functions()
    if settings.DF_PUBLIC_SIGNAL_LIST:
        valid_signal_names = list(REGISTERED_SIGNALS.keys())
        valid_function_names = list(REGISTERED_FUNCTIONS.keys())
    else:
        valid_signal_names = []
        for signal_name, list_of_connections in REGISTERED_SIGNALS.items():
            if any(
                x.is_allowed_to is everyone or x.is_allowed_to(x, signal_request, None)
                for x in list_of_connections
            ):
                valid_signal_names.append(signal_name)
        valid_function_names = []
        for function_name, connection in REGISTERED_FUNCTIONS.items():
            if connection.is_allowed_to is everyone or connection.is_allowed_to(
                connection, signal_request, None
            ):
                valid_function_names.append(function_name)

    function_names_dict = {}
    for name in valid_function_names:
        function_names_dict[name] = (
            'function(opts) { return $.df._wsCallFunction("%(name)s", opts); }'
            % {"name": name}
        )
        name, sep, right = name.rpartition(".")
        while sep == ".":
            function_names_dict.setdefault(name, "{}")
            name, sep, right = name.rpartition(".")
    functions = OrderedDict()
    for key in sorted(function_names_dict):
        functions[key] = function_names_dict[key]

    protocol = "wss" if settings.USE_SSL else "ws"
    site_name = "%s:%s" % (settings.SERVER_NAME, settings.SERVER_PORT)

    # noinspection PyTypeChecker
    csrf_header_name = getattr(settings, "CSRF_HEADER_NAME", "HTTP_X_CSRFTOKEN")
    template_values = {
        "SIGNALS": valid_signal_names,
        "FUNCTIONS": functions,
        "WEBSOCKET_HEARTBEAT": settings.WEBSOCKET_HEARTBEAT,
        "WEBSOCKET_HEADER": settings.WEBSOCKET_HEADER,
        "CSRF_COOKIE_NAME": settings.CSRF_COOKIE_NAME,
        "DEBUG": settings.DEBUG,
        "CSRF_HEADER_NAME": csrf_header_name[5:].replace("_", "-"),
    }
    if settings.WEBSOCKET_URL:
        template_values["WEBSOCKET_URL"] = "%s://%s%s" % (
            protocol,
            site_name,
            settings.WEBSOCKET_URL,
        )
    return TemplateResponse(
        request,
        "djangofloor/signals.html",
        template_values,
        content_type=__get_js_mimetype(),
    )