mirror of
https://github.com/halfwaystudent/douyin-sparkflow.git
synced 2026-09-05 07:27:53 +08:00
Handle stale task lock before scheduled send
This commit is contained in:
@@ -794,10 +794,44 @@ def task_run_lock():
|
|||||||
lock_path = Path("logs/task.run.lock")
|
lock_path = Path("logs/task.run.lock")
|
||||||
lock_path.parent.mkdir(parents=True, exist_ok=True)
|
lock_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
try:
|
def _lock_owner_is_alive(pid):
|
||||||
handle = lock_path.open("x", encoding="utf-8")
|
try:
|
||||||
except FileExistsError as exc:
|
os.kill(pid, 0)
|
||||||
raise RuntimeError("another task run is already in progress") from exc
|
except ProcessLookupError:
|
||||||
|
return False
|
||||||
|
except PermissionError:
|
||||||
|
return True
|
||||||
|
return True
|
||||||
|
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
handle = lock_path.open("x", encoding="utf-8")
|
||||||
|
break
|
||||||
|
except FileExistsError as exc:
|
||||||
|
raw_pid = lock_path.read_text(encoding="utf-8", errors="ignore").strip()
|
||||||
|
stale_pid = None
|
||||||
|
try:
|
||||||
|
stale_pid = int(raw_pid)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
stale_pid = None
|
||||||
|
|
||||||
|
if stale_pid is not None and not _lock_owner_is_alive(stale_pid):
|
||||||
|
logger.warning("Removing stale task lock owned by missing pid=%s", stale_pid)
|
||||||
|
try:
|
||||||
|
lock_path.unlink()
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
|
continue
|
||||||
|
|
||||||
|
if stale_pid is None:
|
||||||
|
logger.warning("Removing unreadable stale task lock with contents=%r", raw_pid)
|
||||||
|
try:
|
||||||
|
lock_path.unlink()
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
|
continue
|
||||||
|
|
||||||
|
raise RuntimeError("another task run is already in progress") from exc
|
||||||
|
|
||||||
try:
|
try:
|
||||||
handle.write(f"{os.getpid()}\n")
|
handle.write(f"{os.getpid()}\n")
|
||||||
|
|||||||
Reference in New Issue
Block a user