From nobody Mon Sep 16 19:00:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=patchew-devel-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=patchew-devel-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1540949961589131.4849067941136; Tue, 30 Oct 2018 18:39:21 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6BD7B83F3D; Wed, 31 Oct 2018 01:39:20 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 57AB260C7A; Wed, 31 Oct 2018 01:39:20 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 4970218005B3; Wed, 31 Oct 2018 01:39:20 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w9V1TOv3012202 for ; Tue, 30 Oct 2018 21:29:24 -0400 Received: by smtp.corp.redhat.com (Postfix) id D11136591C; Wed, 31 Oct 2018 01:29:24 +0000 (UTC) Received: from magic.redhat.com (ovpn-12-37.pek2.redhat.com [10.72.12.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id DACF25D9D6; Wed, 31 Oct 2018 01:29:10 +0000 (UTC) From: Fam Zheng To: patchew-devel@redhat.com Date: Wed, 31 Oct 2018 09:28:55 +0800 Message-Id: <20181031012856.30125-6-famz@redhat.com> In-Reply-To: <20181031012856.30125-1-famz@redhat.com> References: <20181031012856.30125-1-famz@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: patchew-devel@redhat.com Subject: [Patchew-devel] [PATCH 5/6] mod: Lazy access module config from DB X-BeenThere: patchew-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Patchew development and discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: patchew-devel-bounces@redhat.com Errors-To: patchew-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Wed, 31 Oct 2018 01:39:20 +0000 (UTC) Content-Type: text/plain; charset="utf-8" _init_module is called in app.ready where DB access is bad: the tables may not be ready yet. Also we always wanted the module config to be effective so there isn't much point in loading it here. Signed-off-by: Fam Zheng --- mod.py | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/mod.py b/mod.py index 90880ea..4446171 100644 --- a/mod.py +++ b/mod.py @@ -29,6 +29,13 @@ class PatchewModule(object): _module_init_config(self.__class__) return PC.objects.get(name=3Dself.name) =20 + def enabled(self): + try: + m =3D self.get_model() + return m.enabled + except: + return True + def get_config_raw(self): return self.get_model().config or "" =20 @@ -164,18 +171,6 @@ def _module_init_config(cls): pc.save() return pc =20 -def _init_module(cls): - name =3D cls.name - if name in _loaded_modules: - raise Exception("The module named '%s' is already loaded" % name) - pc =3D _module_init_config(cls) - if not pc.enabled: - return None - i =3D cls() - # TODO: let i.enabled follows pc.enabled - i.enabled =3D pc.enabled - return i - def load_modules(): _module_path =3D settings.MODULE_DIR sys.path.append(_module_path) @@ -187,17 +182,12 @@ def load_modules(): except: traceback.print_exc() for cls in PatchewModule.__subclasses__(): - try: - i =3D _init_module(cls) - if not i: - continue - _loaded_modules[cls.name] =3D i + if cls.name not in _loaded_modules: + _loaded_modules[cls.name] =3D cls() print("Loaded module:", cls.name) - except Exception as e: - print("Cannot load module '%s':" % cls, e) =20 def dispatch_module_hook(hook_name, **params): - for i in [x for x in list(_loaded_modules.values()) if x.enabled]: + for i in [x for x in list(_loaded_modules.values()) if x.enabled()]: if hasattr(i, hook_name): try: getattr(i, hook_name)(**params) --=20 2.17.2 _______________________________________________ Patchew-devel mailing list Patchew-devel@redhat.com https://www.redhat.com/mailman/listinfo/patchew-devel