Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
(cherry picked from commit d389fff) Co-authored-by: Ankush Menat <ankush@frappe.io>
- Loading branch information
1 parent
da28290
commit b34e8ce
Showing
6 changed files
with
87 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
""" Utils for thread/process synchronization. """ | ||
|
||
import os | ||
from contextlib import contextmanager | ||
|
||
from filelock import FileLock as _StrongFileLock | ||
from filelock import Timeout | ||
|
||
import frappe | ||
from frappe import _ | ||
from frappe.utils import get_bench_path, get_site_path | ||
from frappe.utils.file_lock import LockTimeoutError | ||
|
||
LOCKS_DIR = "locks" | ||
|
||
|
||
@contextmanager | ||
def filelock(lock_name: str, *, timeout=30, is_global=False): | ||
"""Create a lockfile to prevent concurrent operations acrosss processes. | ||
args: | ||
lock_name: Unique name to identify a specific lock. Lockfile called `{name}.lock` will be | ||
created. | ||
timeout: time to wait before failing. | ||
is_global: if set lock is global to bench | ||
Lock file location: | ||
global - {bench_dir}/config/{name}.lock | ||
site - {bench_dir}/sites/sitename/{name}.lock | ||
""" | ||
|
||
lock_filename = lock_name + ".lock" | ||
if not is_global: | ||
lock_path = os.path.abspath(get_site_path(LOCKS_DIR, lock_filename)) | ||
else: | ||
lock_path = os.path.abspath(os.path.join(get_bench_path(), "config", lock_filename)) | ||
|
||
try: | ||
with _StrongFileLock(lock_path, timeout=timeout): | ||
yield | ||
except Timeout as e: | ||
frappe.log_error("Filelock: Failed to aquire {lock_path}") | ||
|
||
raise LockTimeoutError( | ||
_("Failed to aquire lock: {}").format(lock_name) | ||
+ "<br>" | ||
+ _("You can manually remove the lock if you think it's safe: {}").format(lock_path) | ||
) from e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters