| 567 | close_old_connections() |
| 568 | |
| 569 | def stop_plugin(self, key: str, reason: Optional[str] = None) -> bool: |
| 570 | try: |
| 571 | lp = self.get_plugin(key) |
| 572 | if not lp or not lp.instance: |
| 573 | return False |
| 574 | try: |
| 575 | cfg = PluginConfig.objects.get(key=key) |
| 576 | except PluginConfig.DoesNotExist: |
| 577 | return False |
| 578 | if not cfg.enabled: |
| 579 | return False |
| 580 | |
| 581 | context = self._build_context(lp, cfg) |
| 582 | if reason: |
| 583 | context["reason"] = reason |
| 584 | |
| 585 | stop_method = getattr(lp.instance, "stop", None) |
| 586 | if callable(stop_method): |
| 587 | try: |
| 588 | stop_method(context) |
| 589 | return True |
| 590 | except TypeError: |
| 591 | try: |
| 592 | stop_method() |
| 593 | return True |
| 594 | except Exception: |
| 595 | logger.exception("Plugin '%s' stop() failed", key) |
| 596 | return False |
| 597 | except Exception: |
| 598 | logger.exception("Plugin '%s' stop() failed", key) |
| 599 | return False |
| 600 | |
| 601 | run_method = getattr(lp.instance, "run", None) |
| 602 | if callable(run_method): |
| 603 | actions = {a.get("id") for a in (lp.actions or []) if isinstance(a, dict)} |
| 604 | if "stop" in actions: |
| 605 | try: |
| 606 | run_method("stop", {}, context) |
| 607 | return True |
| 608 | except Exception: |
| 609 | logger.exception("Plugin '%s' stop action failed", key) |
| 610 | return False |
| 611 | return False |
| 612 | finally: |
| 613 | close_old_connections() |
| 614 | |
| 615 | def stop_all_plugins(self, reason: Optional[str] = None) -> int: |
| 616 | stopped = 0 |