2026-10-22 16:49:25 +08:00
|
|
|
"""Docker worker liveness probe: no localhost Web dependency."""
|
|
|
|
|
import os
|
|
|
|
|
import time
|
2026-10-23 12:31:03 +08:00
|
|
|
import socket
|
|
|
|
|
import re
|
2026-10-22 16:49:25 +08:00
|
|
|
from pathlib import Path
|
|
|
|
|
from . import db
|
|
|
|
|
|
2026-10-23 12:31:03 +08:00
|
|
|
def heartbeat_path():
|
|
|
|
|
instance=os.environ.get('SITEFORGE_WORKER_INSTANCE', '')
|
|
|
|
|
if not instance and db.POSTGRES:
|
|
|
|
|
instance=socket.gethostname()
|
|
|
|
|
if instance:
|
|
|
|
|
if not re.fullmatch(r'[A-Za-z0-9._-]{1,120}',instance):
|
|
|
|
|
raise ValueError('Invalid worker instance identifier')
|
|
|
|
|
return db.DATA/'worker-heartbeats'/instance
|
|
|
|
|
return db.DATA/'worker-heartbeat'
|
|
|
|
|
|
|
|
|
|
def healthy(path=None, max_age=None, check_store=False):
|
|
|
|
|
file=Path(path) if path is not None else heartbeat_path()
|
2026-10-22 16:49:25 +08:00
|
|
|
limit=float(max_age if max_age is not None else os.getenv('SITEFORGE_WORKER_HEALTH_MAX_AGE','45'))
|
|
|
|
|
if limit <= 0:return False
|
|
|
|
|
try:
|
|
|
|
|
age=time.time()-file.stat().st_mtime
|
2026-10-23 12:31:03 +08:00
|
|
|
fresh=-5 <= age < limit
|
|
|
|
|
if not fresh:return False
|
|
|
|
|
if check_store:
|
|
|
|
|
from .healthchecks import readiness
|
|
|
|
|
return readiness()
|
|
|
|
|
return True
|
|
|
|
|
except (OSError,ValueError):
|
2026-10-22 16:49:25 +08:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
if __name__=='__main__':
|
2026-10-23 12:31:03 +08:00
|
|
|
raise SystemExit(0 if healthy(check_store=True) else 1)
|