DEV: Accquire file lock before checking if selenium webdriver cache (#25534)

Why this change?

We are getting the following error on CI:

`Text file busy -
/github/home/.cache/selenium/chromedriver/linux64/121.0.6167.85/chromedriver`

This happens when two processes tries to download the chromedriver at
the same time. I'm not entirely sure why the previous implementatio is
not locking properly since we still saw the `Text file busy` error but
by accquring the lock before we even check for the existence of
`~/.cache/selenium`, we should be able to eliminate the chance of two
processes trying to download the chromedriver binary at the same time.
This commit is contained in:
Alan Guo Xiang Tan 2024-02-02 11:04:37 +08:00 committed by GitHub
parent 140b2118af
commit 06b7a6bd59
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 4 additions and 3 deletions

View File

@ -572,9 +572,10 @@ RSpec.configure do |config|
#
# The long term fix here is to get `selenium-manager` to download the `chromedriver` binary to a unique path for each
# process but the `--cache-path` option for `selenium-manager` is currently not supported in `selenium-webdriver`.
if !File.directory?(File.expand_path("~/.cache/selenium"))
File.open("#{Rails.root}/tmp/chrome_driver_flock", File::RDWR | File::CREAT, 0644) do |file|
file.flock(File::LOCK_EX)
File.open("#{Rails.root}/tmp/chrome_driver_flock", File::RDWR | File::CREAT, 0644) do |file|
file.flock(File::LOCK_EX)
if !File.directory?(File.expand_path("~/.cache/selenium"))
`#{Selenium::WebDriver::SeleniumManager.send(:binary)} --browser chrome`
end
end