| 63 | sys.path.append(self.plugins_dir) |
| 64 | |
| 65 | def discover_plugins( |
| 66 | self, |
| 67 | *, |
| 68 | sync_db: bool = True, |
| 69 | force_reload: bool = False, |
| 70 | use_cache: bool = False, |
| 71 | ) -> Dict[str, LoadedPlugin]: |
| 72 | token = self._get_reload_token() |
| 73 | if use_cache and not force_reload: |
| 74 | with self._lock: |
| 75 | if self._discovery_completed and token <= self._last_reload_token: |
| 76 | return self._registry |
| 77 | if token > self._last_reload_token: |
| 78 | force_reload = True |
| 79 | if force_reload: |
| 80 | self._touch_reload_token() |
| 81 | token = self._get_reload_token() |
| 82 | |
| 83 | if sync_db: |
| 84 | logger.info(f"Discovering plugins in {self.plugins_dir}") |
| 85 | else: |
| 86 | logger.debug(f"Discovering plugins (no DB sync) in {self.plugins_dir}") |
| 87 | |
| 88 | with self._lock: |
| 89 | previous_packages = dict(self._package_names) |
| 90 | previous_aliases = dict(self._alias_names) |
| 91 | previous_paths = { |
| 92 | key: lp.path for key, lp in self._registry.items() if lp and lp.path |
| 93 | } |
| 94 | |
| 95 | try: |
| 96 | return self._discover_plugins_impl( |
| 97 | sync_db=sync_db, |
| 98 | force_reload=force_reload, |
| 99 | previous_packages=previous_packages, |
| 100 | previous_aliases=previous_aliases, |
| 101 | previous_paths=previous_paths, |
| 102 | token=token, |
| 103 | ) |
| 104 | finally: |
| 105 | # Discovery runs outside Django's request/task cycle (boot, worker_ready). |
| 106 | close_old_connections() |
| 107 | |
| 108 | def _discover_plugins_impl( |
| 109 | self, |