diff --git a/discogs_alert/client.py b/discogs_alert/client.py index 68e74b5..49ae8d8 100644 --- a/discogs_alert/client.py +++ b/discogs_alert/client.py @@ -10,9 +10,11 @@ import sys from typing import Union +import psutil import requests from fake_useragent import UserAgent from selenium import webdriver +from selenium.common.exceptions import WebDriverException from selenium.webdriver.chrome.service import Service as ChromiumService from selenium.webdriver.chromium.options import ChromiumOptions from webdriver_manager.chrome import ChromeDriverManager @@ -22,6 +24,15 @@ logger = logging.getLogger(__name__) +def kill_chromedriver_processes(): + for process in psutil.process_iter(["pid", "name", "cmdline"]): + pinfo = process.info + if pinfo["name"] == "chromedriver" or (pinfo["cmdline"] is not None and "chromedriver" in pinfo["cmdline"]): + if process.status() != "zombie": + print(f"Terminating chromedriver process (PID {pinfo['pid']})") + process.terminate() + + class Client: """API Client to interact with discogs server. Taken & modified from https://github.com/joalla/discogs_client.""" @@ -124,7 +135,12 @@ def __init__(self, user_agent: str, *args, **kwargs): for argument in options_arguments: options.add_argument(argument) - self.driver = webdriver.Chrome(service=service, options=options) + # If we get an exception because some previous chromedriver instance is still running, we kill it + try: + self.driver = webdriver.Chrome(service=service, options=options) + except WebDriverException as we: + kill_chromedriver_processes() + raise we("We have killed the running chromedriver processes; DA should work next time it is called.") def get_driver_path(self): try: diff --git a/pyproject.toml b/pyproject.toml index 1807d61..ed1d58b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,6 +18,7 @@ dacite = "^1.8.0" fake-useragent = "^1.1.1" freecurrencyapi = "^0.1.0" pre-commit = "^2.20.0" +psutil = "^5.9.7" requests = "^2.28.2" ruff = "^0.0.253" schedule = "^1.1.0"