Refactor bot and integrate services
This commit is contained in:
23
lock_utils.py
Normal file
23
lock_utils.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from pathlib import Path
|
||||
import time
|
||||
|
||||
LOCK_DIR = Path("/var/run/tg-bot")
|
||||
LOCK_DIR.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
|
||||
def lock_path(name: str) -> Path:
|
||||
return LOCK_DIR / f"{name}.lock"
|
||||
|
||||
|
||||
def acquire_lock(name: str) -> bool:
|
||||
p = lock_path(name)
|
||||
if p.exists():
|
||||
return False
|
||||
p.write_text(str(time.time()))
|
||||
return True
|
||||
|
||||
|
||||
def release_lock(name: str):
|
||||
p = lock_path(name)
|
||||
if p.exists():
|
||||
p.unlink()
|
||||
Reference in New Issue
Block a user