|
1 | | -""" |
2 | | -Lua scripts for distributed semaphore operations. |
3 | | -
|
4 | | -This module loads and manages Redis Lua scripts for atomic semaphore operations. |
5 | | -The scripts are stored in individual .lua files in the lua/ subfolder. |
6 | | -""" |
7 | | - |
8 | | -import pathlib |
| 1 | +from importlib import resources |
9 | 2 | from typing import Final |
10 | 3 |
|
11 | | -# Path to the lua scripts directory |
12 | | -_LUA_DIR = pathlib.Path(__file__).parent / "lua" |
13 | | - |
14 | 4 |
|
15 | 5 | def _load_script(script_name: str) -> str: |
16 | | - """Load a Lua script from the lua/ directory.""" |
17 | | - script_file = _LUA_DIR / f"{script_name}.lua" |
18 | | - if not script_file.exists(): |
19 | | - msg = f"Lua script file not found: {script_file}" |
20 | | - raise FileNotFoundError(msg) |
21 | | - return script_file.read_text(encoding="utf-8").strip() |
| 6 | + with resources.path("servicelib", f"redis.lua.{script_name}ga") as script_file: |
| 7 | + return script_file.read_text(encoding="utf-8").strip() |
22 | 8 |
|
23 | 9 |
|
24 | | -# Load individual scripts |
25 | 10 | ACQUIRE_SEMAPHORE_SCRIPT: Final[str] = _load_script("acquire_semaphore") |
26 | 11 | RELEASE_SEMAPHORE_SCRIPT: Final[str] = _load_script("release_semaphore") |
27 | 12 | RENEW_SEMAPHORE_SCRIPT: Final[str] = _load_script("renew_semaphore") |
28 | 13 | COUNT_SEMAPHORE_SCRIPT: Final[str] = _load_script("count_semaphore") |
29 | | - |
30 | | -__all__ = [ |
31 | | - "ACQUIRE_SEMAPHORE_SCRIPT", |
32 | | - "COUNT_SEMAPHORE_SCRIPT", |
33 | | - "RELEASE_SEMAPHORE_SCRIPT", |
34 | | - "RENEW_SEMAPHORE_SCRIPT", |
35 | | -] |
0 commit comments