Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kill existing chromedrivers #72

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading