Skip to content

Commit

Permalink
Properly kill running chromedrivers (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelhball authored Apr 3, 2024
1 parent 04ead2e commit 8eb0c55
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
18 changes: 17 additions & 1 deletion discogs_alert/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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."""

Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 8eb0c55

Please sign in to comment.